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(attribute_factory: ValidationChainFactory, local_opts:, global_opts:) ⇒ AttributeGenerator

Returns a new instance of AttributeGenerator.



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

def initialize(attribute_factory: ValidationChainFactory, local_opts:, global_opts:)
  @factory = attribute_factory
  @opts = extract_opts(locals: local_opts, globals: global_opts)
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



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

def factory
  @factory
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#extract_opts(locals:, globals:) ⇒ Object



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

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

#generate_attributeObject



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

def generate_attribute
  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



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

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

#override_local_values(locals:) ⇒ Object



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

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