Class: Fetchable

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/fetchable.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(collection, args = {}) ⇒ Fetchable

Returns a new instance of Fetchable.



5
6
7
8
9
# File 'lib/fetchable.rb', line 5

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



11
12
13
14
15
16
17
# File 'lib/fetchable.rb', line 11

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