class Symgate::Type
base class for API types, that provides magic initialisation from hash plus assignment and comparison operators
Public Class Methods
hash_value_with_optional_namespace(namespace, key, hash)
click to toggle source
# 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
new(opts = {})
click to toggle source
# 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
Public Instance Methods
==(other)
click to toggle source
# 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
Protected Instance Methods
attributes()
click to toggle source
override this to return an array of symbols for your class variables :nocov:
# File lib/symgate/type.rb, line 38 def attributes raise Symgate::Error, "No attributes defined for object type #{self.class.name}" end
value_or_nil(value)
click to toggle source
:nocov:
# File lib/symgate/type.rb, line 43 def value_or_nil(value) if value.respond_to?(:empty?) ? value.empty? : !value nil else value end end
values_are_empty(values)
click to toggle source
# File lib/symgate/type.rb, line 51 def values_are_empty(values) values.all? { |v| value_or_nil(v).nil? } end