Module: OptStruct::ClassMethods

Defined in:
lib/opt_struct/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_callback(name, callback) ⇒ Object



96
97
98
99
100
# File 'lib/opt_struct/class_methods.rb', line 96

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

#all_callbacksObject



102
103
104
# File 'lib/opt_struct/class_methods.rb', line 102

def all_callbacks
  @_callbacks
end

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



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

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

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



88
89
90
# File 'lib/opt_struct/class_methods.rb', line 88

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

#defaultsObject



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

def defaults
  @defaults ||= {}
end

#defined_keysObject



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

def defined_keys
  @defined_keys ||= []
end

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



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

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

#expected_argumentsObject



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

def expected_arguments
  @expected_arguments ||= []
end

#inherited(subclass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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 each to the child class
    value =
      case ivar
      when :@_callbacks # Hash that we need to duplicate deeper
        instance_variable_get(ivar).transform_values(&:dup)
      else
        instance_variable_get(ivar).dup
      end
    subclass.send(:instance_variable_set, ivar, value)
  end
  super(subclass)
end

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



83
84
85
# File 'lib/opt_struct/class_methods.rb', line 83

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

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



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

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



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

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



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

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



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

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



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

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



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

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

#required_keysObject



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

def required_keys
  @required_keys ||= []
end