Module: YiffSpace::Concerns::AttributeMethods::ClassMethods
- Defined in:
- lib/yiffspace/concerns/attribute_methods.rb
Instance Method Summary collapse
-
#array_attribute(name, parse: /[^[:space:]]+/, join_character: " ", cast: :itself) ⇒ Object
Defines ‘<attribute>_string`, `<attribute>_string=`, and `<attribute>=` methods for converting an array attribute to or from a string.
Instance Method Details
#array_attribute(name, parse: /[^[:space:]]+/, join_character: " ", cast: :itself) ⇒ Object
Defines ‘<attribute>_string`, `<attribute>_string=`, and `<attribute>=` methods for converting an array attribute to or from a string.
The ‘<attribute>=` setter parses strings into an array using the `parse` regex. The resulting strings can be converted to another type with the `cast` option.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/yiffspace/concerns/attribute_methods.rb', line 15 def array_attribute(name, parse: /[^[:space:]]+/, join_character: " ", cast: :itself) define_method("#{name}_string") do send(name).join(join_character) end define_method("#{name}_string=") do |value| raise(ArgumentError, "#{name} must be a String") unless value.respond_to?(:to_str) send("#{name}=", value) end define_method("#{name}=") do |value| if value.respond_to?(:to_str) super(value.to_str.scan(parse).flatten.map(&cast)) elsif value.respond_to?(:to_a) super(value.to_a) else raise(ArgumentError, "#{name} must be a String or an Array") end end end |