Class: Representable::Binding
- Inherits:
-
Object
- Object
- Representable::Binding
- 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 Classes: FragmentNotFound, Options
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
-
#as ⇒ Object
DISCUSS: private?.
-
#compile_fragment(doc) ⇒ Object
Retrieve value and write fragment to the doc.
- #get ⇒ Object
-
#initialize(definition, represented, decorator, user_options = {}) ⇒ Binding
constructor
TODO: remove default arg for user options.
- #read_fragment(doc) {|value| ... } ⇒ Object
- #read_fragment_for(doc) ⇒ Object
-
#representer_module_for(object, *args) ⇒ Object
DISCUSS: do we really need that?.
- #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
Constructor Details
#initialize(definition, represented, decorator, user_options = {}) ⇒ Binding
TODO: remove default arg for user options.
16 17 18 19 20 21 22 23 |
# File 'lib/representable/binding.rb', line 16 def initialize(definition, represented, decorator, ={}) # TODO: remove default arg for user options. @definition = definition @represented = represented @decorator = decorator @user_options = setup_exec_context! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object (private)
Apparently, SimpleDelegator is super slow due to a regex, so we do it ourselves, right, Jimmy?
93 94 95 |
# File 'lib/representable/binding.rb', line 93 def method_missing(*args, &block) @definition.send(*args, &block) end |
Instance Attribute Details
#represented ⇒ Object (readonly)
TODO: make private/remove.
25 26 27 |
# File 'lib/representable/binding.rb', line 25 def represented @represented end |
#user_options ⇒ Object (readonly)
TODO: make private/remove.
25 26 27 |
# File 'lib/representable/binding.rb', line 25 def @user_options end |
Class Method Details
.build(definition, *args) ⇒ Object
10 11 12 13 14 |
# File 'lib/representable/binding.rb', line 10 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
#as ⇒ Object
DISCUSS: private?
27 28 29 |
# File 'lib/representable/binding.rb', line 27 def as # DISCUSS: private? evaluate_option(:as) end |
#compile_fragment(doc) ⇒ Object
Retrieve value and write fragment to the doc.
32 33 34 35 36 |
# File 'lib/representable/binding.rb', line 32 def compile_fragment(doc) evaluate_option(:writer, doc) do write_fragment(doc, get) end end |
#get ⇒ Object
73 74 75 76 77 |
# File 'lib/representable/binding.rb', line 73 def get evaluate_option(:getter) do exec_context.send(getter) end end |
#read_fragment(doc) {|value| ... } ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/representable/binding.rb', line 58 def read_fragment(doc) value = read_fragment_for(doc) if value == FragmentNotFound return unless has_default? value = self[:default] end yield value end |
#read_fragment_for(doc) ⇒ Object
69 70 71 |
# File 'lib/representable/binding.rb', line 69 def read_fragment_for(doc) read(doc) end |
#representer_module_for(object, *args) ⇒ Object
DISCUSS: do we really need that?
86 87 88 |
# File 'lib/representable/binding.rb', line 86 def representer_module_for(object, *args) evaluate_option(:extend, object) # TODO: pass args? do we actually have args at the time this is called (compile-time)? end |
#set(value) ⇒ Object
79 80 81 82 83 |
# File 'lib/representable/binding.rb', line 79 def set(value) evaluate_option(:setter, value) do exec_context.send(setter, value) end end |
#uncompile_fragment(doc) ⇒ Object
Parse value from doc and update the model property.
39 40 41 42 43 44 45 |
# File 'lib/representable/binding.rb', line 39 def uncompile_fragment(doc) evaluate_option(:reader, doc) do read_fragment(doc) do |value| set(value) end end end |
#write_fragment(doc, value) ⇒ Object
47 48 49 50 51 |
# File 'lib/representable/binding.rb', line 47 def write_fragment(doc, value) value = default_for(value) write_fragment_for(value, doc) end |
#write_fragment_for(value, doc) ⇒ Object
53 54 55 56 |
# File 'lib/representable/binding.rb', line 53 def write_fragment_for(value, doc) return if skipable_empty_value?(value) write(doc, value) end |