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 gettring attributes hash for 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

Equalate two value objects

Examples:

Equalate two value objects

class User
  include ShallowAttributes
  attribute :name, String, defauil: '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


156
157
158
# File 'lib/shallow_attributes/instance_methods.rb', line 156

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 attributs

class User
  include ShallowAttributes
  attribute :name, String
end

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

Returns:

  • (Hash)

Since:

  • 0.1.0


52
53
54
55
56
57
58
59
# File 'lib/shallow_attributes/instance_methods.rb', line 52

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

Mass-assignment attribut values

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)

    attibutes hash

Since:

  • 0.1.0


84
85
86
87
# File 'lib/shallow_attributes/instance_methods.rb', line 84

def attributes=(attributes)
  @attributes.merge!(attributes)
  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, defauil: 'Ben'
end

class Post
  include ShallowAttributes
  attribute :author, User
end

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

Parameters:

  • values (Hash)

    the new attributes for current object

  • options (Hash)

Returns:

  • the object

Since:

  • 0.1.0


134
135
136
137
# File 'lib/shallow_attributes/instance_methods.rb', line 134

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

#initialize(attrs = {}) ⇒ Object

Initialize 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
# File 'lib/shallow_attributes/instance_methods.rb', line 32

def initialize(attrs = {})
  @attributes = attrs.delete_if { |key, _| !default_values.key?(key) }
  define_attributes
  define_default_attributes
end

#inspectString

Inspect instance object

Examples:

Equalate two value objects

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

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

Returns:

  • (String)

Since:

  • 0.1.0


174
175
176
# File 'lib/shallow_attributes/instance_methods.rb', line 174

def inspect
  "#<#{self.class}#{attributes.map{ |k, v| " #{k}=#{v.inspect}" }.join}>"
end

#reset_attribute(attribute) ⇒ Object

Reser specific attribute to defaul value.

Examples:

Reset name valus

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

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

Parameters:

  • attribute (Symbol)

    the attribute which will be resete

Returns:

  • the last attribute value

Since:

  • 0.1.0


106
107
108
# File 'lib/shallow_attributes/instance_methods.rb', line 106

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