class Pakyow::Presenter::Binder

Decorates an object being bound to the view.

Attributes

object[R]

The object being bound.

Public Class Methods

new(object, app:) click to toggle source
# File lib/pakyow/presenter/binder.rb, line 16
def initialize(object, app:)
  @object, @app = object, app
  @parts = {}
  @binding = false
  @memoized = {}
end

Public Instance Methods

[](key) click to toggle source

Returns the underlying object value for a key.

# File lib/pakyow/presenter/binder.rb, line 65
def [](key)
  @object[key]
end
__content(key, view) click to toggle source

Returns only the content value for a key.

@api private

# 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) click to toggle source

Returns the value for a key (including parts).

@api private

# 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!() click to toggle source

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.

@api private

# File lib/pakyow/presenter/binder.rb, line 102
def binding!
  @binding = true
end
include?(key) click to toggle source

Returns true if the binder might return a value for key.

# 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) click to toggle source

Defines a binding part, which binds to an aspect of the view.

@example

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

    part :content do
      value.to_s.reverse
    end
  end
end
# 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) click to toggle source

Returns true if the a value is present for key.

# File lib/pakyow/presenter/binder.rb, line 94
def present?(key)
  !__value(key).nil?
end

Private Instance Methods

parts?(name) click to toggle source
# File lib/pakyow/presenter/binder.rb, line 108
def parts?(name)
  @parts.include?(name.to_sym)
end
parts_for(name) click to toggle source
# File lib/pakyow/presenter/binder.rb, line 112
def parts_for(name)
  @parts[name] ||= BindingParts.new
end