Class: Mirah::BaseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/mirah/base_object.rb

Overview

This object provides a DSL by which you can easy create plain ruby objects with ‘attr_accessor` that will automatically be transformed back and forth from a GraphQL endpoint. This includes changing the format of the key from `ruby_style` to `graphqlStyle` and back again, and extra serialization where necessary for scalar types such as dates.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ BaseObject

Returns a new instance of BaseObject.



9
10
11
12
13
# File 'lib/mirah/base_object.rb', line 9

def initialize(attrs = {})
  attrs.each do |key, value|
    send("#{key}=", value)
  end
end

Class Method Details

.from_graphql_hash(graphql_data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/mirah/base_object.rb', line 22

def self.from_graphql_hash(graphql_data)
  return nil unless graphql_data

  attrs = attributes.each_with_object({}) do |attribute, obj|
    value = read_value(graphql_data, attribute)
    obj[attribute[:name]] = attribute[:serializer].deserialize(value)
  end

  new(attrs)
end

Instance Method Details

#to_graphql_hashObject



15
16
17
18
19
20
# File 'lib/mirah/base_object.rb', line 15

def to_graphql_hash
  self.class.attributes.each_with_object({}) do |attribute, obj|
    value = attribute[:serializer].serialize(send(attribute[:name]))
    write_value(obj, value, attribute)
  end
end