Class: Symgate::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/symgate/type.rb

Overview

base class for API types, that provides magic initialisation from hash plus assignment and comparison operators

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Type

Returns a new instance of Type.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/symgate/type.rb', line 7

def initialize(opts = {})
  attrs = attributes
  self.class.class_eval { attr_accessor(*attrs) }
  opts.each do |key, _value|
    unless attributes.include? key
      raise Symgate::Error, "Unknown option #{key} for #{self.class.name}"
    end
  end

  attributes.each do |attribute|
    instance_variable_set "@#{attribute}", opts[attribute]
  end
end

Class Method Details

.hash_value_with_optional_namespace(namespace, key, hash) ⇒ Object



30
31
32
# File 'lib/symgate/type.rb', line 30

def self.hash_value_with_optional_namespace(namespace, key, hash)
  hash[key] || hash["#{namespace}:#{key}".to_sym]
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/symgate/type.rb', line 21

def ==(other)
  attributes.all? do |attribute|
    a = other.instance_variable_get("@#{attribute}")
    b = instance_variable_get("@#{attribute}")

    a == b || values_are_empty([a, b])
  end
end