Class: Rspec::ApiHelpers::ExampleMethods::ObjectHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/api_helpers/example_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, existing: true) ⇒ ObjectHash

Returns a new instance of ObjectHash.



40
41
42
43
# File 'lib/rspec/api_helpers/example_methods.rb', line 40

def initialize(hash, existing: true)
  @hash = HashWithIndifferentAccess.new(hash)
  @existing = existing
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rspec/api_helpers/example_methods.rb', line 44

def method_missing(name)
  if existing
    if hash.key?(name)
      return hash[name]
    else
      return raise KeyError.new("Attribute not found in resource: #{name}")
    end
  else
    if hash.key?(name)
      return raise(
        KeyError.new(
          "Attribute found in resource when it shouldn't: #{name}"
        )
      )
    else
      return :attribute_not_found
    end
  end
end

Instance Attribute Details

#existingObject

existing denotes whether we search for attributes that exist on the resource or attributes that shouldn’t exist



39
40
41
# File 'lib/rspec/api_helpers/example_methods.rb', line 39

def existing
  @existing
end

#hashObject

existing denotes whether we search for attributes that exist on the resource or attributes that shouldn’t exist



39
40
41
# File 'lib/rspec/api_helpers/example_methods.rb', line 39

def hash
  @hash
end