module OptStruct::ClassMethods

Constants

RESERVED_WORDS

Public Instance Methods

add_callback(name, callback) click to toggle source
# File lib/opt_struct/class_methods.rb, line 82
def add_callback(name, callback)
  @_callbacks ||= {}
  @_callbacks[name] ||= []
  @_callbacks[name] << callback
end
after_init(meth = nil, &blk)
Alias for: init
all_callbacks() click to toggle source
# File lib/opt_struct/class_methods.rb, line 88
def all_callbacks
  @_callbacks
end
around_init(meth = nil, &blk) click to toggle source
# File lib/opt_struct/class_methods.rb, line 78
def around_init(meth = nil, &blk)
  add_callback(:around_init, meth || blk)
end
before_init(meth = nil, &blk) click to toggle source
# File lib/opt_struct/class_methods.rb, line 74
def before_init(meth = nil, &blk)
  add_callback(:before_init, meth || blk)
end
defaults() click to toggle source
# File lib/opt_struct/class_methods.rb, line 55
def defaults
  @defaults ||= {}
end
expect_argument(*arguments)
Alias for: expect_arguments
expect_arguments(*arguments) click to toggle source
# File lib/opt_struct/class_methods.rb, line 59
def expect_arguments(*arguments)
  required(*arguments)
  expected_arguments.concat(arguments)
end
Also aliased as: expect_argument
expected_arguments() click to toggle source
# File lib/opt_struct/class_methods.rb, line 65
def expected_arguments
  @expected_arguments ||= []
end
inherited(subclass) click to toggle source
# File lib/opt_struct/class_methods.rb, line 3
def inherited(subclass)
  instance_variables.each do |v|
    ivar = instance_variable_get(v)
    subclass.send(:instance_variable_set, v, ivar.dup) if ivar
  end
end
init(meth = nil, &blk) click to toggle source
# File lib/opt_struct/class_methods.rb, line 69
def init(meth = nil, &blk)
  add_callback(:init, meth || blk)
end
Also aliased as: after_init
option(key, default = nil, required: false, **options) click to toggle source
# File lib/opt_struct/class_methods.rb, line 40
def option(key, default = nil, required: false, **options)
  default = options[:default] if options.key?(:default)
  defaults[key] = default if default
  required_keys << key if required
  option_accessor key, **options
end
option_accessor(*keys, **options) click to toggle source
# File lib/opt_struct/class_methods.rb, line 34
def option_accessor(*keys, **options)
  check_reserved_words(keys)
  option_reader *keys, **options
  option_writer *keys, **options
end
option_reader(*keys, **opts) click to toggle source
# File lib/opt_struct/class_methods.rb, line 19
def option_reader(*keys, **opts)
  keys.each do |key|
    define_method(key) { options[key] }
    private key if opts[:private]
  end
end
option_writer(*keys, **opts) click to toggle source
# File lib/opt_struct/class_methods.rb, line 26
def option_writer(*keys, **opts)
  keys.each do |key|
    meth = "#{key}=".to_sym
    define_method(meth) { |value| options[key] = value }
    private meth if opts[:private]
  end
end
options(*keys, **keys_defaults) click to toggle source
# File lib/opt_struct/class_methods.rb, line 47
def options(*keys, **keys_defaults)
  option_accessor *keys if keys.any?
  if keys_defaults.any?
    defaults.merge!(keys_defaults)
    option_accessor *(keys_defaults.keys - expected_arguments)
  end
end
required(*keys, **options) click to toggle source
# File lib/opt_struct/class_methods.rb, line 14
def required(*keys, **options)
  required_keys.concat keys
  option_accessor *keys, **options
end
required_keys() click to toggle source
# File lib/opt_struct/class_methods.rb, line 10
def required_keys
  @required_keys ||= []
end

Private Instance Methods

check_reserved_words(words) click to toggle source
# File lib/opt_struct/class_methods.rb, line 96
def check_reserved_words(words)
  Array(words).each do |word|
    if RESERVED_WORDS.member?(word)
      raise ArgumentError, "Use of reserved word is not permitted: #{word.inspect}"
    end
  end
end