Module: ActiveModel::AttributeFilters::Common::Split
- Included in:
- ActiveModel::AttributeFilters::Common
- Defined in:
- lib/attribute-filters/common_filters/split.rb
Overview
Splits attributes.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#split_attributes ⇒ Object
Splits attributes and writes the results into other attributes.
Methods included from FilteringRegistration
Instance Method Details
#split_attributes ⇒ Object
Splits attributes and writes the results into other attributes.
The attrubutes to be splitted are taken from the attribute set called should_be_splitted
. This method is safe to be used with multibyte strings (containing diacritics).
The pattern used to split a string and the optional limit argument should be set using the model’s class method split_attribute
or directly with annotations.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/attribute-filters/common_filters/split.rb', line 27 def split_attributes for_each_attr_from_set(:should_be_splitted) do |atr_val, atr_name, set_obj| pattern, limit, flatten, into = set_obj.annotation(atr_name, :split_pattern, :split_limit, :split_flatten, :split_into) if limit.nil? r = AFHelpers.each_element(atr_val, String) do |v| v.mb_chars.split(pattern) end else r = AFHelpers.each_element(atr_val, String) do |v| v.mb_chars.split(pattern, limit) end end r = [ r ] unless r.is_a?(Array) r.flatten! if flatten # writing collected slices if into.blank? public_send("#{atr_name}=", r) else into.each_with_index { |dst_atr, i| public_send("#{dst_atr}=", r[i]) } end end end |