Class: Adapter::ActiveModel::CommonHelpers::ObjectHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/api_helpers/adapter/active_model/common_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, existing: true) ⇒ ObjectHash

Returns a new instance of ObjectHash.



30
31
32
33
# File 'lib/rspec/api_helpers/adapter/active_model/common_helpers.rb', line 30

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/api_helpers/adapter/active_model/common_helpers.rb', line 35

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



29
30
31
# File 'lib/rspec/api_helpers/adapter/active_model/common_helpers.rb', line 29

def existing
  @existing
end

#hashObject

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



29
30
31
# File 'lib/rspec/api_helpers/adapter/active_model/common_helpers.rb', line 29

def hash
  @hash
end