Module: Bronze::Entities::Attributes

Extended by:
SleepingKingStudios::Tools::Toolbox::Mixin
Included in:
Bronze::Entity
Defined in:
lib/bronze/entities/attributes.rb,
lib/bronze/entities/attributes/builder.rb,
lib/bronze/entities/attributes/metadata.rb

Overview

Module for defining attributes on an entity class.

Defined Under Namespace

Modules: ClassMethods Classes: Builder, Metadata

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

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.



113
114
115
116
117
# 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

#assign_attributes(hash) ⇒ Object Also known as: assign

Updates the attributes with the given hash. If an attribute is not in the hash, it is unchanged.

Raises:

  • ArgumentError if one of the keys is not a valid attribute



123
124
125
126
127
128
129
130
131
132
# File 'lib/bronze/entities/attributes.rb', line 123

def assign_attributes(hash)
  validate_attributes(hash)

  self.class.each_attribute do |name, |
    next if .read_only?
    next unless hash.key?(name) || hash.key?(name.to_s)

    set_attribute(name, hash[name] || hash[name.to_s])
  end
end

#attribute?(name) ⇒ Boolean



137
138
139
140
141
142
143
# File 'lib/bronze/entities/attributes.rb', line 137

def attribute?(name)
  attribute_name = name.intern

  self.class.each_attribute.any? { |key, | key == attribute_name }
rescue NoMethodError
  raise ArgumentError, "invalid attribute #{name.inspect}"
end

#attributesHash{Symbol => Object}

Returns the current value of each attribute.



148
149
150
151
152
# 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

#attributes=(hash) ⇒ Object

Replaces the attributes with the given hash. If a non-primary key attribute is not in the hash, it is set to nil.

Raises:

  • ArgumentError if one of the keys is not a valid attribute



158
159
160
161
162
163
164
165
166
# File 'lib/bronze/entities/attributes.rb', line 158

def attributes=(hash)
  validate_attributes(hash)

  self.class.each_attribute do |name, |
    next if .primary_key?

    @attributes[name] = hash[name] || hash[name.to_s]
  end
end

#get_attribute(name) ⇒ Object

Returns the value of the given attribute.

Raises:

  • ArgumentError when the attribute name is not a valid attribute



173
174
175
176
177
178
179
# 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

#initialize(attributes = {}) ⇒ Object



99
100
101
# File 'lib/bronze/entities/attributes.rb', line 99

def initialize(attributes = {})
  initialize_attributes(attributes)
end

#inspectString



183
184
185
186
187
188
189
190
191
# 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, ), index|
    buffer << ',' unless index.zero?
    buffer << ' ' << name.to_s << ': ' << get_attribute(name).inspect
  end
  buffer << '>'
end

#set_attribute(name, value) ⇒ Object

Returns the new value of the given attribute.

Raises:

  • ArgumentError when the attribute name is not a valid attribute



199
200
201
202
203
204
205
# 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