Class: Activecube::Graphql::ParseTree::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/activecube/graphql/parse_tree.rb

Constant Summary collapse

TYPENAME =
'__typename'
KEY_FIELD_PREFIX =
'_aq.'
NULLABLE_OPERATORS =
[:eq,:not_eq,:is,:not]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cube, context_node, parent = nil) ⇒ Element

Returns a new instance of Element.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/activecube/graphql/parse_tree.rb', line 13

def initialize cube, context_node, parent = nil

  @cube = cube
  @parent = parent

  @name = context_node.name
  @key = parent ? (parent.key ? "#{parent.key}.#{name}" : KEY_FIELD_PREFIX+name ) : nil

  @context_node = context_node
  @ast_node = context_node.ast_node

  @arguments =  sort_node_arguments ast_node, context_node.arguments.to_h


  if parent
    @definition = context_node.definitions.first.name
    if parent.dimension
      @dimension = parent.dimension
      @field = (parent.field || dimension)[definition.to_sym]
      raise Activecube::InputArgumentError, "#{definition} not implemented for #{key} in cube #{cube.name}" unless @field
    elsif !parent.metric
      if !(@metric = (cube.metrics && cube.metrics[definition.to_sym])) && !(@dimension = (cube.dimensions && cube.dimensions[definition.to_sym]))
        raise Activecube::InputArgumentError, "Metric or dimension #{definition} for #{key} not defined for cube #{cube.name}"
      end
    end
  end

  @children = context_node.typed_children.values.map(&:values).flatten.uniq(&:name).
      select{|child| child.name!=TYPENAME || union? }.
      collect do |child|
    Element.new cube, child, self
  end

end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def arguments
  @arguments
end

#ast_nodeObject (readonly)

Returns the value of attribute ast_node.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def ast_node
  @ast_node
end

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def children
  @children
end

#context_nodeObject (readonly)

Returns the value of attribute context_node.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def context_node
  @context_node
end

#cubeObject (readonly)

Returns the value of attribute cube.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def cube
  @cube
end

#definitionObject (readonly)

Returns the value of attribute definition.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def definition
  @definition
end

#dimensionObject (readonly)

Returns the value of attribute dimension.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def dimension
  @dimension
end

#fieldObject (readonly)

Returns the value of attribute field.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def field
  @field
end

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def key
  @key
end

#metricObject (readonly)

Returns the value of attribute metric.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def metric
  @metric
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/activecube/graphql/parse_tree.rb', line 11

def parent
  @parent
end

Instance Method Details

#append_query(query) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/activecube/graphql/parse_tree.rb', line 75

def append_query query
  if parent

    if metric
      query = query.measure({key => apply_args(metric)})
    elsif dimension
      if children.empty?
        query = query.slice({key => apply_args(field || dimension)})
      elsif !arguments.empty?
        query = apply_args query
      end
    end

  else
    query = apply_args query, ( arguments && arguments.except('options'))
    query = apply_args query, ( arguments && arguments['options'] )
  end

  children.each do |child|
    query = child.append_query query
  end

  query
end

#sort_node_arguments(ast_node, arguments) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/activecube/graphql/parse_tree.rb', line 48

def sort_node_arguments ast_node, arguments
  if (options = arguments['options']).kind_of?(Hash)
    if opt_keys_args = context_node.ast_node.arguments.detect{|x| x.name=='options'}.value.try(:arguments)
      options_keys = opt_keys_args.map{|x|
        x.name.underscore.to_sym
      }
    elsif opt_keys_args_opt_name = context_node.ast_node.arguments.detect{|x| x.name=='options'}.value.try(:name)
      options_keys = context_node.query.variables[opt_keys_args_opt_name].arguments.argument_values.map{|x, y|
        x.underscore.to_sym
      }
    end

    arguments['options'] = Hash[
        options_keys.collect{|key|
          raise "Unmatched key #{key}" unless options[key]
          [key, options[key]]
        }

    ]
  end
  arguments
end

#union?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/activecube/graphql/parse_tree.rb', line 71

def union?
  context_node.return_type.kind_of? GraphQL::UnionType
end