Module: ShallowAttributes::InstanceMethods Abstract

Included in:
ShallowAttributes
Defined in:
lib/shallow_attributes/instance_methods.rb

Overview

This module is abstract.

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

Since:

  • 0.1.0

Constant Summary collapse

TO_H_PROC =

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

Since:

  • 0.1.0

->(value) { value.respond_to?(:to_hash) ? value.to_hash : value }

Instance Method Summary collapse

Instance Method Details

#==(object) ⇒ boolean

Compare values of two objects

Examples:

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

Parameters:

  • object (Object)

    the other object

Returns:

  • (boolean)

Since:

  • 0.1.0



163
164
165
# File 'lib/shallow_attributes/instance_methods.rb', line 163

def ==(object)
  self.to_h == object.to_h
end

#attributesHash Also known as: to_h, to_hash

Returns hash of object attributes

Examples:

Returns all user attributes

class User
  include ShallowAttributes
  attribute :name, String
end

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

Returns:

  • (Hash)

Since:

  • 0.1.0



57
58
59
60
61
62
63
64
# 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

#attributes=(attributes) ⇒ Hash

Attribute values mass-assignment

Examples:

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" }

Parameters:

  • attributes (Hash)

    the attributes which will be assignment

Returns:

  • (Hash)

    attributes hash

Since:

  • 0.1.0



89
90
91
92
93
94
# 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 = {}) ⇒ Object

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

Examples:

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'

Parameters:

  • value (Hash)

    the new attributes for current object

  • _options (Hash) (defaults to: {})

Returns:

  • the object

Since:

  • 0.1.0



141
142
143
144
# File 'lib/shallow_attributes/instance_methods.rb', line 141

def coerce(value, _options = {})
  self.attributes = value
  self
end

#initialize(attrs = {}) ⇒ Object

Initialize an instance object with specific attributes

Examples:

Create new User instance

class User
  include ShallowAttributes
  attribute :name, String
end

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

Parameters:

  • attrs (Hash) (defaults to: {})

    the attributes contained in the class

Returns:

  • the new instance of value class with specific attributes

Since:

  • 0.1.0



32
33
34
35
36
37
38
39
40
41
# 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

#inspectString

Inspect instance object

Examples:

Inspect the object

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

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

Returns:

  • (String)

Since:

  • 0.1.0



181
182
183
# 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) ⇒ Object

Reset specific attribute to default value.

Examples:

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" }

Parameters:

  • attribute (Symbol)

    the attribute which will be reset

Returns:

  • the last attribute value

Since:

  • 0.1.0



113
114
115
# File 'lib/shallow_attributes/instance_methods.rb', line 113

def reset_attribute(attribute)
  instance_variable_set("@#{attribute}", default_value_for(attribute))
end