Class: SetAttributes::Map
- Inherits:
-
Object
- Object
- SetAttributes::Map
- Includes:
- Enumerable
- Defined in:
- lib/set_attributes/map.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #add(*mappings) ⇒ Object (also: #<<)
- #delete(*attributes) ⇒ Object (also: #exclude)
- #each(&action) ⇒ Object
- #each_mapping(&action) ⇒ Object
- #empty? ⇒ Boolean
- #include?(attribute) ⇒ Boolean
- #keys ⇒ Object
- #mapping(attribute) ⇒ Object
- #mappings ⇒ Object
Class Method Details
.balance_mapping(mapping) ⇒ Object
27 28 29 30 |
# 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) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Method Details
#[](key) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object Also known as: <<
57 58 59 60 61 62 63 64 65 66 67 68 |
# 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 |
#delete(*attributes) ⇒ Object Also known as: exclude
77 78 79 80 81 82 83 84 85 86 87 88 |
# 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 |
#each(&action) ⇒ Object
91 92 93 |
# File 'lib/set_attributes/map.rb', line 91 def each(&action) mappings.each(&action) end |
#each_mapping(&action) ⇒ Object
95 96 97 98 99 100 101 102 |
# 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? ⇒ Boolean
53 54 55 |
# File 'lib/set_attributes/map.rb', line 53 def empty? count == 0 end |
#include?(attribute) ⇒ Boolean
49 50 51 |
# File 'lib/set_attributes/map.rb', line 49 def include?(attribute) mapping(attribute) != nil end |
#keys ⇒ Object
71 72 73 74 75 |
# File 'lib/set_attributes/map.rb', line 71 def keys map do |mapping| mapping.keys[0] end end |
#mapping(attribute) ⇒ Object
43 44 45 46 47 |
# File 'lib/set_attributes/map.rb', line 43 def mapping(attribute) find do |mapping| mapping.keys[0] == attribute end end |
#mappings ⇒ Object
5 6 7 |
# File 'lib/set_attributes/map.rb', line 5 def mappings @mappings ||= [] end |