class SetAttributes::Map

Public Class Methods

balance_mapping(mapping) click to toggle source
# File lib/set_attributes/map.rb, line 27
def self.balance_mapping(mapping)
  return mapping if mapping.is_a? Hash
  { mapping => mapping }
end
build(mappings, exclude: nil) click to toggle source
# File lib/set_attributes/map.rb, line 9
def self.build(mappings, exclude: nil)
  mappings ||= []
  mappings = Array(mappings)

  exclude ||= []
  exclude = Array(exclude)

  instance = new

  mappings.each do |mapping|
    instance.add(mapping)
  end

  instance.exclude(exclude)

  instance
end

Public Instance Methods

<<(*mappings)
Alias for: add
[](key) click to toggle source
# File lib/set_attributes/map.rb, line 32
def [](key)
  mapping = mapping(key)
  values = mapping &.values

  if values.nil?
    nil
  else
    values[0]
  end
end
add(*mappings) click to toggle source
# File lib/set_attributes/map.rb, line 57
def add(*mappings)
  mappings = Array(mappings).flatten

  added_mappings = []
  mappings.each do |mapping|
    balanced_mapping = self.class.balance_mapping(mapping)
    self.mappings << balanced_mapping
    added_mappings << balanced_mapping
  end

  added_mappings
end
Also aliased as: <<
delete(*attributes) click to toggle source
# File lib/set_attributes/map.rb, line 77
def delete(*attributes)
  attributes = Array(attributes).flatten

  deleted_attributes = []
  attributes.each do |attribute|
    mapping = mapping(attribute)
    deleted_attribute = mappings.delete(mapping)
    deleted_attributes << deleted_attribute
  end

  deleted_attributes
end
Also aliased as: exclude
each(&action) click to toggle source
# File lib/set_attributes/map.rb, line 91
def each(&action)
  mappings.each(&action)
end
each_mapping(&action) click to toggle source
# File lib/set_attributes/map.rb, line 95
def each_mapping(&action)
  each do |mapping|
    source_attribute = mapping.keys[0]
    receiver_attribute = mapping.values[0]

    action.(source_attribute, receiver_attribute)
  end
end
empty?() click to toggle source
# File lib/set_attributes/map.rb, line 53
def empty?
  count == 0
end
exclude(*attributes)
Alias for: delete
include?(attribute) click to toggle source
# File lib/set_attributes/map.rb, line 49
def include?(attribute)
  mapping(attribute) != nil
end
keys() click to toggle source
# File lib/set_attributes/map.rb, line 71
def keys
  map do |mapping|
    mapping.keys[0]
  end
end
mapping(attribute) click to toggle source
# File lib/set_attributes/map.rb, line 43
def mapping(attribute)
  find do |mapping|
    mapping.keys[0] == attribute
  end
end
mappings() click to toggle source
# File lib/set_attributes/map.rb, line 5
def mappings
  @mappings ||= []
end