Class: Confstruct::HashWithStructAccess
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- Confstruct::HashWithStructAccess
show all
- Includes:
- Hashie::Extensions::DeepMerge, Hashie::Extensions::Mash::SafeAssignment
- Defined in:
- lib/confstruct/hash_with_struct_access.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 134
def method_missing sym, *args, &block
name = sym.to_s.chomp('=').to_sym
result = nil
if name.to_s =~ /^add_(.+)!$/
name = $1.to_sym
self.assign_property(name, []) unless self.has_key?(name)
unless self[name].is_a?(Array)
raise TypeError, "Cannot #add! to a #{self[name].class}"
end
if args.length > 0
local_args = args.collect { |a| self.class.structurize a }
result = self[name].push *local_args
elsif block_given?
result = HashWithStructAccess.new
self[name].push result
end
elsif args.length == 1
self.assign_property(name, args[0])
result = self[name]
elsif args.length > 1
super(sym,*args,&block)
else
result = self[name]
if result.nil? and block_given?
self.assign_property(name, HashWithStructAccess.new)
result = self[name]
end
end
if block_given?
eval_or_yield result, &block
end
result
end
|
Instance Attribute Details
#default_values ⇒ Object
Returns the value of attribute default_values.
39
40
41
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 39
def default_values
@default_values
end
|
Class Method Details
.from_hash(hash) ⇒ Object
52
53
54
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 52
def self.from_hash(hash)
self.new(hash)
end
|
.structurize(hash) ⇒ Object
126
127
128
129
130
131
132
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 126
def self.structurize hash
result = hash
if result.is_a?(Hash) and not result.is_a?(HashWithStructAccess)
result = HashWithStructAccess.new(result)
end
result
end
|
Instance Method Details
#[](key) ⇒ Object
Override for Deferred support
76
77
78
79
80
81
82
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 76
def [] key
result = super
if result.is_a?(Deferred)
result = eval_or_yield self, &result.block
end
result
end
|
#convert_key(key) ⇒ Object
Hashie::Mash normally standardizes all keys as strings We can override this method to standardize as symbols either, for backwards-compat with previous Confstruct. Turns out changing this effects things like merges into ordinary hashes that client code might be doing, and makes a backward compat nightmare.
48
49
50
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 48
def convert_key(key)
key.to_sym
end
|
#deep_copy ⇒ Object
Also known as:
inheritable_copy
69
70
71
72
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 69
def deep_copy
self.dup
end
|
#deferred!(&block) ⇒ Object
89
90
91
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 89
def deferred! &block
Confstruct.deferred(&block)
end
|
#has?(key_path) ⇒ Boolean
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 93
def has? key_path
val = self
keys = key_path.split(/\./)
keys.each do |key|
return false if val.nil?
if val.respond_to?(:has_key?) and val.has_key?(key.to_sym)
val = val[key.to_sym]
else
return false
end
end
return true
end
|
#i18n!(key = nil, &block) ⇒ Object
107
108
109
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 107
def i18n! key=nil, &block
Confstruct.i18n(key,&block)
end
|
#inspect ⇒ Object
We need an #inspect that does not evlauate Deferreds. We use #fetch instead of #[], since fetch does not evaluate Deferreds. Otherwise copied from hashie’s pretty_inspect
59
60
61
62
63
64
65
66
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 59
def inspect
ret = "#<#{self.class}"
keys.sort_by(&:to_s).each do |key|
ret << " #{key}=#{self.fetch(key).inspect}"
end
ret << '>'
ret
end
|
#lookup!(key_path, fallback = nil) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 112
def lookup! key_path, fallback = nil
val = self
keys = key_path.split(/\./)
keys.each do |key|
return fallback if val.nil?
if val.respond_to?(:has_key?) and val.has_key?(key.to_sym)
val = val[key.to_sym]
else
return fallback
end
end
return val
end
|
#values ⇒ Object
values override needed to ensure Deferreds get evaluated
85
86
87
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 85
def values
keys.collect { |k| self[k] }
end
|