Class: Fixtury::Locator
- Inherits:
-
Object
- Object
- Fixtury::Locator
- 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
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Class Method Summary collapse
Instance Method Summary collapse
-
#dump(stored_value, context: nil) ⇒ Object
Provide the value to the backend to generate a locator key.
-
#initialize(backend: ::Fixtury::LocatorBackend::Memory.new) ⇒ Locator
constructor
A new instance of Locator.
- #inspect ⇒ Object
-
#load(locator_key) ⇒ Object
Load the value associated with the provided locator key.
-
#recognizable_key?(locator_key) ⇒ Boolean
Determine if the provided locator_key is a valid form recognized by the backend.
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
#backend ⇒ Object (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.
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 |
#inspect ⇒ Object
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.
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.
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 |