class Pakyow::Presenter::Attributes
Constants
- ATTRIBUTE_TYPES
Maps non-default attributes to their type
- ATTRIBUTE_TYPE_BOOLEAN
Object for boolean attributes
- ATTRIBUTE_TYPE_DEFAULT
Default attribute object
- ATTRIBUTE_TYPE_HASH
Object for hash attributes
- ATTRIBUTE_TYPE_SET
Object for set attributes
Public Class Methods
default_value_for_attribute(attribute)
click to toggle source
# 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
new(attributes)
click to toggle source
Wraps a hash of view attributes
@param attributes [Hash]
# 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
type_of_attribute(attribute)
click to toggle source
# 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)
click to toggle source
# 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
Public Instance Methods
[](attribute)
click to toggle source
# File lib/pakyow/presenter/attributes.rb, line 102 def [](attribute) attribute = attribute.to_sym attribute_type = self.class.type_of_attribute(attribute) if attribute_type == ATTRIBUTE_TYPE_BOOLEAN @attributes.key?(attribute) else @attributes[attribute] ||= attribute_type.new(self.class.default_value_for_attribute(attribute)) end end
[]=(attribute, value)
click to toggle source
# File lib/pakyow/presenter/attributes.rb, line 113 def []=(attribute, value) attribute = ensure_html_safety(attribute.to_s).to_sym if value.nil? @attributes.delete(attribute) elsif self.class.type_of_attribute(attribute) == ATTRIBUTE_TYPE_BOOLEAN if value @attributes[attribute] = self.class.typed_value_for_attribute_with_name(attribute, attribute) else @attributes.delete(attribute) end else @attributes[attribute] = self.class.typed_value_for_attribute_with_name(value, attribute) end end
has?(attribute)
click to toggle source
# File lib/pakyow/presenter/attributes.rb, line 129 def has?(attribute) @attributes.key?(attribute.to_sym) end