Class: GraphQL::Client::QueryResult
- Inherits:
-
Object
- Object
- GraphQL::Client::QueryResult
- 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
- .cast(obj) ⇒ Object
- .define(fields: {}) ⇒ Object
- .fields ⇒ Object
- .inspect ⇒ Object
- .new(obj) ⇒ Object
- .|(other) ⇒ Object
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
117 118 119 120 121 |
# File 'lib/graphql/client/query_result.rb', line 117 def method_missing(*args) super rescue NoMethodError => e raise NoMethodError, "undefined method `#{e.name}' for #{inspect}" end |
Instance Attribute Details
#data ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute data.
105 106 107 |
# File 'lib/graphql/client/query_result.rb', line 105 def data @data end |
Class Method Details
.cast(obj) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/graphql/client/query_result.rb', line 69 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 54 55 56 57 58 59 60 61 62 63 |
# 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 # Convert GraphQL camelcase to snake case: commitComments -> commit_comments 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 if field == "edges" class_eval <<-RUBY, __FILE__, __LINE__ def each_node return enum_for(:each_node) unless block_given? edges.each { |edge| yield edge.node } self end RUBY end 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
65 66 67 |
# File 'lib/graphql/client/query_result.rb', line 65 def self.inspect "#<GraphQL::Client::QueryResult fields=#{@fields.keys.inspect}>" end |
.new(obj) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/graphql/client/query_result.rb', line 84 def self.new(obj) case obj when Hash super else cast(obj) end end |
.|(other) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/graphql/client/query_result.rb', line 93 def self.|(other) new_fields = self.fields.dup other.fields.each do |name, value| if new_fields[name] new_fields[name] |= value else new_fields[name] = value end end define(fields: new_fields) end |
Instance Method Details
#inspect ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/graphql/client/query_result.rb', line 108 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 |