Module: ActiveModel::AttributeFilters::ClassMethods

Includes:
FilteringRegistration
Defined in:
lib/attribute-filters/dsl_sets.rb,
lib/attribute-filters/common_filters.rb,
lib/attribute-filters/dsl_attr_virtual.rb,
lib/attribute-filters/attribute_set_annotations.rb

Overview

This module contains class methods that create DSL for managing attribute sets.

Instance Method Summary collapse

Methods included from FilteringRegistration

#filtering_method

Instance Method Details

#attribute_setObject, Hash{Symbol => AttributeSet<String>} #attribute_set(set_name) ⇒ AttributeSet<String> #attribute_set(set_name, *attribute_names) ⇒ nil #attribute_set(associations) ⇒ nil #attribute_set(associations, *attribute_names) ⇒ nil Also known as: attributes_set, attributes_that_are, attributes_that, properties_that, has_attribute_set, has_attribute_that, has_attribute_that_is, has_attributes, has_attributes_set, has_attributes_that_are, has_attributes_that, has_properties_that

Overloads:

  • #attribute_setObject, Hash{Symbol => AttributeSet<String>}

    Gets all the defined attribute sets by calling attribute_sets unless some other module overriden this method – In such cache it calls that method.

    Returns:

    • (Object, Hash{Symbol => AttributeSet<String>})

      the collection of attribute sets indexed by their names

  • #attribute_set(set_name) ⇒ AttributeSet<String>

    Gets the attribute set of the given name from internal storage.

    Parameters:

    • set_name (Symbol, String)

      name of a set

    Returns:

  • #attribute_set(set_name, *attribute_names) ⇒ nil

    Adds new attributes to a set of attributes.

    Parameters:

    • set_name (Symbol, String)

      name of a set

    • attribute_names (Array<Symbol,String>)

      names of attributes to be stored in set

    Returns:

    • (nil)
  • #attribute_set(associations) ⇒ nil

    Adds new attributes to a set of attributes.

    Parameters:

    • associations (Hash{Symbol,String => Array<Symbol,String>})

      hash containing set names and arrays of attributes

    Returns:

    • (nil)
  • #attribute_set(associations, *attribute_names) ⇒ nil

    Creates one new set of attributes of the given names.

    Parameters:

    • associations (Hash{Symbol,String => String,Array<Symbol,String>})

      hash containing set names and arrays of attributes

    • attribute_names (Array<Symbol,String>)

      names of additional attributes to be stored in set

    Returns:

    • (nil)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/attribute-filters/dsl_sets.rb', line 298

def af_attribute_set(*args)
  case args.size
  when 0
    attribute_sets
  when 1
    first_arg = args.first
    if first_arg.is_a?(Hash)                              # [write] multiple sets defined
      first_arg.each_pair { |k, v| af_attribute_set(k, v) }
      nil
    else                                                  # [read] single set to read
      r = __attribute_sets[first_arg.to_sym]
      r.frozen? ? r : r.deep_dup
    end
  else
    first_arg = args.shift
    if first_arg.is_a?(Hash)                              # [write] multiple sets defined
      first_arg.each_pair do |k, v|
        af_attribute_set(k, v, args)
      end
    else                                                  # [write core] sinle set and optional attrs given
      AFHelpers.check_wanted_methods(self)
      add_atrs_to_set(first_arg.to_sym, *args)
    end
    nil
  end
end

#annotate_attribute_set(set_name, attribute_name, annotation_key, value) ⇒ nil #annotate_attribute_set(set_name, *annotation) ⇒ nil Also known as: annotate_attributes_that_are, annotate_attributes_that, annotate_attributes_are, annotate_attributes_for, annotate_attributes_set, annotate_properties_that, annotate_attributes, annotates_attributes_that_are, annotates_attributes_that, annotates_attributes_are, annotates_attributes_for, annotates_attributes_set, annotates_properties_that, annotates_attributes, attribute_set_annotate

This method is a wrapper that helps to annotate attributes.

