Class: Inquisitive::Hash

Inherits:
HashWithIndifferentAccess show all
Includes:
Utils
Defined in:
lib/inquisitive/hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HashWithIndifferentAccess

[], #[]=, #assert_valid_keys, #default, #delete, #extractable_options?, #initialize, #key?, #merge, #nested_under_indifferent_access, #regular_update, #regular_writer, #reject, #replace, #select, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_hash, #to_options!, #transform_keys, #transform_keys!, #update, #values_at, #with_indifferent_access

Constructor Details

This class inherits a constructor from Inquisitive::HashWithIndifferentAccess

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/inquisitive/hash.rb', line 55

def method_missing(method_name, *arguments)
  if predicate_method? method_name
    if has_key? predication(method_name)
      Inquisitive.present? self[predication(method_name)]
    else
      false
    end ^ negated
  else
    Inquisitive[self[method_name]]
  end
end

Instance Attribute Details

#negatedObject

Returns the value of attribute negated.



7
8
9
# File 'lib/inquisitive/hash.rb', line 7

def negated
  @negated
end

Instance Method Details

#===(other) ⇒ Object



12
13
14
# File 'lib/inquisitive/hash.rb', line 12

def === other
  other.class == Class and other == ::Hash or super
end

#[](key) ⇒ Object



17
18
19
# File 'lib/inquisitive/hash.rb', line 17

def [](key)
  Inquisitive[regular_reader key]
end

#fetch(key, default = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/inquisitive/hash.rb', line 21

def fetch(key, default=nil)
  key = convert_key(key)
  value = self[key]
  if Inquisitive.present? value
    value
  else
    if block_given?
      yield(key)
    elsif default
      default
    else
      raise KeyError, "key not found: #{key}"
    end
  end
end

#noObject



8
9
10
# File 'lib/inquisitive/hash.rb', line 8

def no
  dup.tap{ |s| s.negated = !s.negated }
end

#regular_readerObject



16
# File 'lib/inquisitive/hash.rb', line 16

alias_method :regular_reader, :[]