Class: DataBindings::Bound::BoundObject
Constant Summary
NoBindingName, ValidationError
Instance Attribute Summary
#errors, #generator, #name, #source
Instance Method Summary
collapse
-
#all_properties(type = nil, opts = nil) ⇒ Object
-
#copy_source ⇒ Object
-
#enforce_strictness ⇒ Object
-
#initialize(generator, array_expected, source, name, opts, &blk) ⇒ BoundObject
constructor
A new instance of BoundObject.
-
#optional(name, type = nil, opts = nil, &blk) ⇒ Object
-
#property(name, type = nil, opts = nil, &blk) ⇒ Object
-
#required(name, type = nil, opts = nil, &blk) ⇒ Object
-
#to_hash ⇒ Object
-
#to_native ⇒ Object
-
#to_nonindifferent_hash ⇒ Object
#calculate_validness, #cast_element, #pre_convert, #valid!, #valid?
#convert!, #convert_key, #convert_value, included, #indifferent_access?, #indifferent_default, #indifferent_delete, #indifferent_fetch, #indifferent_key?, #indifferent_update, #indifferent_values_at, #indifferent_writer, inject, inject!
Constructor Details
#initialize(generator, array_expected, source, name, opts, &blk) ⇒ BoundObject
Returns a new instance of BoundObject.
168
169
170
171
|
# File 'lib/data_bindings/bound.rb', line 168
def initialize(generator, array_expected, source, name, opts, &blk)
raise BindingMismatch if array_expected
init_bound(generator, source, name, opts, blk)
end
|
Instance Method Details
#all_properties(type = nil, opts = nil) ⇒ Object
221
222
223
224
225
226
|
# File 'lib/data_bindings/bound.rb', line 221
def all_properties(type = nil, opts = nil)
type, opts = nil, type if type.is_a?(Hash)
@from.keys.each do |key|
property key, type, opts
end
end
|
#copy_source ⇒ Object
232
233
234
|
# File 'lib/data_bindings/bound.rb', line 232
def copy_source
replace @source
end
|
#enforce_strictness ⇒ Object
228
229
230
|
# File 'lib/data_bindings/bound.rb', line 228
def enforce_strictness
@errors.base = "hasn't been fully matched" unless size == @from.size
end
|
#optional(name, type = nil, opts = nil, &blk) ⇒ Object
214
215
216
217
218
219
|
# File 'lib/data_bindings/bound.rb', line 214
def optional(name, type = nil, opts = nil, &blk)
type, opts = nil, type if type.is_a?(Hash)
opts ||= {}
opts[:allow_nil] = true
property(name, type, opts, &blk)
end
|
#property(name, type = nil, opts = nil, &blk) ⇒ Object
202
203
204
205
|
# File 'lib/data_bindings/bound.rb', line 202
def property(name, type = nil, opts = nil, &blk)
type, opts = nil, type if type.is_a?(Hash)
self[name] = cast_element(name, @from, type, opts, &blk)
end
|
#required(name, type = nil, opts = nil, &blk) ⇒ Object
207
208
209
210
211
212
|
# File 'lib/data_bindings/bound.rb', line 207
def required(name, type = nil, opts = nil, &blk)
type, opts = nil, type if type.is_a?(Hash)
opts ||= {}
opts[:allow_nil] = false
property(name, type, opts, &blk)
end
|
#to_hash ⇒ Object
173
174
175
176
177
178
179
|
# File 'lib/data_bindings/bound.rb', line 173
def to_hash
keys.inject(DataBindings::IndifferentHash.new) { |h, k|
val = self[k]
h[k] = dump_val(val)
h
}
end
|
#to_native ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/data_bindings/bound.rb', line 189
def to_native
valid!
data = inject(IndifferentHash.new) { |h, (k, v)|
h[k] = v.respond_to?(:to_native) ? v.to_native : v
h
}
if constructor = generator.native_constructors[name]
o = constructor[data.to_hash]
else
OpenStruct.new(data)
end
end
|
#to_nonindifferent_hash ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/data_bindings/bound.rb', line 181
def to_nonindifferent_hash
keys.inject({}) { |h, k|
val = self[k]
h[k.to_s] = dump_val(val)
h
}
end
|