Class: Representable::Binding
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Representable::Binding
- Includes:
- Prepare
- 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
Defined Under Namespace
Modules: Object, Prepare Classes: FragmentNotFound
Instance Attribute Summary collapse
-
#represented ⇒ Object
readonly
TODO: make private/remove.
-
#user_options ⇒ Object
readonly
TODO: make private/remove.
Class Method Summary collapse
Instance Method Summary collapse
-
#compile_fragment(doc) ⇒ Object
Retrieve value and write fragment to the doc.
- #get ⇒ Object
-
#initialize(definition, represented, user_options = {}, exec_context = represented) ⇒ Binding
constructor
TODO: remove default arg for user options.
- #read_fragment(doc) {|value| ... } ⇒ Object
- #read_fragment_for(doc) ⇒ Object
- #set(value) ⇒ Object
-
#uncompile_fragment(doc) ⇒ Object
Parse value from doc and update the model property.
- #write_fragment(doc, value) ⇒ Object
- #write_fragment_for(value, doc) ⇒ Object
Methods included from Prepare
Constructor Details
#initialize(definition, represented, user_options = {}, exec_context = represented) ⇒ Binding
TODO: remove default arg for user options. # DISCUSS: make exec_context an options hash?
17 18 19 20 21 22 |
# File 'lib/representable/binding.rb', line 17 def initialize(definition, represented, ={}, exec_context=represented) # TODO: remove default arg for user options. # DISCUSS: make exec_context an options hash? super(definition) @represented = represented = @exec_context = exec_context end |
Instance Attribute Details
#represented ⇒ Object (readonly)
TODO: make private/remove.
24 25 26 |
# File 'lib/representable/binding.rb', line 24 def represented @represented end |
#user_options ⇒ Object (readonly)
TODO: make private/remove.
24 25 26 |
# File 'lib/representable/binding.rb', line 24 def end |
Class Method Details
.build(definition, *args) ⇒ Object
11 12 13 14 15 |
# File 'lib/representable/binding.rb', line 11 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.
27 28 29 30 31 |
# File 'lib/representable/binding.rb', line 27 def compile_fragment(doc) represented_exec_for(:writer, doc) do write_fragment(doc, get) end end |
#get ⇒ Object
68 69 70 71 72 |
# File 'lib/representable/binding.rb', line 68 def get represented_exec_for(:getter) do exec_context.send(getter) end end |
#read_fragment(doc) {|value| ... } ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/representable/binding.rb', line 53 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
64 65 66 |
# File 'lib/representable/binding.rb', line 64 def read_fragment_for(doc) read(doc) end |
#set(value) ⇒ Object
74 75 76 77 78 |
# File 'lib/representable/binding.rb', line 74 def set(value) represented_exec_for(:setter, value) do exec_context.send(setter, value) end end |
#uncompile_fragment(doc) ⇒ Object
Parse value from doc and update the model property.
34 35 36 37 38 39 40 |
# File 'lib/representable/binding.rb', line 34 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
42 43 44 45 46 |
# File 'lib/representable/binding.rb', line 42 def write_fragment(doc, value) value = default_for(value) write_fragment_for(value, doc) end |
#write_fragment_for(value, doc) ⇒ Object
48 49 50 51 |
# File 'lib/representable/binding.rb', line 48 def write_fragment_for(value, doc) return if skipable_nil_value?(value) write(doc, value) end |