Class: Omnis::Query::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/omnis/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, operator, opts = {}, &extractor) ⇒ Param

Returns a new instance of Param.



8
9
10
11
12
# File 'lib/omnis/query.rb', line 8

def initialize(name, operator, opts={}, &extractor)
  # raise ArgumentError("operator must be a descendant of Omnis::Operators::NullOperator") unless operator.is_a? Omnis::Operators::NullOperator
  extractor ||= ->params { params[name] }
  @name, @operator, @opts, @extractor = name, operator, opts, extractor
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/omnis/query.rb', line 7

def name
  @name
end

#operatorObject (readonly)

Returns the value of attribute operator.



7
8
9
# File 'lib/omnis/query.rb', line 7

def operator
  @operator
end

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/omnis/query.rb', line 7

def opts
  @opts
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
17
18
# File 'lib/omnis/query.rb', line 14

def ==(other)
  return false unless other.is_a? self.class
  return false unless @name == other.name
  @operator == other.operator
end

#default_valueObject



28
29
30
31
32
# File 'lib/omnis/query.rb', line 28

def default_value
  expr = @opts[:default]
  return expr.call if expr.is_a? Proc
  return expr
end

#extract(params) ⇒ Object

extracts the value for a param, using the extractor lamba or the default value



21
22
23
24
25
26
# File 'lib/omnis/query.rb', line 21

def extract(params)
  value = @extractor.(params) if params.keys.include? name    # only run extractor if the param was in the request (params)
  value ||= default_value                                     # if there is a default, apply it
  return value if extracted_result_is_an_operator? value
  return @operator.new(@name, value, @opts) unless value.nil?
end