@overload annotate_attribute_set(set_name, attribute_name, annotation)
 @param set_name [Symbol,String] name of a set
 @param attribute_name [Symbol,String] name of annotated attribute
 @param annotation [Hash{Symbol => Object}] annotation key => value pairs
 @return [nil]

@overload annotate_attribute_set(set_name, annotation)
 @param set_name [Symbol,String] name of a set
 @param annotation [Hash{Symbol => Array<Symbol,Object>}] annotation key and value assigned to attribute name
 @return [nil]

@overload annotate_attribute_set(set_name, annotations)
 @param set_name [Symbol,String] name of a set
 @param annotations [Hash{Symbol => Hash{Symbol => Object}}] annotation key => value pairs for attributes
 @return [nil]

Overloads:

  • #annotate_attribute_set(set_name, attribute_name, annotation_key, value) ⇒ nil

    Parameters:

    • set_name (Symbol, String)

      name of a set

    • attribute_name (Symbol, String)

      name of annotated attribute

    • annotation_key (Symbol, String)

      name of annotation key

    • value (Object)

      annotation value

    Returns:

    • (nil)
  • #annotate_attribute_set(set_name, *annotation) ⇒ nil

    Parameters:

    • set_name (Symbol, String)

      name of a set

    • annotation (Array<Symbol,Symbol,Object>)

      ] attribute name, annotation key and value

    Returns:

    • (nil)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 47

def annotate_attribute_set(*args)
  first_arg = args.shift
  if first_arg.is_a?(Hash)                  # multiple sets defined
    first_arg.each_pair do |k, v|
      annotate_attribute_set(k, v, *args)
    end
  else
    atr_name, an_key, an_val = args
    if atr_name.is_a?(Hash)
      atr_name.each_pair do |k, v|
        annotate_attribute_set(first_arg, k, v)
      end
    elsif atr_name.is_a?(Array)
      annotate_attribute_set(first_arg, *atr_name) 
    elsif an_key.is_a?(Hash)
      an_key.each_pair do |k, v|
        annotate_attribute_set(first_arg, atr_name, k, v)
      end
    else
      unless an_key.nil? || atr_name.nil?
        first_arg = first_arg.to_sym
        unless __attribute_sets.key?(first_arg)
          raise ArgumentError, "trying to annotate non-existent set '#{first_arg}'"
        end
        __attribute_sets[first_arg].annotate(atr_name, an_key, an_val)
      end
    end
  end
  nil
end

#attribute_setsMetaSet{Symbol => AttributeSet<String>} Also known as: attributes_sets, properties_sets

Note:

Use key method explicitly to check if the given set exists. The hash returned by this method will always return ActiveModel::AttributeSet object. If there is no such set defined then the returned, matching set will be empty. All set objects are duplicates of the defined sets.

Gets all the defined attribute sets.

Returns:

  • (MetaSet{Symbol => AttributeSet<String>})

    the collection of attribute sets indexed by their names



419
420
421
# File 'lib/attribute-filters/dsl_sets.rb', line 419

def attribute_sets
  __attribute_sets.deep_dup
end

#attributes_to_setsMetaSet{String => AttributeSet<Symbol>} Also known as: attribute_sets_map

Note:

Use key method explicitly to check if the given attribute is assigned to any set. The hash returned by this method will always return ActiveModel::AttributeSet object. If the attribute is not assigned to any set then the returned, matching set will be empty. This method returns a duplicate of each reverse mapped set.

Gets all the defined attribute set names hashed by attribute names.

Returns:

  • (MetaSet{String => AttributeSet<Symbol>})

    the collection of attribute set names indexed by attribute names



433
434
435
# File 'lib/attribute-filters/dsl_sets.rb', line 433

def attributes_to_sets
  __attributes_to_sets_map.deep_dup
end

#delete_annotation_from_set(set_name, atr_name = nil, *annotations) ⇒ nil Also known as: delete_annotations_from_set, deletes_annotation_from_set, deletes_annotations_from_set

Deletes annotaion from a given set

Parameters:

  • set_name (String, Symbol)

    set name

  • atr_name (String, Symbol) (defaults to: nil)

    attribute name

  • annotations (Array<String,Symbol>)

    annotation keys

