class Origin::Options

The options is a hash representation of options passed to MongoDB queries, such as skip, limit, and sorting criteria.

Public Instance Methods

[]=(key, value)
Alias for: store
fields() click to toggle source

Convenience method for getting the field options.

@example Get the fields options.

options.fields

@return [ Hash ] The fields options.

@since 1.0.0

# File lib/origin/options.rb, line 15
def fields
  self[:fields]
end
limit() click to toggle source

Convenience method for getting the limit option.

@example Get the limit option.

options.limit

@return [ Integer ] The limit option.

@since 1.0.0

# File lib/origin/options.rb, line 27
def limit
  self[:limit]
end
skip() click to toggle source

Convenience method for getting the skip option.

@example Get the skip option.

options.skip

@return [ Integer ] The skip option.

@since 1.0.0

# File lib/origin/options.rb, line 39
def skip
  self[:skip]
end
sort() click to toggle source

Convenience method for getting the sort options.

@example Get the sort options.

options.sort

@return [ Hash ] The sort options.

@since 1.0.0

# File lib/origin/options.rb, line 51
def sort
  self[:sort]
end
store(key, value) click to toggle source

Store the value in the options for the provided key. The options will handle all necessary serialization and localization in this step.

@example Store a value in the options.

options.store(:key, "testing")

@param [ String, Symbol ] key The name of the attribute. @param [ Object ] value The value to add.

@return [ Object ] The stored object.

@since 1.0.0

Calls superclass method
# File lib/origin/options.rb, line 67
def store(key, value)
  super(key, evolve(value))
end
Also aliased as: []=
to_pipeline() click to toggle source

Convert the options to aggregation pipeline friendly options.

@example Convert the options to a pipeline.

options.to_pipeline

@return [ Array<Hash> ] The options in pipeline form.

@since 2.0.0

# File lib/origin/options.rb, line 80
def to_pipeline
  pipeline = []
  pipeline.push({ "$skip" => skip }) if skip
  pipeline.push({ "$limit" => limit }) if limit
  pipeline.push({ "$sort" => sort }) if sort
  pipeline
end

Private Instance Methods

evolve(value) click to toggle source

Evolve a single key selection with various types of values.

@api private

@example Evolve a simple selection.

options.evolve(field, 5)

@param [ Object ] value The value to serialize.

@return [ Object ] The serialized object.

@since 1.0.0

# File lib/origin/options.rb, line 102
def evolve(value)
  case value
  when Hash
    evolve_hash(value)
  else
    value
  end
end
evolve_hash(value) click to toggle source

Evolve a single key selection with hash values.

@api private

@example Evolve a simple selection.

options.evolve(field, { "$gt" => 5 })

@param [ Hash ] value The hash to serialize.

@return [ Object ] The serialized hash.

@since 1.0.0

# File lib/origin/options.rb, line 123
def evolve_hash(value)
  value.inject({}) do |hash, (field, _value)|
    name, serializer = storage_pair(field)
    hash[normalized_key(name, serializer)] = _value
    hash
  end
end