module QueryDelegator::MemoryFetching

Public Instance Methods

fetch(condition, default_value = nil) { |condition| ... } click to toggle source

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.

# 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) click to toggle source

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.

# 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