Class: Pakyow::Presenter::Binder
- Inherits:
-
Object
- Object
- Pakyow::Presenter::Binder
- Extended by:
- Support::Makeable
- Defined in:
- lib/pakyow/presenter/binder.rb
Overview
Decorates an object being bound to the view.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
The object being bound.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the underlying object value for a key.
-
#__content(key, view) ⇒ Object
private
Returns only the content value for a key.
-
#__value(key) ⇒ Object
private
Returns the value for a key (including parts).
-
#binding! ⇒ Object
private
Flips a switch, telling the binder that we now only care about content, not other parts.
-
#include?(key) ⇒ Boolean
Returns
true
if the binder might return a value forkey
. -
#initialize(object, app:) ⇒ Binder
constructor
A new instance of Binder.
-
#part(name, &block) ⇒ Object
Defines a binding part, which binds to an aspect of the view.
-
#present?(key) ⇒ Boolean
Returns
true
if the a value is present forkey
.
Constructor Details
#initialize(object, app:) ⇒ Binder
Returns a new instance of Binder.
16 17 18 19 20 21 |
# File 'lib/pakyow/presenter/binder.rb', line 16 def initialize(object, app:) @object, @app = object, app @parts = {} @binding = false @memoized = {} end |
Instance Attribute Details
#object ⇒ Object (readonly)
The object being bound.
14 15 16 |
# File 'lib/pakyow/presenter/binder.rb', line 14 def object @object end |
Instance Method Details
#[](key) ⇒ Object
Returns the underlying object value for a key.
65 66 67 |
# File 'lib/pakyow/presenter/binder.rb', line 65 def [](key) @object[key] end |
#__content(key, view) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns only the content value for a key.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pakyow/presenter/binder.rb', line 72 def __content(key, view) return_value = __value(key) if return_value.is_a?(BindingParts) if return_value.content? return_value.content(view) else @object[key] end else return_value end end |
#__value(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the value for a key (including parts).
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/pakyow/presenter/binder.rb', line 45 def __value(key) if @memoized.include?(key) @memoized[key] else @memoized[key] = if respond_to?(key) value = public_send(key) if parts?(key) parts_for(key) else value end else @object[key] end end end |
#binding! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Flips a switch, telling the binder that we now only care about content, not other parts. This is so that we can transform based on parts, but bind only based on content.
102 103 104 |
# File 'lib/pakyow/presenter/binder.rb', line 102 def binding! @binding = true end |
#include?(key) ⇒ Boolean
Returns true
if the binder might return a value for key
.
87 88 89 90 |
# File 'lib/pakyow/presenter/binder.rb', line 87 def include?(key) return false if @binding && !@object.include?(key) && (parts?(key) && !parts_for(key).content?) respond_to?(key) || @object.include?(key) end |
#part(name, &block) ⇒ Object
Defines a binding part, which binds to an aspect of the view.
38 39 40 |
# File 'lib/pakyow/presenter/binder.rb', line 38 def part(name, &block) parts_for(caller_locations(1, 1)[0].label.to_sym).define_part(name, block) end |
#present?(key) ⇒ Boolean
Returns true
if the a value is present for key
.
94 95 96 |
# File 'lib/pakyow/presenter/binder.rb', line 94 def present?(key) !__value(key).nil? end |