Class: Fixtury::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/fixtury/dependency.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent:, search:, accessor:) ⇒ Dependency

Returns a new instance of Dependency.



37
38
39
40
41
# File 'lib/fixtury/dependency.rb', line 37

def initialize(parent:, search:, accessor:)
  @parent = parent
  @search = search.to_s
  @accessor = accessor.to_s.split("/").last
end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor.



35
36
37
# File 'lib/fixtury/dependency.rb', line 35

def accessor
  @accessor
end

#parentObject (readonly)

Returns the value of attribute parent.



35
36
37
# File 'lib/fixtury/dependency.rb', line 35

def parent
  @parent
end

#searchObject (readonly)

Returns the value of attribute search.



35
36
37
# File 'lib/fixtury/dependency.rb', line 35

def search
  @search
end

Class Method Details

.from(parent, thing) ⇒ Array<Fixtury::Dependency>

Resolve a Dependency from a multitude of input types

Parameters:

  • parent (Fixtury::Definition)

    the parent definition

  • thing (Fixtury::Dependency, Hash, Array, String, Symbol)

    the thing to resolve @option thing [Fixtury::Dependency] a dependency will be cloned. @option thing [Hash] a hash with exactly one key will be resolved as { accessor => search }. @option thing [Array] an array with two elements will be resolved as [ accessor, search ]. @option thing [String, Symbol] a string or symbol will be resolved as both the accessor and the search.

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fixtury/dependency.rb', line 12

def self.from(parent, thing)
  out = case thing
  when self
    Dependency.new(parent: parent, search: thing.search, accessor: thing.accessor)
  when Hash
    thing.each_with_object([]) do |(k, v), arr|
      arr << Dependency.new(parent: parent, search: v, accessor: k)
    end
  when Array
    raise ArgumentError, "Array must have an even number of elements" unless thing.size % 2 == 0

    thing.each_slice(2).map do |pair|
      Dependency.new(parent: parent, search: pair[1], accessor: pair[0])
    end
  when String, Symbol
    Dependency.new(parent: parent, search: thing, accessor: thing)
  else
    raise ArgumentError, "Unknown dependency type: #{thing.inspect}"
  end

  Array(out)
end

Instance Method Details

#definitionObject



43
44
45
# File 'lib/fixtury/dependency.rb', line 43

def definition
  @definition ||= parent&.get!(search)
end

#inspectObject



47
48
49
# File 'lib/fixtury/dependency.rb', line 47

def inspect
  "#{self.class}(accessor: #{accessor.inspect}, search: #{search.inspect}, parent: #{parent.name.inspect})"
end