Module: QueryableArray::DefaultFinder
- Defined in:
- lib/queryable_array/default_finder.rb
Overview
Allows objects to be searched by default_finders
thru []
. For example:
users = QueryableArray.new(User.all, :email)
users['[email protected]'] # => #<User @email='[email protected]'>
users['[email protected]'] # => nil
Instance Attribute Summary collapse
-
#default_finders ⇒ Object
Returns the value of attribute default_finders.
Instance Method Summary collapse
-
#[](key) ⇒ Object
If
default_finders
has been set andkey
is not aFixnum
,Range
, or anything else natively supported byArray
then it loops thru eachdefault_finders
and returns the first matching result of find_by(finder => key) or find_all(finder => key.first) ifkey
is anArray
. -
#initialize(array = [], default_finders = nil) ⇒ Object
Accepts an initial
array
which defaults to[]
. -
#query(search) ⇒ Object
Converts a search into a
Proc
object that can be passed tofind_by
orfind_all
.
Instance Attribute Details
#default_finders ⇒ Object
Returns the value of attribute default_finders.
8 9 10 |
# File 'lib/queryable_array/default_finder.rb', line 8 def default_finders @default_finders end |
Instance Method Details
#[](key) ⇒ Object
If default_finders
has been set and key
is not a Fixnum
, Range
, or anything else natively supported by Array
then it loops thru each default_finders
and returns the first matching result of find_by(finder => key) or find_all(finder => key.first) if key
is an Array
. If key
is already a Hash
or an Array
containing a Hash
then it acts like an alias for find_by
or find_all
respectively. It also accepts a Proc
or any object that responds to call
. It behaves exactly like its superclass Array
in all other cases.
pages = QueryableArray.new(Page.all, [:uri, :name])
pages['/'] # => #<Page @uri='/' @name='Home'>
pages['Home'] # => #<Page @uri='/' @name='Home'>
pages[/home/i] # => #<Page @uri='/' @name='Home'>
pages['missing'] # => nil
pages[[/users/i]] # => [#<Page @uri='/users/bob' @name='Bob'>, #<Page @uri='/users/steve' @name='Steve'>]
pages[[/missing/i]] # => []
pages[proc { |page| page.uri == '/' }] # => #<Page @uri='/' @name='Home'>
pages[[proc { |page| page.uri =~ /users/i }]] # => [#<Page @uri='/users/bob' @name='Bob'>, #<Page @uri='/users/steve' @name='Steve'>]
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/queryable_array/default_finder.rb', line 39 def [](key) super rescue TypeError => error if default_finders.empty? raise error else method, key = key.is_a?(Array) ? [:find_all, key.first] : [:find_by, key] send method, &query(key) end end |
#initialize(array = [], default_finders = nil) ⇒ Object
Accepts an initial array
which defaults to []
. An optional default_finders
may also be specified as the second argument which is used in QueryableArray#[] for quick lookups. It defaults to nil
which disables this behavior. See the QueryableArray#[] method for more documentation.
14 15 16 17 |
# File 'lib/queryable_array/default_finder.rb', line 14 def initialize(array = [], default_finders = nil) super array self.default_finders = Array(default_finders) end |
#query(search) ⇒ Object
Converts a search into a Proc
object that can be passed to find_by
or find_all
. If search
is a Proc
or an object that responds to call
then it is wrapped in a Proc
and returned. Otherwise the returned Proc
loops thru each default_finders
looking for a value that matches search
.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/queryable_array/default_finder.rb', line 54 def query(search) Proc.new do |object| proc = search.respond_to?(:call) ? search : Proc.new do |object| default_finders.any? do |attribute| finder(attribute => search).call object end end proc.call object end end |