Class: MongoQL::Stage::AddFields

Inherits:
MongoQL::Stage show all
Defined in:
lib/mongo_ql/stage/add_fields.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx, *fields) ⇒ AddFields

Returns a new instance of AddFields.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mongo_ql/stage/add_fields.rb', line 8

def initialize(ctx, *fields)
  @ctx = ctx
  @field_projections = fields.map do |field|
                          case field
                          when Hash
                            field.map { |k, v| [k.to_s, to_expression(v)] }.to_h
                          else
                            raise ArgumentError, "#{field} is not a valid field mapping option"
                          end
                        end.inject({}) { |p, c| p.merge(c) }
end

Instance Attribute Details

#ctxObject

Returns the value of attribute ctx.



5
6
7
# File 'lib/mongo_ql/stage/add_fields.rb', line 5

def ctx
  @ctx
end

#field_projectionsObject

Returns the value of attribute field_projections.



6
7
8
# File 'lib/mongo_ql/stage/add_fields.rb', line 6

def field_projections
  @field_projections
end

Instance Method Details

#to_astObject



20
21
22
23
# File 'lib/mongo_ql/stage/add_fields.rb', line 20

def to_ast
  ast = { "$addFields" => field_projections }
  MongoQL::Utils.deep_transform_values(ast, &MongoQL::EXPRESSION_TO_AST_MAPPER)
end