Class: ZeroRailsAdapter::Mutation

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_rails_adapter/mutation.rb

Constant Summary collapse

REQUIRED_KEYS =
%w[type id clientID name args timestamp].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, id:, client_id:, name:, args:, timestamp:) ⇒ Mutation

Returns a new instance of Mutation.



33
34
35
36
37
38
39
40
# File 'lib/zero_rails_adapter/mutation.rb', line 33

def initialize(type:, id:, client_id:, name:, args:, timestamp:)
  @type = type
  @id = id
  @client_id = client_id
  @name = name
  @args = args
  @timestamp = timestamp
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/zero_rails_adapter/mutation.rb', line 7

def args
  @args
end

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/zero_rails_adapter/mutation.rb', line 7

def client_id
  @client_id
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/zero_rails_adapter/mutation.rb', line 7

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/zero_rails_adapter/mutation.rb', line 7

def name
  @name
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/zero_rails_adapter/mutation.rb', line 7

def timestamp
  @timestamp
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/zero_rails_adapter/mutation.rb', line 7

def type
  @type
end

Class Method Details

.parse(value) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zero_rails_adapter/mutation.rb', line 9

def self.parse(value)
  unless value.is_a?(Hash)
    raise ParseError, "Mutation must be an object"
  end

  missing = REQUIRED_KEYS.reject { |key| value.key?(key) }
  raise ParseError, "Mutation is missing #{missing.join(', ')}" if missing.any?
  raise ParseError, "Mutation type must be custom" unless value["type"] == "custom"
  raise ParseError, "Mutation id must be a number" unless value["id"].is_a?(Numeric)
  raise ParseError, "Mutation clientID must be a string" unless value["clientID"].is_a?(String)
  raise ParseError, "Mutation name must be a string" unless value["name"].is_a?(String)
  raise ParseError, "Mutation args must be an array" unless value["args"].is_a?(Array)
  raise ParseError, "Mutation timestamp must be a number" unless value["timestamp"].is_a?(Numeric)

  new(
    type: value["type"],
    id: value["id"],
    client_id: value["clientID"],
    name: value["name"],
    args: value["args"],
    timestamp: value["timestamp"]
  )
end

Instance Method Details

#argumentObject



42
43
44
# File 'lib/zero_rails_adapter/mutation.rb', line 42

def argument
  args.first
end

#identifierObject



46
47
48
# File 'lib/zero_rails_adapter/mutation.rb', line 46

def identifier
  {"clientID" => client_id, "id" => id}
end