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
- #af_attribute_set(*args) ⇒ Object (also: #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)
-
#annotate_attribute_set(*args) ⇒ Object
(also: #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.
-
#attribute_sets ⇒ MetaSet{Symbol => AttributeSet<String>}
(also: #attributes_sets, #properties_sets)
Gets all the defined attribute sets.
-
#attributes_to_sets ⇒ MetaSet{String => AttributeSet<Symbol>}
(also: #attribute_sets_map)
Gets all the defined attribute set names hashed by attribute names.
-
#delete_annotation_from_set(set_name, atr_name = nil, *annotations) ⇒ nil
(also: #delete_annotations_from_set, #deletes_annotation_from_set, #deletes_annotations_from_set)
Deletes annotaion from a given set.
- #filter_attribute(*args) ⇒ Object (also: #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)
-
#setup_attributes_set(set_name, attribute_defs, param_defs = {}, default_param = nil) ⇒ nil
(also: #setup_attributes_that)
Helps in adding attributes to sets with annotations used to store parameters.
- #treat_as_real(*args) ⇒ Object (also: #attribute_filters_semi_real, #treat_attribute_as_real, #treat_attributes_as_real, #treats_attribute_as_real, #treats_attributes_as_real, #treats_as_real)
- #treat_as_virtual(*args) ⇒ Object (also: #attribute_filters_virtual, #treat_attribute_as_virtual, #treat_attributes_as_virtual, #treats_attribute_as_virtual, #treats_attributes_as_virtual, #treats_as_virtual)
Methods included from FilteringRegistration
Instance Method Details
#attribute_set ⇒ Object, 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
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]
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_sets ⇒ MetaSet{Symbol => AttributeSet<String>} Also known as: attributes_sets, properties_sets
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.
419 420 421 |
# File 'lib/attribute-filters/dsl_sets.rb', line 419 def attribute_sets __attribute_sets.deep_dup end |
#attributes_to_sets ⇒ MetaSet{String => AttributeSet<Symbol>} Also known as: attribute_sets_map
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.
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
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_attribute ⇒ Hash{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
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.
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_real ⇒ AttributeSet 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
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_virtual ⇒ AttributeSet 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
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 |