Class: FactoryTrace::Structures::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_trace/structures/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factories = [], traits = []) ⇒ Collection

Returns a new instance of Collection.

Parameters:



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

#factoriesObject (readonly)

Returns the value of attribute factories.



6
7
8
# File 'lib/factory_trace/structures/collection.rb', line 6

def factories
  @factories
end

#traitsObject (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

Returns:

  • (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

Parameters:

  • names (Array<String>)

Returns:



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

Parameters:

  • name (String)

Returns:



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_hHash

Returns:

  • (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

#totalInteger

Total number of factories and traits

Returns:

  • (Integer)


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