Class: Pakyow::Presenter::Attributes::Set

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

Overview

Wraps the value for a set-type view attribute (e.g. class).

Behaves just like a normal Set.

Constant Summary collapse

VALUE_SEPARATOR =
" ".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Attribute

#==, #initialize, #initialize_copy

Constructor Details

This class inherits a constructor from Pakyow::Presenter::Attributes::Attribute

Class Method Details

.parse(value) ⇒ Object

[View source]

48
49
50
51
52
53
54
55
56
# File 'lib/pakyow/presenter/attributes/set.rb', line 48

def parse(value)
  if value.is_a?(Array) || value.is_a?(::Set)
    new(::Set.new(value.map { |v| ensure_html_safety(v) }))
  elsif value.respond_to?(:to_s)
    new(::Set.new(value.to_s.split(VALUE_SEPARATOR).map { |v| ensure_html_safety(v) }))
  else
    raise ArgumentError.new("expected value to be an Array, Set, or String")
  end
end

Instance Method Details

#<<(value) ⇒ Object

[View source]

29
30
31
# File 'lib/pakyow/presenter/attributes/set.rb', line 29

def <<(value)
  @value << ensure_html_safety(value)
end

#add(value) ⇒ Object

[View source]

33
34
35
# File 'lib/pakyow/presenter/attributes/set.rb', line 33

def add(value)
  @value.add(ensure_html_safety(value))
end

#delete(value) ⇒ Object

[View source]

37
38
39
# File 'lib/pakyow/presenter/attributes/set.rb', line 37

def delete(value)
  @value.delete(value.to_s)
end

#include?(value) ⇒ Boolean

Returns:

[View source]

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

def include?(value)
  @value.include?(value.to_s)
end

#to_sObject

[View source]

41
42
43
# File 'lib/pakyow/presenter/attributes/set.rb', line 41

def to_s
  @value.to_a.join(VALUE_SEPARATOR)
end