Class: EasyJSONMatcher::AttributeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_json_matcher/attribute_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_opts:, global_opts:, **args) ⇒ AttributeGenerator

Returns a new instance of AttributeGenerator.



7
8
9
10
# File 'lib/easy_json_matcher/attribute_generator.rb', line 7

def initialize(local_opts:, global_opts:, **args)
  super(**args)
  @opts = extract_opts(locals: local_opts, globals: global_opts)
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



5
6
7
# File 'lib/easy_json_matcher/attribute_generator.rb', line 5

def factory
  @factory
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/easy_json_matcher/attribute_generator.rb', line 5

def opts
  @opts
end

Instance Method Details

#extract_opts(locals:, globals:) ⇒ Object



16
17
18
# File 'lib/easy_json_matcher/attribute_generator.rb', line 16

def extract_opts(locals:, globals:)
  (globals.reject(&non_attribute_options_filter) + locals).map(&override_local_values(locals: locals))
end

#generate_attributeObject



12
13
14
# File 'lib/easy_json_matcher/attribute_generator.rb', line 12

def generate_attribute
  chain_factory.get_chain(steps: opts)
end

#non_attribute_options_filterObject

TODO this method should whitelist the options, and the options should be in a constant Remember to change extract_ops so that it uses globals.select afterwards



22
23
24
25
# File 'lib/easy_json_matcher/attribute_generator.rb', line 22

def non_attribute_options_filter
  non_attribute_options = [:strict]
  ->(opt) { non_attribute_options.include? opt }
end

#override_local_values(locals:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/easy_json_matcher/attribute_generator.rb', line 27

def override_local_values(locals:)
  conflicts = { required: :not_required }
  ->(opt) { 
    if conflicts.keys.include? opt
      locals.include?(conflicts[opt]) ? conflicts[opt] : opt
    else
      opt
    end
  }
end