module Bronze::Entities::Attributes
Module for defining attributes on an entity class.
Public Class Methods
@param attributes [Hash] The default attributes with which to initialize
the entity. Defaults to an empty hash.
# File lib/bronze/entities/attributes.rb, line 99 def initialize(attributes = {}) initialize_attributes(attributes) end
Public Instance Methods
Compares with the other object.
If the other object is a Hash, returns true if the entity attributes hash is equal to the given hash. Otherwise, returns true if the other object has the same class and attributes as the entity.
@param other [Bronze::Entities::Attributes, Hash] The object to compare.
@return [Boolean] true if the other object matches the entity, otherwise
false.
# File lib/bronze/entities/attributes.rb, line 113 def ==(other) return attributes == other if other.is_a?(Hash) other.class == self.class && other.attributes == attributes end
Updates the attributes with the given hash. If an attribute is not in the hash, it is unchanged.
@raise ArgumentError if one of the keys is not a valid attribute
# File lib/bronze/entities/attributes.rb, line 123 def assign_attributes(hash) validate_attributes(hash) self.class.each_attribute do |name, metadata| next if metadata.read_only? next unless hash.key?(name) || hash.key?(name.to_s) set_attribute(name, hash[name] || hash[name.to_s]) end end
@return true if the entity has an attribute with the given name, otherwise
false.
# File lib/bronze/entities/attributes.rb, line 137 def attribute?(name) attribute_name = name.intern self.class.each_attribute.any? { |key, _metadata| key == attribute_name } rescue NoMethodError raise ArgumentError, "invalid attribute #{name.inspect}" end
Returns the current value of each attribute.
@return [Hash{Symbol => Object}] the attribute values.
# File lib/bronze/entities/attributes.rb, line 148 def attributes each_attribute.with_object({}) do |attr_name, hsh| hsh[attr_name] = get_attribute(attr_name) end end
Replaces the attributes with the given hash. If a non-primary key attribute is not in the hash, it is set to nil.
@raise ArgumentError if one of the keys is not a valid attribute
# File lib/bronze/entities/attributes.rb, line 158 def attributes=(hash) validate_attributes(hash) self.class.each_attribute do |name, metadata| next if metadata.primary_key? @attributes[name] = hash[name] || hash[name.to_s] end end
@param name [String] The name of the attribute.
@return [Object] the value of the given attribute.
@raise ArgumentError when the attribute name is not a valid attribute
# File lib/bronze/entities/attributes.rb, line 173 def get_attribute(name) unless attribute?(name) raise ArgumentError, "invalid attribute #{name.inspect}" end @attributes[name.intern] end
@return [String] a human-readable representation of the entity, composed
of the class name and the attribute keys and values.
# File lib/bronze/entities/attributes.rb, line 183 def inspect # rubocop:disable Metrics/AbcSize buffer = +'#<' buffer << self.class.name each_attribute.with_index do |(name, _metadata), index| buffer << ',' unless index.zero? buffer << ' ' << name.to_s << ': ' << get_attribute(name).inspect end buffer << '>' end
@param name [String] The name of the attribute. @param value [Object] The new value of the attribute.
@return [Object] the new value of the given attribute.
@raise ArgumentError when the attribute name is not a valid attribute
# File lib/bronze/entities/attributes.rb, line 199 def set_attribute(name, value) unless attribute?(name) raise ArgumentError, "invalid attribute #{name.inspect}" end @attributes[name.intern] = value end
Private Instance Methods
# File lib/bronze/entities/attributes.rb, line 209 def each_attribute return enum_for(:each_attribute) unless block_given? self.class.each_attribute { |name, _metadata| yield(name) } end
# File lib/bronze/entities/attributes.rb, line 215 def initialize_attributes(data) @attributes = {} validate_attributes(data) self.class.each_attribute do |name, metadata| name = name.intern if name.is_a?(String) value = data[name] || data[name.to_s] || metadata.default @attributes[name] = value end end
# File lib/bronze/entities/attributes.rb, line 228 def validate_attributes(obj) unless obj.is_a?(Hash) raise ArgumentError, "expected attributes to be a Hash, but was #{obj.inspect}" end obj.each_key do |name| unless attribute?(name) raise ArgumentError, "invalid attribute #{name.inspect}" end end end