Module: QueryDelegator::MemoryFetching

Defined in:
lib/query_delegator/memory_fetching.rb

Instance Method Summary collapse

Instance Method Details

#fetch(condition, default_value = nil) ⇒ Object

Returns the first record that meets the condition, otherwise returns the given block value or returns nil by default unless a second argument is specified.



8
9
10
11
12
13
# File 'lib/query_delegator/memory_fetching.rb', line 8

def fetch(condition, default_value = nil)
  grep(condition) { |found| return found }
  return yield condition if defined? yield

  default_value
end

#fetch_or_new(condition, &block_condition) ⇒ Object

Returns the first record that meets either the block condition if given or the argument condition, otherwise returns a new record with Hash from the argument condition.



17
18
19
# File 'lib/query_delegator/memory_fetching.rb', line 17

def fetch_or_new(condition, &block_condition)
  fetch(block_condition || condition) { new(condition.to_h) }
end