Module: ActiveModel::AttributeFilters::AttributeSetMethods
- Includes:
- ActiveModel::AttributeSet::Enumerable
- Included in:
- ActiveModel::AttributeSet, MetaSet
- Defined in:
- lib/attribute-filters/attribute_set.rb
Constant Summary collapse
- AFHelpers =
Helpers module shortcut
ActiveModel::AttributeFilters::AttributeFiltersHelpers
Instance Method Summary collapse
-
#&(o) ⇒ AttributeSet
(also: #intersection, #intersect)
Returns a new attribute set containing elements common to the attribute set and the given enumerable object.
-
#+(o) ⇒ AttributeSet
Adds two sets by deeply merging their contents.
-
#-(o) ⇒ AttributeSet
Subtracts the given set from the current one by removing all the elements that have the same keys.
-
#^(o) ⇒ AttributeSet
Returns a new attribute set containing elements exclusive between the set and the given enumerable object (exclusive disjuction).
-
#add(*args) ⇒ AttributeSet
(also: #<<)
Adds the given object to the set and returns self.
-
#to_a ⇒ Array<String>
Returns an array of attribute names.
-
#to_attribute_set ⇒ AttributeSet
Returns self.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns a hash based on a set.
-
#to_set ⇒ Set<String>
Returns a set containing attribute names.
Methods included from ActiveModel::AttributeSet::Enumerable
#each_name_value, #select_accessible
Instance Method Details
#&(o) ⇒ AttributeSet Also known as: intersection, intersect
Returns a new attribute set containing elements common to the attribute set and the given enumerable object. Annotations from other set that aren’t in this set are copied.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/attribute-filters/attribute_set.rb', line 112 def &(o) my_class = self.class o = my_class.new(o) unless o.is_a?(Hash) r = my_class.new each_pair do |k, my_v| if o.include?(k) r[k] = merge_set(my_v, o[k]) { |a, b| a & b } end end r end |
#+(o) ⇒ AttributeSet
Adds two sets by deeply merging their contents. If any value stored in one set under conflicting key is true
, false
or nil
then value is taken from other set. If one of the conflicting values is a kind of Hash and the other is not the it’s converted to a hash which is merged in. Otherwise the left value wins.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/attribute-filters/attribute_set.rb', line 83 def +(o) my_class = self.class o = my_class.new(o) unless o.is_a?(Hash) r = my_class.new (keys + o.keys).uniq.each do |k| if self.key?(k) && o.key?(k) r[k] = merge_set(self[k], o[k]) { |a, b| a + b } else r[k] = AFHelpers.safe_dup(self[k] || o[k]) end end r end |
#-(o) ⇒ AttributeSet
Subtracts the given set from the current one by removing all the elements that have the same keys.
101 102 103 104 |
# File 'lib/attribute-filters/attribute_set.rb', line 101 def -(o) o = self.class.new(o) unless o.is_a?(Hash) reject { |k, v| o.include?(k) } end |
#^(o) ⇒ AttributeSet
Returns a new attribute set containing elements exclusive between the set and the given enumerable object (exclusive disjuction).
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/attribute-filters/attribute_set.rb', line 132 def ^(o) my_class = self.class o = my_class.new(o) unless o.is_a?(Hash) r = my_class.new (o.keys + keys).uniq.each do |k| if key?(k) next if o.key?(k) src = self[k] elsif o.key?(k) src = o[k] end r[k] = AFHelpers.safe_dup(src) end r end |
#add(*args) ⇒ AttributeSet Also known as: <<
Adds the given object to the set and returns self. If the object is already in the set, returns nil. If the object is an array it adds each element of the array. The array is not flattened so if it contains other arrays then they will be added as the arrays. When adding an array the returning value is also an array, which contains elements that were successfuly added to set and didn’t existed there before.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/attribute-filters/attribute_set.rb', line 32 def add(*args) args.flatten.each do |a| if a.is_a?(Hash) deep_merge!(a) elsif a.is_a?(::Enumerable) a.each { |e| self[e] = true unless e.blank? } else self[a] = true end end self end |
#to_a ⇒ Array<String>
Returns an array of attribute names.
49 50 51 |
# File 'lib/attribute-filters/attribute_set.rb', line 49 def to_a keys end |
#to_attribute_set ⇒ AttributeSet
Returns self.
64 65 66 |
# File 'lib/attribute-filters/attribute_set.rb', line 64 def to_attribute_set self end |
#to_hash ⇒ Hash Also known as: to_h
Returns a hash based on a set.
56 57 58 |
# File 'lib/attribute-filters/attribute_set.rb', line 56 def to_hash Hash.new.deep_merge(self) end |
#to_set ⇒ Set<String>
Returns a set containing attribute names.
71 72 73 |
# File 'lib/attribute-filters/attribute_set.rb', line 71 def to_set keys.to_set end |