Class: FactoryTrace::Structures::Collection
- Inherits:
-
Object
- Object
- FactoryTrace::Structures::Collection
- Defined in:
- lib/factory_trace/structures/collection.rb
Instance Attribute Summary collapse
-
#factories ⇒ Object
readonly
Returns the value of attribute factories.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #add(element) ⇒ FactoryTrace::Structures::Factory|FactoryTrace::Structures::Trait
- #find_factory_by_names(names) ⇒ FactoryTrace::Structures::Factory|nil
- #find_trait_by_name(name) ⇒ FactoryTrace::Structures::Trait|nil
-
#initialize(factories = [], traits = []) ⇒ Collection
constructor
A new instance of Collection.
-
#merge!(collection) ⇒ Object
Merge passed collection into self.
- #to_h ⇒ Hash
-
#total ⇒ Integer
Total number of factories and traits.
Constructor Details
#initialize(factories = [], traits = []) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 |
# File 'lib/factory_trace/structures/collection.rb', line 10 def initialize(factories = [], traits = []) @factories = factories @traits = traits end |
Instance Attribute Details
#factories ⇒ Object (readonly)
Returns the value of attribute factories.
6 7 8 |
# File 'lib/factory_trace/structures/collection.rb', line 6 def factories @factories end |
#traits ⇒ Object (readonly)
Returns the value of attribute traits.
6 7 8 |
# File 'lib/factory_trace/structures/collection.rb', line 6 def traits @traits end |
Instance Method Details
#==(other) ⇒ Boolean
74 75 76 77 78 |
# File 'lib/factory_trace/structures/collection.rb', line 74 def ==(other) return false unless other.is_a?(FactoryTrace::Structures::Collection) factories == other.factories && traits == other.traits end |
#add(element) ⇒ FactoryTrace::Structures::Factory|FactoryTrace::Structures::Trait
18 19 20 21 22 23 24 25 |
# File 'lib/factory_trace/structures/collection.rb', line 18 def add(element) case element when FactoryTrace::Structures::Factory then factories << element when FactoryTrace::Structures::Trait then traits << element end element end |
#find_factory_by_names(names) ⇒ FactoryTrace::Structures::Factory|nil
30 31 32 |
# File 'lib/factory_trace/structures/collection.rb', line 30 def find_factory_by_names(names) factories.find { |factory| (names & factory.names).size > 0 } end |
#find_trait_by_name(name) ⇒ FactoryTrace::Structures::Trait|nil
37 38 39 |
# File 'lib/factory_trace/structures/collection.rb', line 37 def find_trait_by_name(name) traits.find { |trait| name == trait.name } end |
#merge!(collection) ⇒ Object
Merge passed collection into self
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/factory_trace/structures/collection.rb', line 52 def merge!(collection) collection.factories.each do |factory| if (persisted = find_factory_by_names(factory.names)) persisted.merge!(factory) else add(factory) end end collection.traits.each do |trait| add(trait) unless find_trait_by_name(trait.name) end end |
#to_h ⇒ Hash
42 43 44 45 46 47 |
# File 'lib/factory_trace/structures/collection.rb', line 42 def to_h { factories: factories.map(&:to_h), traits: traits.map(&:to_h) } end |
#total ⇒ Integer
Total number of factories and traits
69 70 71 |
# File 'lib/factory_trace/structures/collection.rb', line 69 def total traits.size + factories.size + factories.sum { |factory| factory.traits.size } end |