Module: ActiveModel::AttributeFilters::Common::Pick

Included in:
ActiveModel::AttributeFilters::Common
Defined in:
lib/attribute-filters/common_filters/pick.rb

Overview

Picks attributes from leading and trailing spaces.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from FilteringRegistration

#filtering_method

Instance Method Details

#pick_attributes

Note:

If a value of currently processed attribute is an array then any element of the array is changed. The same with hash (its values are changed).

This method returns an undefined value.

Picks attributes from leading and trailing spaces.

The attrubutes to be picked are taken from the attribute set called should_be_picked. It operates directly on attribute’s contents.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/attribute-filters/common_filters/pick.rb', line 37

def pick_attributes
  filter_attrs_from_set(:should_be_picked) do |atr_val, atr_name, set_obj|
    pick_enum, step, from, to, range, sep, jnt = set_obj.annotation(atr_name, :pick_enumerable, :pick_step,
                                                                              :pick_from, :pick_to, :pick_range,
                                                                              :pick_separator, :pick_join)
    if pick_enum
      if atr_val.is_a?(String)
        sep ||= ""
        jnt ||= sep.is_a?(String) ? sep : nil
        pick_attributes_core(atr_val.mb_chars.split(sep), from, to, range, step).join(jnt)
      elsif atr_val.is_a?(Array)
        pick_attributes_core(atr_val, from, to, range, step)
      elsif atr_val.is_a?(Hash)
        Hash[pick_attributes_core(atr_val.to_a, from, to, range, step)]
      else
        atr_val
      end
    else
      sep ||= ""
      jnt ||= sep.is_a?(String) ? sep : nil
      AFHelpers.each_element(atr_val, String) do |v|
        pick_attributes_core(v.mb_chars.split(sep), from, to, range, step).join(jnt)
      end
    end
  end
end