gql 0.0.15 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 057b52e4c11305751f76664db776b55aca6ddc7c
4
- data.tar.gz: 701c40c23c2a7d846dadf1620545851d9d31788e
3
+ metadata.gz: fd8a84ed2a267ac54920709090a7f462e68d3669
4
+ data.tar.gz: 4a3e5bec9744df6f0af716d1ac21bdb9dfe309cc
5
5
  SHA512:
6
- metadata.gz: bb1b082c1cb33cdd35c951a29d44e480fa2e820b6116855df6326553ef678b4de9328a539f92e5d5b3d258be34288ad4f6c576de576ccef68315ee0fff68d435
7
- data.tar.gz: 401ffa1b6ad1780498e360701a274348c09215d55929caa3abe971d11d17724505fffd69742672b1aac5db96e90d58bff162de64e65a40f2b7cfa427e5d1238d
6
+ metadata.gz: f35fc1eae1c53765b4c1f931bb12b2fd5f8325f470d9315e2ed26485cdb79fd6604413e127e096f6a475332b720fcc53238d068c1f11433e61ae3e5c4fb83b87
7
+ data.tar.gz: f76fd8dff372e093470a0dda9b5aac075ddfdfd17f9e8f83759a90748677a54542988491e84e1c024a71f49e3b83d88ebcf64bfa48d81b62a0cc0c1197ea57b4
data/lib/gql.rb CHANGED
@@ -18,6 +18,7 @@ module GQL
18
18
  autoload :Raw
19
19
  autoload :String
20
20
  autoload :TestCase
21
+ autoload :Tokenizer
21
22
 
22
23
  module Errors
23
24
  extend ActiveSupport::Autoload
@@ -31,6 +32,7 @@ module GQL
31
32
  autoload :ScanError
32
33
  autoload :SyntaxError
33
34
  autoload :UndefinedNodeClass
35
+ autoload :VariableNotFound
34
36
  end
35
37
  end
36
38
 
@@ -41,8 +43,7 @@ module GQL
41
43
  autoload :Field
42
44
  autoload :List
43
45
  autoload :Parameter
44
- autoload :Placeholder
45
- autoload :Root
46
+ autoload :CallerClass
46
47
  end
47
48
 
48
49
  extend(Module.new {
@@ -56,7 +57,7 @@ module GQL
56
57
 
57
58
  %w(root_node_class root_target_proc field_types
58
59
  default_list_class default_field_proc
59
- default_call_proc).each do |method|
60
+ default_call_proc debug).each do |method|
60
61
  module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
61
62
  def #{method}
62
63
  config.#{method}
@@ -68,11 +69,11 @@ module GQL
68
69
  DELEGATORS
69
70
  end
70
71
 
71
- def execute(input, context = {})
72
+ def execute(input, context = {}, variables = {})
72
73
  query = parse(input)
73
74
 
74
75
  executor = Executor.new(query)
75
- executor.execute context
76
+ executor.execute context, variables
76
77
  end
77
78
 
78
79
  def parse(input)
@@ -80,10 +81,11 @@ module GQL
80
81
  end
81
82
 
82
83
  def tokenize(input)
83
- parser = Parser.new(input)
84
+ tokenizer = Tokenizer.new
85
+ tokenizer.scan_setup input
84
86
 
85
87
  [].tap do |result|
86
- while token = parser.next_token
88
+ while token = tokenizer.next_token
87
89
  result << token
88
90
  yield token if block_given?
89
91
  end
data/lib/gql/call.rb CHANGED
@@ -21,8 +21,14 @@ module GQL
21
21
 
22
22
  private
23
23
  def substitute_variables(args, variables)
24
- return args unless variables.any?
25
- args.map { |arg| arg.is_a?(::Symbol) ? variables[arg] : arg }
24
+ args.map { |arg| substitute_variable arg, variables }
25
+ end
26
+
27
+ def substitute_variable(arg, variables)
28
+ return arg unless arg.is_a?(::Symbol)
29
+ return variables[arg] if variables.has_key?(arg)
30
+
31
+ raise Errors::VariableNotFound, arg
26
32
  end
27
33
 
28
34
  def result_class_from_block(block)
data/lib/gql/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/class/subclasses'
2
+
1
3
  module GQL
2
4
  class Config
3
5
  def root_node_class
@@ -9,10 +11,6 @@ module GQL
9
11
  raise Errors::InvalidNodeClass.new(value, Node)
10
12
  end
11
13
 
12
- if value && ENV['DEBUG']
13
- value.call :schema, -> { context[:__schema_root] }, returns: Schema::Root
14
- end
15
-
16
14
  @@root_node_class = value
17
15
  end
18
16
 
@@ -66,5 +64,57 @@ module GQL
66
64
  def default_call_proc=(value)
67
65
  @@default_call_proc = value
68
66
  end
67
+
68
+ def debug
69
+ @@debug ||= ENV.has_key?('DEBUG')
70
+ end
71
+
72
+ def debug=(value)
73
+ value = !!value
74
+
75
+ return if value == debug
76
+
77
+ value ? switch_debug_on : switch_debug_off
78
+
79
+ @@debug = value
80
+ end
81
+
82
+ private
83
+ def switch_debug_on
84
+ switch_on_type_field
85
+ switch_on_execution_context
86
+ end
87
+
88
+ def switch_debug_off
89
+ switch_off_type_field
90
+ switch_off_execution_context
91
+ end
92
+
93
+ def switch_on_type_field
94
+ return if Node.fields.has_key? :__type__
95
+
96
+ [Node, *Node.descendants].each do |node_class|
97
+ node_class.object :__type__, -> { field_class }, node_class: Schema::Field
98
+ end
99
+ end
100
+
101
+ def switch_off_type_field
102
+ return unless Node.fields.has_key? :__type__
103
+
104
+ [Node, *Node.descendants].each do |node_class|
105
+ node_class.remove_field :__type__
106
+ end
107
+ end
108
+
109
+ def switch_on_execution_context
110
+ Node.send :remove_const, :ExecutionContext if Node.const_defined?(:ExecutionContext)
111
+ Node.const_set :ExecutionContext, Node::ExecutionContextDebug
112
+ end
113
+
114
+ def switch_off_execution_context
115
+ Node.send :remove_const, :ExecutionContext if Node.const_defined?(:ExecutionContext)
116
+ Node.const_set :ExecutionContext, Node::ExecutionContextNoDebug
117
+ end
118
+
69
119
  end
70
120
  end
data/lib/gql/errors.rb CHANGED
@@ -19,7 +19,7 @@ module GQL
19
19
  }
