Class: Fixtury::Dependency
- Inherits:
-
Object
- Object
- Fixtury::Dependency
- Defined in:
- lib/fixtury/dependency.rb
Instance Attribute Summary collapse
-
#accessor ⇒ Object
readonly
Returns the value of attribute accessor.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
Class Method Summary collapse
-
.from(parent, thing) ⇒ Array<Fixtury::Dependency>
Resolve a Dependency from a multitude of input types.
Instance Method Summary collapse
- #definition ⇒ Object
-
#initialize(parent:, search:, accessor:) ⇒ Dependency
constructor
A new instance of Dependency.
- #inspect ⇒ Object
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
#accessor ⇒ Object (readonly)
Returns the value of attribute accessor.
35 36 37 |
# File 'lib/fixtury/dependency.rb', line 35 def accessor @accessor end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
35 36 37 |
# File 'lib/fixtury/dependency.rb', line 35 def parent @parent end |
#search ⇒ Object (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
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
#definition ⇒ Object
43 44 45 |
# File 'lib/fixtury/dependency.rb', line 43 def definition @definition ||= parent&.get!(search) end |
#inspect ⇒ Object
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 |