gql 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ab57728a3f404a0dc03938f49211795ec0919f9
4
- data.tar.gz: b56f90154a8ee4253c76f66404861c53b18254b0
3
+ metadata.gz: 0b8d0bd13d8d50371e8a27403e0939d0c313c816
4
+ data.tar.gz: 854371b7797057ad99cb47a1e04ab912cf6aa7d0
5
5
  SHA512:
6
- metadata.gz: 22ee10af77dc8cd90b0692b1ca4c91ff36a0bb71e3bd11f0412fc2a9622c784475f38985e6c055e8339a6cc22b7b794ef9309373fbf8c8b7940d67aa18d718c8
7
- data.tar.gz: f4c135e4a7325589051a06004a166ab3cd198ce41077da5cfce3673dc76150968faa5f2db98057ad744e78575ab1ee2c247cd69b509f3f185a6bc7f9d3e0b8e3
6
+ metadata.gz: d52b45346facafa3c994dce27ffc6f8d708a4a3c26fb9878ee013fd904c993fa5ece24d6ee38e79fd1007f029064c2cece59fe0b55beac711f0af2d8dd599bf5
7
+ data.tar.gz: 18fa4cd86fbb2c6a094f77dc00fa68efd82f5efd6f4b1831b0b7973fae416e5d417d2f808f0fbe70885879515d2d2431cfadca6ff74bb76ab98dfa24ed8aeeb4
@@ -7,21 +7,23 @@ module GQL
7
7
  class << self
8
8
  def build_class(id, proc, options = {})
9
9
  item_class = options.delete(:item_class) || self.item_class
10
- item_class = ::Hash.new(item_class) unless item_class.is_a?(::Hash)
10
+ item_class = Object.build_class(:item, -> { target }, object_class: item_class)
11
11
 
12
12
  Class.new(self).tap do |field_class|
13
13
  field_class.id = id
14
14
  field_class.proc = proc
15
15
  field_class.item_class = item_class
16
+
17
+ if item_class && item_class.name.nil?
18
+ field_class.const_set :Item, item_class
19
+ end
16
20
  end
17
21
  end
18
22
  end
19
23
 
20
24
  def value
21
25
  target.map do |item|
22
- field_class = Registry.fetch(self.class.item_class[item.class])
23
-
24
- field = field_class.new(ast_node, item, variables, context)
26
+ field = self.class.item_class.new(ast_node, item, variables, context)
25
27
  field.value
26
28
  end
27
29
  end
@@ -19,6 +19,10 @@ module GQL
19
19
  result.value
20
20
  end
21
21
 
22
+ def parameters
23
+ (proc || instance_method(:execute)).parameters
24
+ end
25
+
22
26
  private
23
27
  def substitute_variables(args, variables)
24
28
  args.map { |arg| substitute_variable arg, variables }
@@ -8,14 +8,6 @@ module GQL
8
8
  @@root_class = value
9
9
  end
10
10
 
11
- def root_target_proc
12
- @@root_target_proc ||= -> context { nil }
13
- end
14
-
15
- def root_target_proc=(value)
16
- @@root_target_proc = value
17
- end
18
-
19
11
  def field_types
