Class: Fetchable
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Fetchable
- Defined in:
- lib/fetchable.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
- #fetch(key, not_found_value = no_default_given, &block) ⇒ Object
-
#initialize(collection, args = {}) ⇒ Fetchable
constructor
A new instance of Fetchable.
Constructor Details
#initialize(collection, args = {}) ⇒ Fetchable
Returns a new instance of Fetchable.
4 5 6 7 8 |
# File 'lib/fetchable.rb', line 4 def initialize(collection, args={}) @collection = collection @finder_method_name = args.fetch(:finder_method, :[]) super(collection) end |
Instance Method Details
#fetch(key, not_found_value = no_default_given, &block) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/fetchable.rb', line 10 def fetch(key, not_found_value = no_default_given, &block) if not_found_value != no_default_given && block raise ArgumentError.new("Cannot provide both a default arg and block to #fetch") end @collection.public_send(@finder_method_name, key) || default_value(key, not_found_value, &block) end |