Class: EasyJSONMatcher::ArrayGenerator

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

Constant Summary collapse

WHITELIST =
[:required, :not_required]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_opts:, global_opts:, target_class: ArrayValidator) ⇒ ArrayGenerator

Returns a new instance of ArrayGenerator.



8
9
10
11
12
# File 'lib/easy_json_matcher/array_generator.rb', line 8

def initialize(local_opts:, global_opts:, target_class: ArrayValidator)
  @target_class = target_class
  @opts = extract_opts(locals: local_opts, globals: global_opts)
  @content_opts = []
end

Instance Attribute Details

#content_optsObject (readonly)

Returns the value of attribute content_opts.



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

def content_opts
  @content_opts
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#target_classObject (readonly)

Returns the value of attribute target_class.



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

def target_class
  @target_class
end

Instance Method Details

#array_options_filterObject



26
27
28
# File 'lib/easy_json_matcher/array_generator.rb', line 26

def array_options_filter
  ->(opt) { WHITELIST.include?(opt) }
end

#elements_should(be:) ⇒ Object



18
19
20
# File 'lib/easy_json_matcher/array_generator.rb', line 18

def elements_should(be:)
  @content_opts = be
end

#extract_opts(locals:, globals:) ⇒ Object



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

def extract_opts(locals:, globals:)
  (globals.select(&array_options_filter) + locals).map(&override_local_values(locals: locals))
end

#generate_arrayObject



14
15
16
# File 'lib/easy_json_matcher/array_generator.rb', line 14

def generate_array
  target_class.new(opts: opts, verify_content_as: content_opts)
end

#override_local_values(locals:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/easy_json_matcher/array_generator.rb', line 30

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