Class: Pakyow::Presenter::Attributes

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Support::SafeStringHelpers
Defined in:
lib/pakyow/presenter/attributes.rb,
lib/pakyow/presenter/attributes/set.rb,
lib/pakyow/presenter/attributes/hash.rb,
lib/pakyow/presenter/attributes/string.rb,
lib/pakyow/presenter/attributes/boolean.rb,
lib/pakyow/presenter/attributes/attribute.rb

Defined Under Namespace

Classes: Attribute, Boolean, Hash, Set, String

Constant Summary collapse

ATTRIBUTE_TYPE_HASH =

Object for hash attributes

Attributes::Hash
ATTRIBUTE_TYPE_SET =

Object for set attributes

Attributes::Set
ATTRIBUTE_TYPE_BOOLEAN =

Object for boolean attributes

Attributes::Boolean
ATTRIBUTE_TYPE_DEFAULT =

Default attribute object

Attributes::String
ATTRIBUTE_TYPES =

Maps non-default attributes to their type

{
  class: ATTRIBUTE_TYPE_SET,
  style: ATTRIBUTE_TYPE_HASH,
  selected: ATTRIBUTE_TYPE_BOOLEAN,
  checked: ATTRIBUTE_TYPE_BOOLEAN,
  disabled: ATTRIBUTE_TYPE_BOOLEAN,
  readonly: ATTRIBUTE_TYPE_BOOLEAN,
  multiple: ATTRIBUTE_TYPE_BOOLEAN,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Attributes

Wraps a hash of view attributes

Parameters:



94
95
96
97
98
99
100
# File 'lib/pakyow/presenter/attributes.rb', line 94

def initialize(attributes)
  attributes.wrap do |value, name|
    Attributes.typed_value_for_attribute_with_name(value, name)
  end

  @attributes = attributes
end

Class Method Details

.default_value_for_attribute(attribute) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pakyow/presenter/attributes.rb', line 30

def default_value_for_attribute(attribute)
  type = type_of_attribute(attribute.to_sym)
  if type == ATTRIBUTE_TYPE_SET
    ::Set.new
  elsif type == ATTRIBUTE_TYPE_HASH
    ::Hash.new
  elsif type == ATTRIBUTE_TYPE_BOOLEAN
    false
  else
    ::String.new
  end
end

.type_of_attribute(attribute) ⇒ Object



26
27
28
# File 'lib/pakyow/presenter/attributes.rb', line 26

def type_of_attribute(attribute)
  ATTRIBUTE_TYPES[attribute.to_sym] || ATTRIBUTE_TYPE_DEFAULT
end

.typed_value_for_attribute_with_name(value, name) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/pakyow/presenter/attributes.rb', line 16

def typed_value_for_attribute_with_name(value, name)
  type = type_of_attribute(name.to_sym)

  if value.is_a?(type)
    value
  else
    type.parse(value)
  end
end

Instance Method Details

#[](attribute) ⇒ Object

Returns value of key from @attributes.



88
# File 'lib/pakyow/presenter/attributes.rb', line 88

def_delegators :@attributes, :keys, :delete, :each

#[]=(attribute, value) ⇒ Object

Returns sets value for key on @attributes.



88
# File 'lib/pakyow/presenter/attributes.rb', line 88

def_delegators :@attributes, :keys, :delete, :each

#deleteObject

Deletes key by name from @attributes.



88
# File 'lib/pakyow/presenter/attributes.rb', line 88

def_delegators :@attributes, :keys, :delete, :each

#has?(attribute) ⇒ Boolean

Returns:



129
130
131
# File 'lib/pakyow/presenter/attributes.rb', line 129

def has?(attribute)
  @attributes.key?(attribute.to_sym)
end

#keysObject

Returns keys from @attributes.



88
# File 'lib/pakyow/presenter/attributes.rb', line 88

def_delegators :@attributes, :keys, :delete, :each