Returns:

  • (nil)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 149

def delete_annotation_from_set(set_name, atr_name = nil, *annotations)
  if set_name.is_a?(Hash)
    r = {}
    set_name.each_pair do |k_set, v_attrs|
      if v_attrs.is_a?(Hash)
        v_attrs.each_pair do |k_attr, v_annotations|
          delete_annotation_from_set(k_set, k_attr, *v_annotations)
        end
      else
        delete_annotation_from_set(k_set, v_attrs, *annotations)
      end
    end
  else
    set_name = set_name.to_sym
    return nil unless __attribute_sets.key?(set_name) && atr_name.present?
    atr_name = atr_name.to_sym
    __attribute_sets[set_name].delete_annotation(atr_name, *annotations)
  end
  nil
end

#filter_attributeHash{String => AttributeSet<Symbol>} #filter_attribute(attribute_name) ⇒ AttributeSet<Symbol> #filter_attribute(attribute_name, *set_names) ⇒ nil #filter_attribute(associations) ⇒ nil #filter_attribute(associations, *set_names) ⇒ nil Also known as: the_attribute, attribute_to_set, filtered_attribute, filtered_attributes, filters_attribute, filters_attributes, its_attribute, has_attribute, has_the_attribute, has_filtered_attribute, has_filtered_attributes

Overloads:

  • #filter_attributeHash{String => AttributeSet<Symbol>}

    Gets all the defined attribute sets.

    Returns:

    • (Hash{String => AttributeSet<Symbol>})

      the collection of attribute set names indexed by attribute names

  • #filter_attribute(attribute_name) ⇒ AttributeSet<Symbol>

    Gets the names of all attribute sets that an attribute of the given name belongs to.

    Parameters:

    • attribute_name (Symbol, String)

      attribute name

    Returns:

    • (AttributeSet<Symbol>)

      the collection of attribute set names

  • #filter_attribute(attribute_name, *set_names) ⇒ nil

    Adds an attribute of the given name to attribute sets.

    Parameters:

    • attribute_name (Symbol, String)

      name of an attribute

    • set_names (Array<Symbol,String>)

      names of attribute sets to add the attribute to

    Returns:

    • (nil)
  • #filter_attribute(associations) ⇒ nil

    Adds an attribute of the given name to attribute sets.

    Parameters:

    • associations (Hash{Symbol,String => Array<Symbol,String>})

      hash containing attribute names and arrays of attribute set names

    Returns:

    • (nil)
  • #filter_attribute(associations, *set_names) ⇒ nil

    Creates one new set of attributes of the given names.

    Parameters:

    • associations (Hash{Symbol,String => String,Array<Symbol,String>})

      hash containing attribute names and arrays of attribute set name

    • set_names (Array<Symbol,String>)

      names of additional sets to assign attributes to

    Returns:

    • (nil)


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/attribute-filters/dsl_sets.rb', line 365

def filter_attribute(*args)
  AFHelpers.check_wanted_methods(self)
  case args.size
  when 0
    attributes_to_sets
  when 1
    first_arg = args.first
    if first_arg.is_a?(Hash)
      first_arg.each_pair { |k, v| filter_attribute(k, v) }
      nil
    else
      r = __attributes_to_sets_map[first_arg.to_s]
      r.frozen? ? r : r.deep_dup
    end
  else
    first_arg = args.shift
    if first_arg.is_a?(Hash)
      first_arg.each_pair do |k, v|
        filter_attribute(k, v, args)
      end
    else
      first_arg = first_arg.to_s
      args.flatten.compact.each do |set_name|
        if set_name.is_a?(Hash) # annotation
          set_name.each_pair do |set_name_b, a_defs|
            af_attribute_set(set_name_b, first_arg => a_defs)
          end
        else
          af_attribute_set(set_name, first_arg)
        end
      end
    end
    nil
  end
end

#setup_attributes_set(set_name, attribute_defs, param_defs = {}, default_param = nil) ⇒ nil Also known as: setup_attributes_that

Helps in adding attributes to sets with annotations used to store parameters.

Examples:

