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:, **args) ⇒ ArrayGenerator

Returns a new instance of ArrayGenerator.



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

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

Instance Attribute Details

#array_validatorObject (readonly)

Returns the value of attribute array_validator.



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

def array_validator
  @array_validator
end

#content_optsObject (readonly)

Returns the value of attribute content_opts.



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

def content_opts
  @content_opts
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#array_options_filterObject



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

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

#elements_should(be:) ⇒ Object



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

def elements_should(be:)
  @content_opts = be
end

#extract_opts(locals:, globals:) ⇒ Object



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

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

#generate_arrayObject



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

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

#override_local_values(locals:) ⇒ Object



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

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