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

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.

Parameters:

  • o (Enumerable)

    object to intersect with

Returns:



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.

Returns:



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.

Returns:



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).

Parameters:

  • o (Enumerable)

    object to exclusively disjunct with

Returns:



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.

Parameters:

  • args (Array<Object,Hash,Array,Enumerable>)

    object(s) to be added to set

Returns:



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_aArray<String>

Returns an array of attribute names.

Returns:

  • (Array<String>)


49
50
51
# File 'lib/attribute-filters/attribute_set.rb', line 49

def to_a
  keys
end

#to_attribute_setAttributeSet

Returns self.

Returns:



64
65
66
# File 'lib/attribute-filters/attribute_set.rb', line 64

def to_attribute_set
  self
end

#to_hashHash Also known as: to_h

Returns a hash based on a set.

Returns:

  • (Hash)


56
57
58
# File 'lib/attribute-filters/attribute_set.rb', line 56

def to_hash
  Hash.new.deep_merge(self)
end

#to_setSet<String>

Returns a set containing attribute names.

Returns:

  • (Set<String>)


71
72
73
# File 'lib/attribute-filters/attribute_set.rb', line 71

def to_set
  keys.to_set
end