Module: OptStruct::ClassMethods

Defined in:
lib/opt_struct/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_callback(name, callback) ⇒ Object



89
90
91
92
93
# File 'lib/opt_struct/class_methods.rb', line 89

def add_callback(name, callback)
  @_callbacks ||= {}
  @_callbacks[name] ||= []
  @_callbacks[name] << callback
end

#all_callbacksObject



95
96
97
# File 'lib/opt_struct/class_methods.rb', line 95

def all_callbacks
  @_callbacks
end

#around_init(meth = nil, &blk) ⇒ Object



85
86
87
# File 'lib/opt_struct/class_methods.rb', line 85

def around_init(meth = nil, &blk)
  add_callback(:around_init, meth || blk)
end

#before_init(meth = nil, &blk) ⇒ Object



81
82
83
# File 'lib/opt_struct/class_methods.rb', line 81

def before_init(meth = nil, &blk)
  add_callback(:before_init, meth || blk)
end

#defaultsObject



24
25
26
# File 'lib/opt_struct/class_methods.rb', line 24

def defaults
  @defaults ||= {}
end

#defined_keysObject



12
13
14
# File 'lib/opt_struct/class_methods.rb', line 12

def defined_keys
  @defined_keys ||= []
end

#expect_arguments(*arguments) ⇒ Object Also known as: expect_argument



70
71
72
73
# File 'lib/opt_struct/class_methods.rb', line 70

def expect_arguments(*arguments)
  required(*arguments)
  expected_arguments.concat(arguments)
end

#expected_argumentsObject



20
21
22
# File 'lib/opt_struct/class_methods.rb', line 20

def expected_arguments
  @expected_arguments ||= []
end

#inherited(subclass) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/opt_struct/class_methods.rb', line 3

def inherited(subclass)
  # intersection of defined vars and the ones we care about
  (instance_variables & OptStruct::CLASS_IVARS).each do |ivar|
    # copy them to the child class
    subclass.send(:instance_variable_set, ivar, instance_variable_get(ivar).dup)
  end
  super(subclass)
end

#init(meth = nil, &blk) ⇒ Object Also known as: after_init



76
77
78
# File 'lib/opt_struct/class_methods.rb', line 76

def init(meth = nil, &blk)
  add_callback(:init, meth || blk)
end

#option(key, default = OptStruct::DEFAULT, required: false, **options) ⇒ Object



55
56
57
58
59
60
# File 'lib/opt_struct/class_methods.rb', line 55

def option(key, default = OptStruct::DEFAULT, required: false, **options)
  default = options[:default] if options.key?(:default)
  defaults[key] = default unless default == OptStruct::DEFAULT
  required_keys << key if required
  option_accessor key, **options
end

#option_accessor(*keys, **options) ⇒ Object



48
49
50
51
52
53
# File 'lib/opt_struct/class_methods.rb', line 48

def option_accessor(*keys, **options)
  check_reserved_words(keys)
  defined_keys.concat keys
  option_reader *keys, **options
  option_writer *keys, **options
end

#option_reader(*keys, **opts) ⇒ Object



33
34
35
36
37
38
# File 'lib/opt_struct/class_methods.rb', line 33

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) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/opt_struct/class_methods.rb', line 40

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) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/opt_struct/class_methods.rb', line 62

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) ⇒ Object



28
29
30
31
# File 'lib/opt_struct/class_methods.rb', line 28

def required(*keys, **options)
  required_keys.concat keys
  option_accessor *keys, **options
end

#required_keysObject



16
17
18
# File 'lib/opt_struct/class_methods.rb', line 16

def required_keys
  @required_keys ||= []
end