class Pakyow::Presenter::Attributes::Hash

Wraps the value for a hash-type view attribute (e.g. style).

Behaves just like a normal Hash.

Constants

PAIR_SEPARATOR
VALUE_SEPARATOR
WRITE_PAIR_SEPARATOR
WRITE_VALUE_SEPARATOR

Public Class Methods

parse(value) click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 72
def parse(value)
  if value.is_a?(::Hash)
    new(::Hash[value.map { |k, v| [ensure_html_safety(k), ensure_html_safety(v.to_s)]}])
  elsif value.respond_to?(:to_s)
    new(value.to_s.split(PAIR_SEPARATOR).each_with_object({}) { |style, attributes|
      key, value = style.split(VALUE_SEPARATOR)
      next unless key && value
      attributes[ensure_html_safety(key.strip)] = ensure_html_safety(value.strip)
    })
  else
    raise ArgumentError.new("expected value to be a Hash or String")
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 36
def [](key)
  @value[key.to_s]
end
[]=(key, value) click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 40
def []=(key, value)
  @value[ensure_html_safety(key)] = ensure_html_safety(value)
end
delete(key) click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 44
def delete(key)
  @value.delete(key.to_s)
end
include?(key) click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 28
def include?(key)
  @value.include?(key.to_s)
end
to_s() click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 48
def to_s
  string = ::String.new
  first = true
  @value.each do |key, value|
    unless first
      string << WRITE_PAIR_SEPARATOR
    end

    string << key
    string << WRITE_VALUE_SEPARATOR
    string << value
    first = false
  end

  unless string.empty?
    string = string + PAIR_SEPARATOR
  end

  string
end
value?(value) click to toggle source
# File lib/pakyow/presenter/attributes/hash.rb, line 32
def value?(value)
  @value.value?(value.to_s)
end