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

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.

Examples:

class User < ActiveRecord::Base
  include ActiveModel::AttributeFilters::Common::Split

  attr_virtual      :real_name
  attr_accessible   :real_name
  split_attributes  :real_name, :limit => 2, :into => [ :first_name, :last_name ], :pattern => ' '
  before_validation :split_attributes
end
class User < ActiveRecord::Base
  include ActiveModel::AttributeFilters::Common::Split

  attr_virtual      :real_name
  attr_accessible   :real_name
  attributes_that   :should_be_splitted =>  { :real_name =>
                                                { :limit => 2,
                                                  :into => [ :first_name, :last_name ],
                                                  :pattern => ' ' } }
  before_validation :split_attributes
end

Parameters:

  • atr_name (String, Symbol)

    attribute name

  • parameters (Hash) (defaults to: nil)

    parameters hash

Options Hash (parameters):

  • :pattern (String)

    pattern passed to split method call

  • :split_pattern (String)

    pattern passed to split method call (alternative name)

  • :with (String)

    pattern passed to split method call (alternative name)

  • :limit (Fixnum)

    limit parameter passed to split method call

  • :split_limit (Fixnum)

    limit parameter passed to split method call (alternative name)

  • :into (String, Symbol, Array<String,Symbol>)

    names of attributes that results should be written to

  • :to (String, Symbol, Array<String,Symbol>)

    names of attributes that results should be written to (alternative name)

  • :split_into (String, Symbol, Array<String,Symbol>)

    names of attributes that results should be written to (alternative name)

  • :destination (String, Symbol, Array<String,Symbol>)

    names of attributes that results should be written to (alternative name)

  • :flatten (Boolean)

    flag that causes the resulting, intermediate array to be flattened

  • :split_flatten (Boolean)

    flag that causes the resulting, intermediate array to be flattened (alternative name)

Returns:

  • (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