Class: Flex::Struct::Hash

Inherits:
Hash
  • Object
show all
Includes:
Symbolize
Defined in:
lib/flex/struct/hash.rb

Direct Known Subclasses

Variables

Defined Under Namespace

Modules: Nil

Instance Method Summary collapse

Methods included from Symbolize

#symbolize

Constructor Details

#initializeHash

Returns a new instance of Hash.



6
7
8
9
10
11
12
13
# File 'lib/flex/struct/hash.rb', line 6

def initialize
  super do |hash, key|
    if key[-1] == '!'
      klass = (key[0] == '_' ? Array : Hash)
      hash[clean_key(key)] = klass.new
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



37
38
39
40
# File 'lib/flex/struct/hash.rb', line 37

def [](key)
  cleaned = clean_key(key)
  super has_key?(cleaned) ? cleaned : key.to_sym
end

#deep_merge(*hashes) ⇒ Object



42
43
44
45
46
# File 'lib/flex/struct/hash.rb', line 42

def deep_merge(*hashes)
  dupe = deep_dup(self)
  hashes.each {|h2| dupe.replace(deep_merge_hash(dupe,h2))}
  dupe
end

#deep_merge!(*hashes) ⇒ Object



48
49
50
# File 'lib/flex/struct/hash.rb', line 48

def deep_merge!(*hashes)
  replace deep_merge(*hashes)
end

#fetch(key, *rest, &block) ⇒ Object



32
33
34
35
# File 'lib/flex/struct/hash.rb', line 32

def fetch(key, *rest, &block)
  cleaned = clean_key(key)
  super has_key?(cleaned) ? cleaned : key.to_sym, *rest, &block
end

#merge(hash) ⇒ Object



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

def merge(hash)
  super symbolize(hash)
end

#merge!(hash) ⇒ Object



19
20
21
# File 'lib/flex/struct/hash.rb', line 19

def merge!(hash)
  super symbolize(hash)
end

#store(key, val) ⇒ Object Also known as: []=



23
24
25
26
27
28
29
# File 'lib/flex/struct/hash.rb', line 23

def store(key, val)
  if key[-1] == '='
    super key[0..-2].to_sym, val.extend(AsIs)
  else
    super clean_key(key), symbolize(val)
  end
end

#try(key) ⇒ Object



58
59
60
# File 'lib/flex/struct/hash.rb', line 58

def try(key)
  has_key?(key) ? self[key] : nil.extend(Nil)
end

#try_delete(key, *rest, &block) ⇒ Object



62
63
64
65
# File 'lib/flex/struct/hash.rb', line 62

def try_delete(key, *rest, &block)
  val = delete clean_key(key), *rest, &block
  val.nil? ? nil.extend(Nil) : val
end