Module: OptStruct::ClassMethods

Defined in:
lib/opt_struct/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_callback(name, callback) ⇒ Object



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

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

#all_callbacksObject



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

def all_callbacks
  @_callbacks
end

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



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

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

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



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

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

#defaultsObject



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

def defaults
  @defaults ||= {}
end

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



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

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

#expected_argumentsObject



68
69
70
# File 'lib/opt_struct/class_methods.rb', line 68

def expected_arguments
  @expected_arguments ||= []
end

#inherited(subclass) ⇒ Object



3
4
5
6
7
8
# 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) ⇒ Object Also known as: after_init



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

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

#option(key, default = nil, **options) ⇒ Object



43
44
45
46
47
48
# File 'lib/opt_struct/class_methods.rb', line 43

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

#option_accessor(*keys) ⇒ Object



37
38
39
40
41
# File 'lib/opt_struct/class_methods.rb', line 37

def option_accessor(*keys)
  check_reserved_words(keys)
  option_reader *keys
  option_writer *keys
end

#option_reader(*keys) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/opt_struct/class_methods.rb', line 19

def option_reader(*keys)
  keys.each do |key|
    define_method(key) do
      if options.key?(key)
        options[key]
      elsif defaults.key?(key)
        options[key] = read_default_value(key)
      end
    end
  end
end

#option_writer(*keys) ⇒ Object



31
32
33
34
35
# File 'lib/opt_struct/class_methods.rb', line 31

def option_writer(*keys)
  keys.each do |key|
    define_method("#{key}=") { |value| options[key] = value }
  end
end

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



50
51
52
53
54
55
56
# File 'lib/opt_struct/class_methods.rb', line 50

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



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

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

#required_keysObject



10
11
12
# File 'lib/opt_struct/class_methods.rb', line 10

def required_keys
  @required_keys ||= []
end