Module: DataBindings::Unbound

Overview

Module that handles unbound objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bindingObject (readonly)

Returns the value of attribute binding.



5
6
7
# File 'lib/data_bindings/unbound.rb', line 5

def binding
  @binding
end

#binding_nameObject (readonly)

Returns the value of attribute binding_name.



5
6
7
# File 'lib/data_bindings/unbound.rb', line 5

def binding_name
  @binding_name
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/data_bindings/unbound.rb', line 46

def array?
  type == :array
end

#bind(name = nil, opts = nil, &blk) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/data_bindings/unbound.rb', line 15

def bind(name = nil, opts = nil, &blk)
  if name.is_a?(Unbound)
    update_binding(name.binding_name, &name.binding)
  else
    name, opts = nil, name if name.is_a?(Hash) && opts.nil?
    raise if name.nil? && blk.nil?
    update_binding(name, &blk)
  end
  binding_class.new(@generator, @array, self, name, opts, &@binding)
end

#bind!(name = nil, &blk) ⇒ Object



7
8
9
# File 'lib/data_bindings/unbound.rb', line 7

def bind!(name = nil, &blk)
  bind(name, &blk).valid!
end

#bind_array(type = nil, opts = nil, &blk) ⇒ Object



26
27
28
29
30
# File 'lib/data_bindings/unbound.rb', line 26

def bind_array(type = nil, opts = nil, &blk)
  type, opts = nil, type if type.is_a?(Hash) && opts.nil?
  update_binding([], &blk)
  binding_class.new(@generator, @array, self, nil, opts, &blk)
end

#binding_classObject



69
70
71
72
73
74
# File 'lib/data_bindings/unbound.rb', line 69

def binding_class
  case type
  when :array then @generator.binding_class(Bound::BoundArray)
  when :hash  then @generator.binding_class(Bound::BoundObject)
  end
end

#convert_targetObject



11
12
13
# File 'lib/data_bindings/unbound.rb', line 11

def convert_target
  bind { copy_source }
end

#hash?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/data_bindings/unbound.rb', line 42

def hash?
  type == :hash
end

#to_nativeObject



50
51
52
53
54
# File 'lib/data_bindings/unbound.rb', line 50

def to_native
  array? ?
    map{ |m| m.respond_to?(:to_native) ? m.to_native : m } :
    OpenStruct.new(inject({}) {|h, (k, v)| v = @generator.from_ruby(v); h[k] = (v.respond_to?(:to_native) ? v.to_native : v); h})
end

#typeObject



32
33
34
35
36
37
38
39
40
# File 'lib/data_bindings/unbound.rb', line 32

def type
  if self.is_a?(Array)
    :array
  elsif self.is_a?(Hash)
    :hash
  else
    raise
  end
end

#update_binding(name, &blk) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/data_bindings/unbound.rb', line 56

def update_binding(name, &blk)
  if name.is_a?(Array)
    n = name.at(0)
    @array = true
    blk = proc { all_elements n }
    name = nil
  else
    @array = false
  end
  @binding = @generator.get_type(name) || blk || raise(UnknownBindingError, "Unknown binding #{name.inspect}")
  @name = name
end