module ShallowAttributes::InstanceMethods

Abstract class for value classes. Provides some helper methods for working with attributes.

@abstract

@since 0.1.0

Constants

TO_H_PROC

Lambda object for getting attributes hash for a specific value object.

@private

@since 0.1.0

Public Class Methods

new(attrs = {}) click to toggle source

Initialize an instance object with specific attributes

@param [Hash] attrs the attributes contained in the class

@example Create new User instance

class User
  include ShallowAttributes
  attribute :name, String
end

User.new(name: 'Anton') # => #<User @attributes={:name=>"Anton"}, @name="Anton">

@return the new instance of value class with specific attributes

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 32
def initialize(attrs = {})
  @attributes = {}
  attrs.each_pair do |key, value|
    key = key.to_sym
    @attributes[key] = value if default_values.key?(key)
  end
  define_attributes
  define_default_attributes
  define_mandatory_attributes
end

Public Instance Methods

==(object) click to toggle source

Compare values of two objects

@param [Object] object the other object

@example Compare two value objects

class User
  include ShallowAttributes
  attribute :name, String, default: 'Ben'
end

user1 = User.new(name: 'Anton')
user2 = User.new(name: 'Anton')
user1 == user2 # => true

@return [boolean]

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 163
def ==(object)
  self.to_h == object.to_h
end
attributes() click to toggle source

Returns hash of object attributes

@example Returns all user attributes

class User
  include ShallowAttributes
  attribute :name, String
end

user = User.new(name: 'Anton')
user.attributes # => { name: "Anton" }

@return [Hash]

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 57
def attributes
  hash = {}
  @attributes.map do |key, value|
    hash[key] =
      value.is_a?(Array) ? value.map(&TO_H_PROC) : TO_H_PROC.call(value)
  end
  hash
end
Also aliased as: to_h, to_hash
attributes=(attributes) click to toggle source

Attribute values mass-assignment

@param [Hash] attributes the attributes which will be assignment

@example Assignment new user name

class User
  include ShallowAttributes
  attribute :name, String
end

user = User.new(name: 'Anton')
user.attributes = { name: "Ben" }
user.attributes # => { name: "Ben" }

@return [Hash] attributes hash

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 89
def attributes=(attributes)
  attributes.each_pair do |key, value|
    @attributes[key.to_sym] = value
  end
  define_attributes
end
coerce(value, _options = {}) click to toggle source

Sets new values and returns self. Needs for embedded value.

@private

@param [Hash] value the new attributes for current object @param [Hash] _options

@example Use embedded values

class User
  include ShallowAttributes
  attribute :name, String, default: 'Ben'
end

class Post
  include ShallowAttributes
  attribute :author, User
end

post = Post.new(author: { name: 'Anton'} )
post.user.name # => 'Anton'

@return the object

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 141
def coerce(value, _options = {})
  self.attributes = value
  self
end
inspect() click to toggle source

Inspect instance object

@example Inspect the object

class User
  include ShallowAttributes
  attribute :name, String, default: 'Ben'
end

user = User.new(name: 'Anton')
user.inspect # => "#<User name=\"Anton\">"

@return [String]

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 181
def inspect
  "#<#{self.class}#{attributes.map{ |k, v| " #{k}=#{v.inspect}" }.join}>"
end
reset_attribute(attribute) click to toggle source

Reset specific attribute to default value.

@param [Symbol] attribute the attribute which will be reset

@example Reset name value

class User
  include ShallowAttributes
  attribute :name, String, default: 'Ben'
end

user = User.new(name: 'Anton')
user.reset_attribute(:name)
user.attributes # => { name: "Ben" }

@return the last attribute value

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 113
def reset_attribute(attribute)
  instance_variable_set("@#{attribute}", default_value_for(attribute))
end
to_h()

@since 0.1.0

Alias for: attributes
to_hash()

@since 0.1.0

Alias for: attributes

Private Instance Methods

default_value_for(attribute) click to toggle source

Returns default value for specific attribute. Default values hash is taken from class getter `default_values`.

@private

@return [nil] if default value not defined @return [Object] if default value is defined

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 240
def default_value_for(attribute)
  value = default_values[attribute]

  case value
  when Proc
    value.call(self, attribute)
  when Symbol, String
    respond_to?(value, true) ? send(value) : value
  else
    value
  end
end
default_values() click to toggle source

Returns hash of default class values

@private

@return [Hash]

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 260
def default_values
  @default_values ||= self.class.default_values
end
define_attributes() click to toggle source

Define attributes from `@attributes` instance value.

@private

@return the object

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 225
def define_attributes
  @attributes.each do |key, value|
    send("#{key}=", value)
  end
end
define_default_attributes() click to toggle source

Define default values for attributes.

@private

@return the object

@since 0.1.0

# File lib/shallow_attributes/instance_methods.rb, line 194
def define_default_attributes
  default_values.each do |key, value|
    next unless @attributes[key].nil? && !value.nil?
    send("#{key}=", default_value_for(key))
  end
end
define_mandatory_attributes() click to toggle source

Define mandatory attributes for object and raise exception if they were not provided

@private

@raise [MissingAttributeError] if attribute was not provided

@return the object

@since 0.10.0

# File lib/shallow_attributes/instance_methods.rb, line 211
def define_mandatory_attributes
  mandatory_attributes.each do |key, value|
    next unless @attributes[key].nil? && value
    raise ShallowAttributes::MissingAttributeError, %(Mandatory attribute "#{key}" was not provided)
  end
end
mandatory_attributes() click to toggle source

Returns hash of mandatory class attributes

@private

@return [Hash]

@since 0.10.0

# File lib/shallow_attributes/instance_methods.rb, line 271
def mandatory_attributes
  @mandatory_attributes ||= self.class.mandatory_attributes
end