Class: Representable::Binding

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/representable/binding.rb

Overview

The Binding wraps the Definition instance for this property and provides methods to read/write fragments.

Direct Known Subclasses

Hash::PropertyBinding, XML::PropertyBinding

Defined Under Namespace

Modules: Object, Prepare Classes: FragmentNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, represented, user_options = {}, lambda_context = represented) ⇒ Binding

TODO: remove default arg for user options. # DISCUSS: make lambda_context an options hash?



19
20
21
22
23
24
# File 'lib/representable/binding.rb', line 19

def initialize(definition, represented, user_options={}, lambda_context=represented)  # TODO: remove default arg for user options. # DISCUSS: make lambda_context an options hash?
  super(definition)
  @represented    = represented
  @user_options   = user_options
  @lambda_context = lambda_context
end

Instance Attribute Details

#lambda_contextObject (readonly)

TODO: make private/remove.



26
27
28
# File 'lib/representable/binding.rb', line 26

def lambda_context
  @lambda_context
end

#representedObject (readonly)

TODO: make private/remove.



26
27
28
# File 'lib/representable/binding.rb', line 26

def represented
  @represented
end

#user_optionsObject (readonly)

TODO: make private/remove.



26
27
28
# File 'lib/representable/binding.rb', line 26

def user_options
  @user_options
end

Class Method Details

.build(definition, *args) ⇒ Object



9
10
11
12
13
# File 'lib/representable/binding.rb', line 9

def self.build(definition, *args)
  # DISCUSS: move #create_binding to this class?
  return definition.create_binding(*args) if definition.binding
  build_for(definition, *args)
end

Instance Method Details

#compile_fragment(doc) ⇒ Object

Retrieve value and write fragment to the doc.



38
39
40
41
42
# File 'lib/representable/binding.rb', line 38

def compile_fragment(doc)
  represented_exec_for(:writer, doc) do
    write_fragment(doc, get)
  end
end

#definitionObject

TODO: remove in 1.4.



15
16
17
# File 'lib/representable/binding.rb', line 15

def definition  # TODO: remove in 1.4.
  raise "Binding#definition is no longer supported as all Definition methods are now delegated automatically."
end

#deserialize(fragment) ⇒ Object



33
34
35
# File 'lib/representable/binding.rb', line 33

def deserialize(fragment)
  fragment
end

#getObject



79
80
81
82
83
# File 'lib/representable/binding.rb', line 79

def get
  represented_exec_for(:getter) do
    represented.send(getter)
  end
end

#read_fragment(doc) {|value| ... } ⇒ Object

Yields:

  • (value)


64
65
66
67
68
69
70
71
72
73
# File 'lib/representable/binding.rb', line 64

def read_fragment(doc)
  value = read_fragment_for(doc)

  if value == FragmentNotFound
    return unless has_default?
    value = default
  end

  yield value
end

#read_fragment_for(doc) ⇒ Object



75
76
77
# File 'lib/representable/binding.rb', line 75

def read_fragment_for(doc)
  read(doc)
end

#serialize(value) ⇒ Object

Main entry point for rendering/parsing a property object.



29
30
31
# File 'lib/representable/binding.rb', line 29

def serialize(value)
  value
end

#set(value) ⇒ Object



85
86
87
88
89
# File 'lib/representable/binding.rb', line 85

def set(value)
  represented_exec_for(:setter, value) do
    represented.send(setter, value)
  end
end

#uncompile_fragment(doc) ⇒ Object

Parse value from doc and update the model property.



45
46
47
48
49
50
51
# File 'lib/representable/binding.rb', line 45

def uncompile_fragment(doc)
  represented_exec_for(:reader, doc) do
    read_fragment(doc) do |value|
      set(value)
    end
  end
end

#write_fragment(doc, value) ⇒ Object



53
54
55
56
57
# File 'lib/representable/binding.rb', line 53

def write_fragment(doc, value)
  value = default_for(value)

  write_fragment_for(value, doc)
end

#write_fragment_for(value, doc) ⇒ Object



59
60
61
62
# File 'lib/representable/binding.rb', line 59

def write_fragment_for(value, doc)
  return if skipable_nil_value?(value)
  write(doc, value)
end