Class: Pakyow::Presenter::Binder

Inherits:
Object
  • Object
show all
Extended by:
Support::Makeable
Defined in:
lib/pakyow/presenter/binder.rb

Overview

Decorates an object being bound to the view.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#objectObject (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.

Returns:

  • (Boolean)


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.

Examples:

binder :post do
  def title(value)
    part :class do
      [:first_classname, :second_classname]
    end

    part :content do
      value.to_s.reverse
    end
  end
end


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.

Returns:

  • (Boolean)


94
95
96
# File 'lib/pakyow/presenter/binder.rb', line 94

def present?(key)
  !__value(key).nil?
end