Class: GraphQL::Client::QueryResult
- Inherits:
-
Object
- Object
- GraphQL::Client::QueryResult
show all
- Defined in:
- lib/graphql/client/query_result.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute data.
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
95
96
97
98
99
|
# File 'lib/graphql/client/query_result.rb', line 95
def method_missing(*args)
super
rescue NoMethodError => e
raise NoMethodError, "undefined method `#{e.name}' for #{inspect}"
end
|
Instance Attribute Details
#data ⇒ Object
Also known as:
to_h
Returns the value of attribute data.
83
84
85
|
# File 'lib/graphql/client/query_result.rb', line 83
def data
@data
end
|
Class Method Details
.cast(obj) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/graphql/client/query_result.rb', line 59
def self.cast(obj)
case obj
when Hash
new(obj)
when QueryResult
cast(obj.to_h)
when Array
obj.map { |e| cast(e) }
when NilClass
nil
else
raise TypeError, "#{obj.class}"
end
end
|
.define(fields: {}) ⇒ Object
11
12
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
47
48
49
50
51
52
53
|
# File 'lib/graphql/client/query_result.rb', line 11
def self.define(fields: {})
Class.new(self) do
@fields = {}
fields.each do |field, type|
@fields[field.to_sym] = type
send :attr_reader, field
field_alias = ActiveSupport::Inflector.underscore(field)
if field != field_alias
send :alias_method, field_alias, field
end
class_eval <<-RUBY, __FILE__, __LINE__
def #{field_alias}?
#{field_alias} ? true : false
end
RUBY
end
assigns = fields.map do |field, type|
if type
<<-RUBY
@#{field} = self.class.fields[:#{field}].cast(@data["#{field}"])
RUBY
else
<<-RUBY
@#{field} = @data["#{field}"]
RUBY
end
end
class_eval <<-RUBY, __FILE__, __LINE__
def initialize(data)
@data = data
#{assigns.join("\n")}
freeze
end
RUBY
end
end
|
.fields ⇒ Object
7
8
9
|
# File 'lib/graphql/client/query_result.rb', line 7
def self.fields
@fields
end
|
.inspect ⇒ Object
55
56
57
|
# File 'lib/graphql/client/query_result.rb', line 55
def self.inspect
"#<GraphQL::Client::QueryResult fields=#{@fields.keys.inspect}>"
end
|
.new(obj) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/graphql/client/query_result.rb', line 74
def self.new(obj)
case obj
when Hash
super
else
cast(obj)
end
end
|
Instance Method Details
#inspect ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/graphql/client/query_result.rb', line 86
def inspect
ivars = (self.class.fields.keys - [:__typename]).map { |sym| "#{sym}=#{instance_variable_get("@#{sym}").inspect}" }
buf = "#<GraphQL::Client::QueryResult"
buf << " " << @__typename if @__typename
buf << " " << ivars.join(" ") if ivars.any?
buf << ">"
buf
end
|