Class: Fixtury::Locator

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

Overview

Locator is responsible for recognizing, loading, and dumping references. It is a simple wrapper around a backend that is responsible for the actual work. The backend is expected to implement the following methods: recognizable_key?, recognized_value?, load_recognized_reference, dump_recognized_value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend: ::Fixtury::LocatorBackend::Memory.new) ⇒ Locator

Returns a new instance of Locator.



31
32
33
# File 'lib/fixtury/locator.rb', line 31

def initialize(backend: ::Fixtury::LocatorBackend::Memory.new)
  @backend = backend
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



29
30
31
# File 'lib/fixtury/locator.rb', line 29

def backend
  @backend
end

Class Method Details

.from(thing) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fixtury/locator.rb', line 11

def self.from(thing)
  case thing
  when ::Fixtury::Locator
    thing
  when nil
     ::Fixtury::Locator.new
  when Symbol
    begin
      require "fixtury/locator_backend/#{thing}"
    rescue LoadError
    end
    backend = ::Fixtury::LocatorBackend.const_get(thing.to_s.camelize, false).new
    ::Fixtury::Locator.new(backend: backend)
  else
    raise ArgumentError, "Unable to create a locator from #{thing.inspect}"
  end
end

Instance Method Details

#dump(stored_value, context: nil) ⇒ Object

Provide the value to the backend to generate a locator key.

Parameters:

  • stored_value (Object)

    the value to dump

  • context (String) (defaults to: nil)

    a string to include in the error message if the value is nil

Returns:

  • (Object)

    the locator key

Raises:

  • (ArgumentError)

    if the value is nil

  • (ArgumentError)

    if the backend is unable to dump the value



68
69
70
71
72
73
74
75
# File 'lib/fixtury/locator.rb', line 68

def dump(stored_value, context: nil)
  raise ArgumentError, "Unable to dump a nil value. #{context}" if stored_value.nil?

  locator_key = backend.dump(stored_value)
  raise ArgumentError, "Dump resulted in a nil locator value. #{context}" if locator_key.nil?

  locator_key
end

#inspectObject



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

def inspect
  "#{self.class}(backend: #{backend.class})"
end

#load(locator_key) ⇒ Object

Load the value associated with the provided locator key.

Parameters:

  • locator_key (Object)

    the locator key to load

Returns:

  • (Object)

    the loaded value

Raises:

  • (ArgumentError)

    if the locator key is nil



55
56
57
58
59
# File 'lib/fixtury/locator.rb', line 55

def load(locator_key)
  raise ArgumentError, "Unable to load a nil locator value" if locator_key.nil?

  backend.load(locator_key)
end

#recognizable_key?(locator_key) ⇒ Boolean

Determine if the provided locator_key is a valid form recognized by the backend.

Parameters:

  • locator_key (Object)

    the locator key to check

Returns:

  • (Boolean)

    true if the locator key is recognizable by the backend

Raises:

  • (ArgumentError)

    if the locator key is nil



44
45
46
47
48
# File 'lib/fixtury/locator.rb', line 44

def recognizable_key?(locator_key)
  raise ArgumentError, "Unable to recognize a nil locator value" if locator_key.nil?

  backend.recognizable_key?(locator_key)
end