Class: Omnis::Query::Param
- Inherits:
-
Object
- Object
- Omnis::Query::Param
- Defined in:
- lib/omnis/query.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #default_value ⇒ Object
-
#extract(params) ⇒ Object
extracts the value for a param, using the extractor lamba or the default value.
-
#initialize(name, operator, opts = {}, &extractor) ⇒ Param
constructor
A new instance of Param.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/omnis/query.rb', line 7 def name @name end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
7 8 9 |
# File 'lib/omnis/query.rb', line 7 def operator @operator end |
#opts ⇒ Object (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_value ⇒ Object
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 is_operator? value return @operator.new(@name, value, @opts) unless value.nil? end |