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)



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/inquisitive/hash.rb', line 51

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.



5
6
7
# File 'lib/inquisitive/hash.rb', line 5

def negated
  @negated
end

Instance Method Details

#===(other) ⇒ Object



10
11
12
# File 'lib/inquisitive/hash.rb', line 10

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

#[](key) ⇒ Object



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

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

#fetch(key, default = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inquisitive/hash.rb', line 19

def fetch(key, default=nil)
  key = convert_key(key)
  value = self[key]
  if Inquisitive.present? value
    value
  else
    if block_given?
      yield(key)
    else
      default
    end
  end
end

#noObject



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

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

#regular_readerObject



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

alias_method :regular_reader, :[]