Module: QueryableArray::Shorthand

Included in:
QueryableArray
Defined in:
lib/queryable_array/shorthand.rb

Overview

Makes [search_hash] and [[search_hash]] behave as an alias for find_by and find_all respectively

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

If key is a Hash, Proc, or an Array containing a Hash or Proc then it acts like an alias for find_by or find_all respectively. It delegates the call to super in all other cases.

pages = QueryableArray.new Page.all

pages[uri: '/']                # => #<Page @uri='/' @name='Home'>
pages[uri: '/', name: 'Home']  # => #<Page @uri='/' @name='Home'>
pages[uri: '/', name: 'Typo']  # => nil

pages[[uri: '/']]                # => [#<Page @uri='/' @name='Home'>]
pages[[uri: '/', name: 'Typo']]  # => []

pages[uri: proc { |uri| uri.count('/') > 1 }]  # => #<Page @uri='/users/bob' @name='Bob'>


19
20
21
22
23
24
25
26
# File 'lib/queryable_array/shorthand.rb', line 19

def [](key)
  # Try to handle numeric indexes, ranges, and anything else that is
  # natively supported by +Array+ first
  super
rescue TypeError => error
  method, key = key.is_a?(Array) ? [:find_all, key.first] : [:find_by, key]
  key.is_a?(Hash) ? send(method, key) : raise(error)
end