Module: Poro::Context::FindMethods::HelperMethods

Defined in:
lib/poro/context.rb

Overview

Contains some private helper methods to help process finds. These rarely need to be used or overriden by Context subclasses.

Instance Method Summary collapse

Instance Method Details

#clean_find_opts(opts) ⇒ Object

Cleans the find opts. This makes it so that no matter which (legal) style that they give their options to find, they are made into a single standard format that the subclasses can depend on.



356
357
358
359
360
361
# File 'lib/poro/context.rb', line 356

def clean_find_opts(opts)
  cleaned_opts = opts.dup
  cleaned_opts[:limit] = hashize_limit(opts[:limit]) if opts.has_key?(:limit)
  cleaned_opts[:order] = hashize_order(opts[:order]) if opts.has_key?(:order)
  return cleaned_opts
end

#hashize_limit(limit_opt) ⇒ Object

Takes the limit option to find and returns a uniform hash version of it.

The hash has the form:

{:limit => (integer || nil), :offset => (integer)}

Note that a limit of nil means that all records shoudl be returned.



369
370
371
372
373
374
375
376
377
# File 'lib/poro/context.rb', line 369

def hashize_limit(limit_opt)
  if( limit_opt.kind_of?(Hash) )
    return {:limit => nil, :offset => 0}.merge(limit_opt)
  elsif( limit_opt.kind_of?(Array) )
    return {:limit => limit_opt[0], :offset => limit_opt[1]||0}
  else
    return {:limit => (limit_opt&&limit_opt.to_i), :offset => 0}
  end
end

#hashize_order(order_opt) ⇒ Object

Takes the order option to find and returns a uniform hash version of it.

Returns a hash of each sort key followed by either :asc or :desc. If there are no sort keys, this returns an empty hash.



383
384
385
386
387
388
389
390
391
392
393
# File 'lib/poro/context.rb', line 383

def hashize_order(order_opt)
  if( order_opt.kind_of?(Hash) )
    return order_opt
  elsif( order_opt.kind_of?(Array) )
    return order_opt.inject({}) {|hash,(key,direction)| hash[key] = direction || :asc; hash}
  elsif( order_opt.nil? )
    return {}
  else
    return {order_opt => :asc}
  end
end