module AlphaCard::Attribute::InstanceMethods

Attributes class methods

* initialize
* attributes
* []

Public Class Methods

new(attributes = {}) click to toggle source

Constructor supports setting attributes when creating a new instance of the class. Sets default values for the attributes if they are present.

@param attributes [Hash] attributes hash

@example

class User
  include AlphaCard::Attribute

  attribute :email
  attribute :name, default: 'John'
end

User.new(email: 'john.doe@gmail.com')
#=> #<User:0x29cca00 @email='john.doe@gmail.com', @name="John">
# File lib/alpha_card/attribute.rb, line 209
def initialize(attributes = {})
  set_attributes_defaults!

  attributes.each do |name, value|
    set_attribute_safely(name, value)
  end
end

Public Instance Methods

[](name) click to toggle source

Returns attribute value by it's name.

@param name [String, Symbol] attribute name

@return [Object] attribute value

@example

class User
  include AlphaCard::Attribute

  attribute :email
end

u = User.new(email: 'john@email.com')
u[:email]
#=> 'john@email.com'
# File lib/alpha_card/attribute.rb, line 255
def [](name)
  __send__(name)
end
attributes() click to toggle source

Returns class instance attributes.

@return [Hash] attributes of the instance object

@example

class User
  include AlphaCard::Attribute

  attribute :email
  attribute :name, default: 'John'
end

User.new.attributes
#=> { email: nil, name: 'John' }
# File lib/alpha_card/attribute.rb, line 232
def attributes
  self.class.attributes_set.each_with_object({}) do |(name, _), attributes|
    attributes[name] = __send__(name)
  end
end
required_attributes() click to toggle source

Returns names of the attributes that was marked as :required.

@return [Array] array of attributes names

@example

class User
  include AlphaCard::Attribute

  attribute :id
  attribute :email, required: true
  attribute :name, required: true
end

u = User.new
u.required_attributes
#=> [:email, :name]
# File lib/alpha_card/attribute.rb, line 276
def required_attributes
  self.class.attributes_set.select { |_, options| options[:required] }.keys
end
required_attributes?() click to toggle source

Indicates if all the attributes with option required: true are filled with non-nil value.

@return [Bool]

@example

class User
  include AlphaCard::Attribute

  attribute :email, required: true
  attribute :name
end

u = User.new
u.required_attributes?
#=> false

u.email = 'john.doe@gmail.com'
u.required_attributes?
#=> true
# File lib/alpha_card/attribute.rb, line 301
def required_attributes?
  required_attributes.all? { |attr| !self[attr].nil? }
end

Protected Instance Methods

attribute_writable?(name) click to toggle source

Checks if attribute is writable by it's options in the Attributes Set.

@param name [String, Symbol] attribute name

@return [Boolean]

# File lib/alpha_card/attribute.rb, line 322
def attribute_writable?(name)
  attribute_options = self.class.attributes_set[name.to_sym]
  return false if attribute_options.nil?

  attribute_options[:writable].nil? || attribute_options[:writable]
end
set_attribute_safely(name, value) click to toggle source

Set attribute value only if attribute writable

@param name [String, Symbol] attribute name @param value [Object] attribute value

# File lib/alpha_card/attribute.rb, line 312
def set_attribute_safely(name, value)
  __send__("#{name}=", value) if attribute_writable?(name)
end
set_attributes_defaults!() click to toggle source

Sets default values for the attributes, based on Attributes Set.

# File lib/alpha_card/attribute.rb, line 330
def set_attributes_defaults!
  self.class.attributes_set.each do |attr_name, options|
    instance_variable_set(:"@#{attr_name}", options[:default])
  end
end