20
20
 
21
21
  result[:error][:handle] = handle.to_s if handle
22
- result[:error][:message] = message if ENV['DEBUG']
22
+ result[:error][:message] = message if GQL.debug
23
23
 
24
24
  result
25
25
  end
@@ -53,6 +53,14 @@ module GQL
53
53
  end
54
54
  end
55
55
 
56
+ class VariableNotFound < NotFoundError
57
+ def initialize(id)
58
+ msg = "The variable named `<#{id}>' has no value."
59
+
60
+ super(msg, 113, id)
61
+ end
62
+ end
63
+
56
64
  class InvalidNodeClass < Error
57
65
  def initialize(node_class, super_class)
58
66
  msg = "#{node_class} must be a (subclass of) #{super_class}."
@@ -72,7 +80,7 @@ module GQL
72
80
  msg << "If so, you have to register your field type first "
73
81
  msg << "like this: `GQL.field_types[:#{id}] = My#{id.to_s.camelize}'. "
74
82
  msg << "The following field types are currently registered: "
75
- msg << GQL.field_types.keys.sort.map { |id| "`#{id}'" }.to_sentence
83
+ msg << GQL.field_types.keys.sort.map { |key| "`#{key}'" }.to_sentence
76
84
 
77
85
  super(msg, 122)
78
86
  end
data/lib/gql/executor.rb CHANGED
@@ -7,12 +7,12 @@ module GQL
7
7
  @variables = ast_query.variables
8
8
  end
9
9
 
10
- def execute(context = {})
10
+ def execute(context = {}, vars = {})
11
11
  node_class = GQL.root_node_class
12
12
 
13
13
  raise Errors::RootClassNotSet if node_class.nil?
14
14
 
15
- context[:__schema_root] = node_class if ENV['DEBUG']
15
+ variables.update vars
16
16
 
17
17
  target = GQL.root_target_proc.call(context)
18
18
 
data/lib/gql/has_calls.rb CHANGED
@@ -14,7 +14,7 @@ module GQL
14
14
  end
15
15
 
16
16
  module ClassMethods
17
- def call(id, *args, &block)
17
+ def add_call(id, *args, &block)
18
18
  options = args.extract_options!
19
19
 
20
20
  call_spec = args.shift || block || proc_for_call(id)
@@ -27,11 +27,32 @@ module GQL
27
27
  call_class.id = id.to_s
28
28
  call_class.result_class = result_class
29
29
 
30
- self.const_set "#{id.to_s.camelize}Call", call_class
30
+ if result_class && result_class.name.nil?
31
+ call_class.const_set :Result, result_class
32
+ end
33
+
34
+ self.const_set const_name_for_call(id), call_class
31
35
  self.calls = calls.merge(id.to_sym => call_class)
