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.



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
# File 'lib/activecube/graphql/parse_tree.rb', line 14

def initialize cube, context_node, parent = nil

  @cube = cube
  @parent = parent
  @type_name = context_node.definition.type.try(:of_type).try(:name) || context_node.definition.type.try(:name)

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

  @arguments =  sort_node_arguments context_node

  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?(context_node) }.
      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

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
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

#type_nameObject (readonly)

Returns the value of attribute type_name.



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

def type_name
  @type_name
end

Instance Method Details

#append_query(query) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/activecube/graphql/parse_tree.rb', line 84

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

#as_json(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/activecube/graphql/parse_tree.rb', line 74

def as_json options = {}
  {
    cube: cube.name,
    name: name,
    definition: definition,
    key: key,
    children: children
  }
end

#sort_node_arguments(context_node) ⇒ Object



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

def sort_node_arguments context_node
  arguments = context_node.arguments.to_h
  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?(context_node) ⇒ Boolean

Returns:

  • (Boolean)


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

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