Class: CastedHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/casted_hash.rb

Constant Summary collapse

VERSION =
"0.8.4"

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}, cast_proc = nil) ⇒ CastedHash

Returns a new instance of CastedHash.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/casted_hash.rb', line 4

def initialize(constructor = {}, cast_proc = nil)
  raise ArgumentError, "`cast_proc` required" unless cast_proc

  @cast_proc = cast_proc
  @casting_keys = []

  if constructor.is_a?(CastedHash)
    @casted_keys = constructor.instance_variable_get(:@casted_keys).dup
    super()
    update(constructor)
  elsif constructor.is_a?(Hash)
    @casted_keys = []
    super()
    update(constructor)
  else
    @casted_keys = []
    super(constructor)
  end
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/casted_hash.rb', line 35

def [](key)
  cast! convert_key(key)
end

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



52
53
54
55
56
# File 'lib/casted_hash.rb', line 52

def []=(key, value)
  key = convert_key(key)
  uncast! key
  regular_writer(key, value)
end

#castedObject



141
142
143
144
145
146
147
# File 'lib/casted_hash.rb', line 141

def casted
  Hash.new.tap do |hash|
    @casted_keys.each do |key|
      hash[key] = regular_reader(key)
    end
  end
end

#casted!(*keys) ⇒ Object



149
150
151
152
153
# File 'lib/casted_hash.rb', line 149

def casted!(*keys)
  keys.each do |key|
    @casted_keys << convert_key(key) if key?(key)
  end
end

#casted?(key) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/casted_hash.rb', line 125

def casted?(key)
  @casted_keys.include?(convert_key(key))
end

#casted_hashObject



120
121
122
123
# File 'lib/casted_hash.rb', line 120

def casted_hash
  cast_all!
  self
end

#casting!(*keys) ⇒ Object



155
156
157
158
159
# File 'lib/casted_hash.rb', line 155

def casting!(*keys)
  keys.each do |key|
    @casting_keys << convert_key(key) if key?(key)
  end
end

#casting?(key) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/casted_hash.rb', line 129

def casting?(key)
  @casting_keys.include?(convert_key(key))
end

#delete(key) ⇒ Object



104
105
106
107
108
# File 'lib/casted_hash.rb', line 104

def delete(key)
  key = convert_key(key)
  uncast! key
  super(key)
end

#dupObject



96
97
98
99
100
101
102
# File 'lib/casted_hash.rb', line 96

def dup
  duplicate = super
  instance_variables.each do |instance_variable|
    duplicate.instance_variable_set(instance_variable, instance_variable_get(instance_variable).dup)
  end
  duplicate
end

#eachObject



115
116
117
118
# File 'lib/casted_hash.rb', line 115

def each
  cast_all!
  super
end

#fetch(key, *extras) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/casted_hash.rb', line 39

def fetch(key, *extras)
  key = convert_key(key)
  value = cast!(key)

  if value.nil?
    super(key, *extras)
  else
    value
  end
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


84
85
86
# File 'lib/casted_hash.rb', line 84

def key?(key)
  super(convert_key(key))
end

#merge(hash) ⇒ Object



60
61
62
# File 'lib/casted_hash.rb', line 60

def merge(hash)
  self.dup.update(hash)
end

#regular_readerObject



24
# File 'lib/casted_hash.rb', line 24

alias_method :regular_reader, :[]

#regular_writerObject



50
# File 'lib/casted_hash.rb', line 50

alias_method :regular_writer, :[]=

#to_hashObject



133
134
135
136
137
138
139
# File 'lib/casted_hash.rb', line 133

def to_hash
  Hash.new.tap do |hash|
    keys.each do |key|
      hash[key] = regular_reader(key)
    end
  end
end

#transform_keysObject



26
27
28
29
30
31
32
33
# File 'lib/casted_hash.rb', line 26

def transform_keys
  return enum_for(:transform_keys) unless block_given?
  result = dup
  each_key do |key|
    result[yield(key)] = regular_reader(key)
  end
  result
end

#update(other_hash) ⇒ Object Also known as: merge!



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/casted_hash.rb', line 64

def update(other_hash)
  return self if other_hash.empty?

  other_hash.each_pair do |key, value|
    converted_key = convert_key(key)

    regular_writer converted_key, value

    if other_hash.is_a?(CastedHash) && other_hash.casted?(key)
      casted!(key)
    elsif casted?(key)
      uncast!(converted_key)
    end
  end

  self
end

#valuesObject



110
111
112
113
# File 'lib/casted_hash.rb', line 110

def values
  cast_all!
  super
end

#values_at(*indices) ⇒ Object



92
93
94
# File 'lib/casted_hash.rb', line 92

def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end