32
36
  end
33
37
 
38
+ alias :call :add_call
39
+
40
+ def remove_call(id)
41
+ const_name = const_name_for_call(id)
42
+
43
+ send :remove_const, const_name if const_defined?(const_name)
44
+ calls.delete id
45
+ end
46
+
47
+ def has_call?(id)
48
+ calls.has_key? id
49
+ end
50
+
34
51
  private
52
+ def const_name_for_call(id)
53
+ :"#{id.to_s.camelize}Call"
54
+ end
55
+
35
56
  def proc_for_call(id)
36
57
  instance_exec id, &(call_proc || GQL.default_call_proc)
37
58
  end
@@ -10,10 +10,14 @@ module GQL
10
10
  included do
11
11
  class_attribute :fields, :field_proc, instance_accessor: false, instance_predicate: false
12
12
  self.fields = {}
13
+
14
+ object :__type__, -> { field_class }, node_class: Schema::Field if GQL.debug
15
+
16
+ const_set :ExecutionContext, GQL.debug ? ExecutionContextDebug : ExecutionContextNoDebug
13
17
  end
14
18
 
15
19
  module ClassMethods
16
- def field(id, *args, &block)
20
+ def add_field(id, *args, &block)
17
21
  options = args.extract_options!
18
22
  type = options.delete(:type) || Node
19
23
  proc = args.shift || block || proc_for_field(id)
@@ -21,16 +25,31 @@ module GQL
21
25
  Node.validate_is_subclass! type, 'type'
22
26
 
23
27
  type.build_class(id, proc, options).tap do |field_class|
24
- self.const_set "#{id.to_s.camelize}Field", field_class
28
+ const_name = const_name_for_field(id)
29
+
30
+ const_set const_name, field_class unless const_defined?(const_name)
25
31
  self.fields = fields.merge(id.to_sym => field_class)
26
32
  end
27
33
  end
28
34
 
35
+ alias :field :add_field
36
+
37
+ def remove_field(id)
38
+ const_name = const_name_for_field(id)
39
+
40
+ send :remove_const, const_name if const_defined?(const_name)
41
+ fields.delete id
42
+ end
43
+
44
+ def has_field?(id)
45
+ fields.has_key? id
46
+ end
47
+
29
48
  def cursor(id_or_proc)
30
49
  id = id_or_proc.is_a?(Proc) ? nil : id_or_proc
31
50
  proc = id ? -> { target.public_send(id) } : id_or_proc
32
51
 
33
- field :cursor, proc, type: Raw
52
+ add_field :cursor, proc, type: Raw
34
53
  end
35
54
 
36
55
  def respond_to?(method, *args)
@@ -49,6 +68,11 @@ module GQL
49
68
  end
50
69
 
51
70
  private
71
+ def const_name_for_field(id)
72
+ prefix = id == :__type__ ? 'Schema_Type' : id.to_s.camelize
73
+ :"#{prefix}Field"
74
+ end
75
+
52
76
  def proc_for_field(id)
53
77
  instance_exec id, &(field_proc || GQL.default_field_proc)
54
78
  end
@@ -58,7 +82,7 @@ module GQL
58
82
  options = args.extract_options!.merge(type: type)
59
83
  args = args.push(options)
60
84
 
61
- field(*args, &block)
85
+ add_field(*args, &block)
62
86
  end
63
87
  end
64
88
  end
@@ -90,11 +114,20 @@ module GQL
90
114
  end
91
115
 
92
116
  def target_for_field(current_target, proc)
93
- method = ExecutionContext.new(current_target, context)
117
+ args = [current_target, context]
118
+ args.push self.class if GQL.debug
119
+
120
+ method = self.class.const_get(:ExecutionContext).new(*args)
94
121
  method.execute proc
95
122
  end
96
123
 
97
- class ExecutionContext < Struct.new(:target, :context)
124
+ class ExecutionContextNoDebug < Struct.new(:target, :context)
125
+ def execute(method, args = [])
126
+ instance_exec(*args, &method)
127
+ end
128
+ end
129
+
130
+ class ExecutionContextDebug < Struct.new(:target, :context, :field_class)
98
131
  def execute(method, args = [])
99
132
  instance_exec(*args, &method)
100
133
  end
data/lib/gql/node.rb CHANGED
@@ -5,9 +5,6 @@ require 'gql/has_fields'
5
5
 
6
6
  module GQL
7
7
  class Node
8
- include HasCalls
9
- include HasFields
10
-
11
8
  class_attribute :id, :proc, instance_accessor: false, instance_predicate: false
12
9
 
13
10
  class << self