setup_attributes_set  :should_be_filled,
                      { 'atr_name'  => { :with => 'x', :fill_any => true }, :other_atr => 'text' },
                      { :fill_value => [ :with, :fill_with, :value, :content ] },
                      :fill_value

Parameters:

  • set_name (Symbol, String)

    name of a set

  • param_defs (Hash{Symbol => Array<Symbol,String>}) (defaults to: {})
  • default_param (Symbol, String, nil) (defaults to: nil)
  • attribute_defs (Hash{Symbol => Object}, Array<Symbol,String>)

Returns:

  • (nil)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/attribute-filters/attribute_set_annotations.rb', line 105

def setup_attributes_set(set_name, attribute_defs, param_defs = {}, default_param = nil)
  # create parameter keys conversion hash
  pdefs = {}
  param_defs.each_pair do |k, v|
    k = k.to_sym
    if v.is_a?(Array)
      v.each { |x| pdefs[x.to_sym] = k }
    else
      pdefs[v.to_sym] = k
    end
  end
  # process attribute -> annotations pairs or other arguments given
  if attribute_defs.is_a?(Array)
    attribute_defs.each { |arg| setup_attributes_set(set_name, arg, param_defs, default_param) }
  elsif attribute_defs.is_a?(Hash)
    output_set = {}
    attribute_defs.each_pair do |atr_name, atr_annotations|
      atr_name = atr_name.to_s
      output_set[atr_name] = {}
      if atr_annotations.is_a?(Hash)
        atr_annotations.each_pair do |an_key, an_val|
          an_key = an_key.to_sym
          an_key = pdefs[an_key] if pdefs.key?(an_key)
          output_set[atr_name][an_key] = an_val
        end
      elsif !default_param.nil?
        output_set[atr_name][default_param.to_sym] = atr_annotations
      end
    end
    af_attribute_set(set_name, output_set)
  else
    af_attribute_set(set_name, *attribute_defs)
  end
  nil
end

#treat_as_real(*attributes) #treat_as_realAttributeSet Also known as: attribute_filters_semi_real, treat_attribute_as_real, treat_attributes_as_real, treats_attribute_as_real, treats_attributes_as_real, treats_as_real

Overloads:

  • #treat_as_real(*attributes)
    Note:

    To operate on virtual attributes use attr_virtual instead.

    This method returns an undefined value.

    Informs Attribute Filters that the given attributes should be treated as present, even they are not in attributes hash provided by ORM or ActiveModel. Useful when operating on semi-virtual attributes.

    Parameters:

    • attributes (Array)

      list of attribute names

  • #treat_as_realAttributeSet

    Gets the memorized attribute names that should be treated as existing.

    Returns:



454
455
456
457
458
# File 'lib/attribute-filters/dsl_sets.rb', line 454

def treat_as_real(*args)
  return __attribute_filters_semi_real.deep_dup if args.blank?
  __attribute_filters_semi_real << args.flatten.compact.map { |atr| atr.to_s }
  nil
end

#attribute_filters_virtual(*attributes) #attribute_filters_virtualAttributeSet Also known as: attribute_filters_virtual, treat_attribute_as_virtual, treat_attributes_as_virtual, treats_attribute_as_virtual, treats_attributes_as_virtual, treats_as_virtual

Overloads:

  • #attribute_filters_virtual(*attributes)
    Note:

    This method may be used directly ONLY if your setter notifies Rails about changes. Otherwise it’s recommended to use the DSL keyword attr_virtual.

    This method returns an undefined value.

    Informs Attribute Filters that the given attributes should be treated as virtual (even not present in the attributes hash provided by ORM or ActiveModel).

    Parameters:

    • attributes (Array)

      list of attribute names

  • #attribute_filters_virtualAttributeSet

    Gets the memorized attribute names that should be treated as virtual.

    Returns:



482
483
484
485
486
487
488
# File 'lib/attribute-filters/dsl_sets.rb', line 482

def treat_as_virtual(*args)
  return __attribute_filters_virtual.deep_dup if args.blank?
  args.flatten.compact.each do |atr|
    __attribute_filters_virtual[atr.to_s] = :no_wrap
  end
  nil
end