Class: Komagire::KeyList::Converter
- Inherits:
-
Object
- Object
- Komagire::KeyList::Converter
- Defined in:
- lib/komagire/key_list.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#content_class_name ⇒ Object
readonly
Returns the value of attribute content_class_name.
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
Instance Method Summary collapse
- #convert(value) ⇒ Object
- #convert_to_cskeys(value) ⇒ Object
- #convert_to_cskeys_from_array(values) ⇒ Object
-
#initialize(content_class_name, attribute, delimiter) ⇒ Converter
constructor
A new instance of Converter.
Constructor Details
#initialize(content_class_name, attribute, delimiter) ⇒ Converter
Returns a new instance of Converter.
69 70 71 72 73 |
# File 'lib/komagire/key_list.rb', line 69 def initialize(content_class_name, attribute, delimiter) @content_class_name = content_class_name @attribute = attribute @delimiter = delimiter end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
67 68 69 |
# File 'lib/komagire/key_list.rb', line 67 def attribute @attribute end |
#content_class_name ⇒ Object (readonly)
Returns the value of attribute content_class_name.
67 68 69 |
# File 'lib/komagire/key_list.rb', line 67 def content_class_name @content_class_name end |
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
67 68 69 |
# File 'lib/komagire/key_list.rb', line 67 def delimiter @delimiter end |
Instance Method Details
#convert(value) ⇒ Object
75 76 77 |
# File 'lib/komagire/key_list.rb', line 75 def convert(value) convert_to_cskeys(value) || '' end |
#convert_to_cskeys(value) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/komagire/key_list.rb', line 79 def convert_to_cskeys(value) case value when String value when Komagire::KeyList if value.content_class_name == content_class_name && value.attribute == attribute return value.cskeys end raise DifferentContentClass else values = if value.is_a?(Array) then value elsif value.respond_to?(:to_a) then value.to_a elsif value.respond_to?(:to_ary) then value.to_ary else raise ArgumentError end convert_to_cskeys_from_array(values.compact) end end |
#convert_to_cskeys_from_array(values) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/komagire/key_list.rb', line 100 def convert_to_cskeys_from_array(values) if values.all? { |v| v.is_a?(String) } values.join(delimiter) elsif values.all? { |v| v.is_a?(content_class_name.constantize) } values.map(&attribute).join(delimiter) else raise ArgumentError end end |