Module: ActiveModel::AttributeFilters::Common::Split::ClassMethods
- Defined in:
- lib/attribute-filters/common_filters/split.rb
Overview
This submodule contains class methods needed to describe attribute splitting.
Instance Method Summary collapse
-
#split_attributes(atr_name, parameters = nil) ⇒ nil
(also: #split_attribute, #splits_attribute, #splits_attributes)
This method parametrizes splitting operation for an attribute of the given name.
Instance Method Details
#split_attributes(atr_name, parameters = nil) ⇒ nil Also known as: split_attribute, splits_attribute, splits_attributes
This method parametrizes splitting operation for an attribute of the given name. It uses attribute set annotations to register parameters used when splitting.
If the separator is not specified and the source attribute is a kind of String
then the default separator is applied, which is a whitespace character. You can change that by explicitly setting the separator to nil
. In such case the split will occuch for each character. If there are more resulting parts then destination attributes then the redundant elements are ignored. If there is a limit given and there are more array elements than attributes then the filter behaves like puts leaves the redundant (unsplittable) and puts it into the last destination attribute.
If the source attribute is an array then the filter will put each element of that array into each destination attribute. If there are more array elements than attributes then the reduntant elements are ignored.
If the destination attributes are not given then the split filter will generate an array and replace currently processed attribute with an array.
The pattern parameter (:pattern
when using split_attributes
class method or :split_pattern
when directly annotating attribute in a set) should be a string. If you would like to separate each character you have to set it to nil
.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/attribute-filters/common_filters/split.rb', line 114 def split_attributes(atr_name, parameters = nil) if atr_name.is_a?(Hash) atr_name.each_pair { |k, v| split_attribute(k, v) } return nil end setup_attributes_that :should_be_splitted, { atr_name => parameters }, { :split_pattern => [ :with, :pattern, :split_pattern ], :split_into => [ :into, :to, :destination, :split_into ], :split_limit => [ :limit, :split_limit ], :split_flatten => [ :flatten, :split_flatten ] }, :split_into # write some defaults limit, into = af_attribute_set(:should_be_splitted).get_annotations(atr_name, :split_limit, :split_into) annotate_attribute_set(:should_be_splitted, atr_name, :split_limit, limit.to_i) unless limit.blank? if into.blank? annotate_attribute_set(:should_be_splitted, atr_name,:split_into, nil) elsif !into.is_a?(Array) annotate_attributes_that(:should_be_splitted, :split_into, into.respond_to?(:to_a) ? into.to_a : [ into ]) end nil end |