Class: DataBindings::Bound::BoundObject

Inherits:
IndifferentHash show all
Includes:
DataBindings::Bound
Defined in:
lib/data_bindings/bound.rb

Constant Summary

Constants included from DataBindings::Bound

NoBindingName, ValidationError

Instance Attribute Summary

Attributes included from DataBindings::Bound

#errors, #generator, #name, #source

Instance Method Summary collapse

Methods included from DataBindings::Bound

#calculate_validness, #cast_element, #pre_convert, #valid!, #valid?

Methods included from Hashie::Extensions::IndifferentAccess

#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.

Raises:



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_sourceObject



232
233
234
# File 'lib/data_bindings/bound.rb', line 232

def copy_source
  replace @source
end

#enforce_strictnessObject



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_hashObject



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_nativeObject



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_hashObject



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