20
12
  @@field_types ||= {
21
13
  array: 'GQL::Array',
@@ -39,6 +31,14 @@ module GQL
39
31
  @@default_list_class = value
40
32
  end
41
33
 
34
+ def root_target_proc
35
+ @@root_target_proc ||= -> { nil }
36
+ end
37
+
38
+ def root_target_proc=(value)
39
+ @@root_target_proc = value
40
+ end
41
+
42
42
  def default_field_proc
43
43
  @@default_field_proc ||= -> id { -> { target.public_send(id) } }
44
44
  end
@@ -71,23 +71,13 @@ module GQL
71
71
 
72
72
  return if value == @@debug
73
73
 
74
- value ? switch_debug_on : switch_debug_off
75
-
76
- @@debug = value
77
- end
78
-
79
- private
80
- def switch_debug_on
81
- Field.object :__type__, -> { field_class }, object_class: Schema::Field
82
- Field.send :remove_const, :ExecutionContext if Field.const_defined?(:ExecutionContext)
83
- Field.const_set :ExecutionContext, Field::ExecutionContextDebug
84
- end
85
-
86
- def switch_debug_off
74
+ if value
75
+ Field.object :__type__, -> { field_class }, object_class: 'GQL::Schema::Field'
76
+ else
87
77
  Field.remove_field :__type__
88
- Field.send :remove_const, :ExecutionContext if Field.const_defined?(:ExecutionContext)
89
- Field.const_set :ExecutionContext, Field::ExecutionContextNoDebug
90
78
  end
91
79
 
80
+ @@debug = value
81
+ end
92
82
  end
93
83
  end
@@ -71,8 +71,8 @@ module GQL
71
71
  end
72
72
 
73
73
  class InvalidClass < Error
74
- def initialize(node_class, super_class)
75
- msg = "#{node_class} must be a (subclass of) #{super_class}."
74
+ def initialize(klass, baseclass)
75
+ msg = "#{klass} must be a (subclass of) #{baseclass}."
76
76
 
77
77
  super(msg, 123)
78
78
  end
@@ -8,16 +8,12 @@ module GQL
8
8
  end
9
9
 
10
10
  def execute(context = {}, vars = {})
11
- field_class = GQL.root_class
11
+ raise Errors::RootClassNotSet unless GQL.root_class
12
12
 
13
- raise Errors::RootClassNotSet if field_class.nil?
14
-
15
- variables.update vars
16
-
17
- target = GQL.root_target_proc.call(context)
18
-
19
- field = Registry.fetch(field_class).new(ast_root, target, variables, context)
20
- field.value
13
+ root_class = Registry.fetch(GQL.root_class)
14
+ root_class.id = ast_root.id
15
+ root_class.proc = GQL.root_target_proc
16
+ root_class.execute self.class, ast_root, nil, variables.merge(vars), context
21
17
  end
22
18
  end
23
19
  end
@@ -6,6 +6,8 @@ require 'gql/mixins/has_fields'
6
6
 
7
7
  module GQL
8
8
  class Field
9
+ ExecutionContext = Struct.new(:target, :context, :field_class)
10
+
9
11
  class_attribute :id, :proc, instance_accessor: false, instance_predicate: false
10
12
 
11
13
  class << self
@@ -15,6 +17,14 @@ module GQL
15
17
  field_class.proc = proc
16
18
  end
17
19
  end
20
+
21
+ def execute(parent, ast_node, target, variables, context)
22
+ args = [target, context, GQL.debug ? parent : nil]
23
+ target = ExecutionContext.new(*args).instance_exec(&proc)
24
+ field = new(ast_node, target, variables, context)
25
+
26
+ field.value
27
+ end
18
28
  end
19
29
 
20
30
  extend Mixins::Common
@@ -80,8 +80,8 @@ module GQL
80
80
  case spec
81
81
  when ::Array
82
82
  result_class_from_connection_spec spec.dup
83
- when ::Hash
84
- result_class_from_mapping_spec spec.dup
83
+ when ::Hash, ::Proc
84
+ result_class_from_object_spec spec.dup
85
85
  else
86
86
  spec
87
87
  end
@@ -102,7 +102,7 @@ module GQL
102
102
  Connection.build_class :result, nil, options
103
103
  end
104
104
 
105
- def result_class_from_mapping_spec(spec)
105
+ def result_class_from_object_spec(spec)
106
106
  Object.build_class :result, nil, object_class: spec
107
107
  end
108
108
  end
@@ -110,7 +110,7 @@ module GQL
110
110
  private
111
111
  def value_of_call(ast_call)
112
112
  call_class = call_class_for_id(ast_call.id)
113
- call_class.execute(self.class, ast_call, target, variables, context)
113
+ call_class.execute self.class, ast_call, target, variables, context
114
114
  end
115
115
 
116
116
  def call_class_for_id(id)
@@ -126,10 +126,22 @@ module GQL
126
126
  end
127
127
 
128
128
  def execute(caller_class, ast_node, target, variables, context)
129
- real_call_class = Registry.fetch(call_class_name, Call)
130
- call_class = caller_class.add_call(id, real_call_class, returns: result_class)
131
- call_class.execute caller_class, ast_node, target, variables, context
129
+ spurred = spur_call_class(caller_class, id)
130
+ spurred.execute caller_class, ast_node, target, variables, context
132
131
  end
132
+
133
+ def parameters
134
+ call_class.parameters
135
+ end
136
+
137
+ private
138
+ def call_class
139
+ @call_class ||= Registry.fetch(call_class_name, Call)
140
+ end
141
+
142
+ def spur_call_class(caller_class, id)
143
+ caller_class.add_call id, call_class, returns: result_class
144
+ end
133
145
  end
134
146
  end
135
147
  end
@@ -103,36 +103,13 @@ module GQL
103
103
  field.value
104
104
  else
105
105
  field_class = field_class_for_id(ast_field.id)
106
- next_target = target_for_field(target, field_class.proc)
107
-
108
- field = field_class.new(ast_field, next_target, variables, context)
109
- field.value
106
+ field_class.execute self.class, ast_field, target, variables, context
110
107
  end
111
108
  end
112
109
 
113
110
  def field_class_for_id(id)
114
111
  self.class.fields[id] or raise Errors::FieldNotFound.new(id, self.class)
115
112
  end
116
-
117
- def target_for_field(current_target, proc)
118
- args = [current_target, context]
119
- args.push self.class if GQL.debug
120
-
121
- method = self.class.const_get(:ExecutionContext).new(*args)
122
- method.execute proc
123
- end
124
-
125
- class ExecutionContextNoDebug < Struct.new(:target, :context)
126
- def execute(method, args = [])
127
- instance_exec(*args, &method)
128
- end
129
- end
130
-
131
- class ExecutionContextDebug < Struct.new(:target, :context, :field_class)
132
- def execute(method, args = [])
133
- instance_exec(*args, &method)
134
- end
135
- end
136
113
  end
137
114
  end
138
115
  end
@@ -1,6 +1,6 @@
1
1
  module GQL
2
2
  class Number < Scalar
3
3
  # This is just an example call. Monkeypatch class to add your own.
4
- call :is_zero, -> { target.zero? }, returns: Boolean
4
+ call :is_zero, -> { target.zero? }, returns: 'GQL::Boolean'
5
5
  end
6
6
  end
@@ -2,26 +2,51 @@ require 'active_support/core_ext/class/attribute'
2
2
 
3
3
  module GQL
4
4
  class Object < Field
5
- class_attribute :object_class, instance_accessor: false, instance_predicate: false
5
+ class_attribute :object_proc, instance_accessor: false, instance_predicate: false
6
6
 
7
7
  class << self
8
8
  def build_class(id, proc, options = {})
9
9
  object_class = options.delete(:object_class) || options.delete(:as)
10
- object_class = ::Hash.new(object_class) unless object_class.is_a?(::Hash)
10
+ object_proc = object_proc_for_class(object_class)
11
11
 
12
12
  Class.new(self).tap do |klass|
13
13
  klass.id = id
14
14
  klass.proc = proc
15
- klass.object_class = object_class
15
+ klass.object_proc = object_proc
16
16
  end
17
17
  end
18
+
19
+ private
20
+ def object_proc_for_class(object_class)
21
+ case object_class
22
+ when ::Hash
23
+ -> target { object_class[target.class] }
24
+ when ::Class, ::String
25
+ -> _ { object_class }
26
+ when ::Proc
27
+ object_class
28
+ else
29
+ nil # raise error?
30
+ end
31
+ end
18
32
  end
19
33
 
20
34
  def value
21
- field_class = Registry.fetch(self.class.object_class[target.class])
35
+ field_class = Registry.fetch(object_proc_result)
22
36
 
23
37
  field = field_class.new(ast_node, target, variables, context)
24
38
  field.value
25
39
  end
40
+
41
+ private
42
+ def object_proc_result
43
+ proc = self.class.object_proc
44
+
45
+ if proc.arity == 1
46
+ proc.call target
47
+ else
48
+ proc.call target, context
49
+ end
50
+ end
26
51
  end
27
52
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/object/try'
2
+
1
3
  module GQL
2
4
  module Schema
3
5
  class Call < GQL::Field
@@ -6,7 +8,7 @@ module GQL
6
8
  string :id
7
9
  string :name
8
10
  object :result_class, -> { target.result_class || CallerClass }, object_class: Field
9
- array :parameters, -> { (target.proc || target.instance_method(:execute)).parameters }, item_class: Parameter
11
+ array :parameters, -> { target.parameters }, item_class: Parameter
10
12
 
11
13
  def scalar_value
12
14
  target.name
@@ -1,11 +1,19 @@
1
1
  module GQL
2
2
  module Schema
3
3
  class List < GQL::Field
4
+ RESULT_PROC = -> target, _ { target.superclass == GQL::Call ? 'GQL::Schema::Call' : 'GQL::Schema::Field' }
5
+
4
6
  call :count, returns: GQL::Number
5
7
  number :count
6
8
 
7
9
  call :reverse
8
10
  call :first, -> size { target.first(size) }
11
+
12
+ call :find, returns: RESULT_PROC do |id|
13
+ item = target.find { |item| item.id.to_s == id.to_s }
14
+ raise(GQL::Error, "id not found: #{id}") unless item
15
+ item.respond_to?(:spur) ? item.spur : item
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -3,6 +3,6 @@ module GQL
3
3
  # These are just example calls. Monkeypatch class to add your own.
4
4
  call :upcase
5
5
  call :downcase
6
- call :length, returns: Number
6
+ call :length, returns: 'GQL::Number'
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module GQL
2
- VERSION = '0.0.21'
2
+ VERSION = '0.0.22'
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.21
4
+ version: 0.0.22
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-14 00:00:00.000000000 Z
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake