Class: Horza::Adapters::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/horza/adapters/options.rb

Direct Known Subclasses

ArelJoin

Constant Summary collapse

META_METHODS =
[:conditions, :limit, :offset, :order, :target, :id, :via]

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Options

Returns a new instance of Options.



6
7
8
# File 'lib/horza/adapters/options.rb', line 6

def initialize(options)
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



10
11
12
13
# File 'lib/horza/adapters/options.rb', line 10

def method_missing(method)
  super unless META_METHODS.include? method
  @options[method]
end

Instance Method Details

#eager_argsObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/horza/adapters/options.rb', line 31

def eager_args
  raise ::Horza::Errors::InvalidOption.new('You must pass eager_load: true and defined a target') unless eager_load? && target
  return target unless via && via.present?

  via.reverse.reduce({}) do |hash, table|
    if hash.empty?
      hash.merge(table => target)
    else
      { table => hash }
    end
  end
end

#eager_load?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/horza/adapters/options.rb', line 27

def eager_load?
  !!@options[:eager_load]
end

#order_directionObject



20
21
22
23
24
25
# File 'lib/horza/adapters/options.rb', line 20

def order_direction
  return :desc unless order.present?

  raise ::Horza::Errors::InvalidOption.new('Order must be :asc or :desc') unless [:asc, :desc].include?(@options[:order][order_field])
  order[order_field]
end

#order_fieldObject



15
16
17
18
# File 'lib/horza/adapters/options.rb', line 15

def order_field
  return :id unless order.present?
  @options[:order].keys.first
end