@@ -29,6 +26,9 @@ module GQL
29
26
  end
30
27
  end
31
28
 
29
+ include HasCalls
30
+ include HasFields
31
+
32
32
  attr_reader :ast_node, :target, :variables, :context
33
33
 
34
34
  def initialize(ast_node, target, variables, context)
data/lib/gql/parser.rb CHANGED
@@ -14,7 +14,7 @@ require 'active_support/core_ext/object/json'
14
14
  require 'gql/tokenizer'
15
15
 
16
16
  module GQL
17
- class Parser < Racc::Parser
17
+ class Parser < Tokenizer
18
18
 
19
19
  module_eval(<<'...end parser.racc/module_eval...', 'parser.racc', 136)
20
20
 
@@ -27,15 +27,6 @@ module_eval(<<'...end parser.racc/module_eval...', 'parser.racc', 136)
27
27
  end
28
28
  end
29
29
 
30
- class Root < Struct.new(:call, :fields)
31
- def as_json(*)
32
- {
33
- call: call.as_json,
34
- fields: fields.as_json
35
- }
36
- end
37
- end
38
-
39
30
  class Node < Struct.new(:id, :alias_id, :call, :fields)
40
31
  def as_json(*)
41
32
  {
@@ -310,14 +301,14 @@ module_eval(<<'.,.,', 'parser.racc', 4)
310
301
 
311
302
  module_eval(<<'.,.,', 'parser.racc', 8)
312
303
  def _reduce_2(val, _values, result)
313
- result = Root.new(val[0], nil )
304
+ result = Node.new('[root]', nil, val[0], nil )
314
305
  result
315
306
  end
316
307
  .,.,
317
308
 
318
309
  module_eval(<<'.,.,', 'parser.racc', 9)
319
310
  def _reduce_3(val, _values, result)
320
- result = Root.new(nil, val[1])
311
+ result = Node.new('[root]', nil, nil, val[1])
321
312
  result
322
313
  end
323
314
  .,.,
@@ -4,8 +4,13 @@ module GQL
4
4
  cursor :id
5
5
 
6
6
  string :id
7
- object :result_class, -> { target.result_class || Placeholder }, node_class: Root
7
+ string :name
8
+ object :result_class, -> { target.result_class || CallerClass }, node_class: Field
8
9
  array :parameters, -> { (target.proc || target.instance_method(:execute)).parameters }, item_class: Parameter
10
+
11
+ def raw_value
12
+ target.name
13
+ end
9
14
  end
10
15
  end
11
16
  end
@@ -1,6 +1,6 @@
1
1
  module GQL
2
2
  module Schema
3
- class Placeholder < GQL::Node
3
+ class CallerClass < GQL::Node
4
4
  end
5
5
  end
6
6
  end
@@ -4,8 +4,13 @@ module GQL
4
4
  cursor :id
5
5
 
6
6
  string :id
7
+ string :name
7
8
  connection :calls, -> { target.calls.values }, list_class: List, item_class: Call
8
9
  connection :fields, -> { target.fields.values }, list_class: List, item_class: Field
10
+
11
+ def raw_value
12
+ target.name
13
+ end
9
14
  end
10
15
  end
11
16
  end
data/lib/gql/tokenizer.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  #++
6
6
 
7
7
  require 'racc/parser'
8
- class GQL::Parser < Racc::Parser
8
+ class GQL::Tokenizer < Racc::Parser
9
9
  require 'strscan'
10
10
 
11
11
  class Unused < StandardError ; end
data/lib/gql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module GQL
2
- VERSION = '0.0.15'
2
+ VERSION = '0.0.16'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Andert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-11 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -119,11 +119,10 @@ files:
119
119
  - lib/gql/parser.rb
120
120
  - lib/gql/raw.rb
121
121
  - lib/gql/schema/call.rb
122
+ - lib/gql/schema/caller_class.rb
122
123
  - lib/gql/schema/field.rb
123
124
  - lib/gql/schema/list.rb
124
125
  - lib/gql/schema/parameter.rb
125
- - lib/gql/schema/placeholder.rb
126
- - lib/gql/schema/root.rb
127
126
  - lib/gql/string.rb
128
127
  - lib/gql/test_case.rb
129
128
  - lib/gql/tokenizer.rb
@@ -1,14 +0,0 @@
1
- module GQL
2
- module Schema
3
- class Root < GQL::Node
4
- string :name
5
-
6
- connection :calls, -> { target.calls.values }, list_class: List, item_class: Call
7
- connection :fields, -> { target.fields.values }, list_class: List, item_class: Field
8
-
9
- def raw_value
10
- target.name
11
- end
12
- end
13
- end
14
- end