Class: Fixtury::DependencyStore

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

Overview

An object which allows access to a specific subset of fixtures in the context of a definition’s dependencies.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition:, store:) ⇒ DependencyStore

Returns a new instance of DependencyStore.



8
9
10
11
# File 'lib/fixtury/dependency_store.rb', line 8

def initialize(definition:, store:)
  @definition = definition
  @store = store
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

If an accessor is used and we recognize the accessor as a dependency of our definition, we return the value of the dependency.



43
44
45
46
47
48
49
# File 'lib/fixtury/dependency_store.rb', line 43

def method_missing(method, *args, &block)
  if definition.dependencies.key?(method.to_s)
    get(method)
  else
    super
  end
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



6
7
8
# File 'lib/fixtury/dependency_store.rb', line 6

def definition
  @definition
end

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/fixtury/dependency_store.rb', line 6

def store
  @store
end

Instance Method Details

#get(search) ⇒ Object Also known as: []

Returns the value of the dependency with the given key. If the key is not present in the dependencies and strict_dependencies is enabled, an error will be raised. If strict_dependencies is not enabled the store will receive the search term directly.

Parameters:

  • search (String, Symbol)

    the accessor of the dependency

Returns:

  • (Object)

    the value of the dependency

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fixtury/dependency_store.rb', line 24

def get(search)
  dep = definition.dependencies[search.to_s]

  if dep.nil? && Fixtury.configuration.strict_dependencies
    raise Errors::UnknownDependencyError.new(definition, search)
  end

  if dep
    store.get(dep.definition.pathname)
  else
    store.with_relative_schema(definition.parent) do
      store.get(search)
    end
  end
end

#inspectObject



13
14
15
# File 'lib/fixtury/dependency_store.rb', line 13

def inspect
  "#{self.class}(definition: #{definition.pathname.inspect}, dependencies: #{definition.dependencies.keys.inspect})"
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/fixtury/dependency_store.rb', line 51

def respond_to_missing?(method, include_private = false)
  definition.dependencies.key?(method.to_s) || super
end