Module: Lotus::Validations::AttributeDefiner Private

Defined in:
lib/lotus/validations/attribute_definer.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Define attributes and their validations together

Since:

  • 0.2.2

Defined Under Namespace

Modules: ClassMethods, EntityAttributeDefiner

Constant Summary collapse

LOTUS_ENTITY_CLASS_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.3

'Lotus::Entity'.freeze
LOTUS_ENTITY_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.3

'id'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override Ruby’s hook for modules.

Parameters:

  • base (Class)

    the target class

See Also:

Since:

  • 0.2.2



28
29
30
31
# File 'lib/lotus/validations/attribute_definer.rb', line 28

def self.included(base)
  base.extend ClassMethods
  base.extend EntityAttributeDefiner if lotus_entity?(base)
end

.lotus_entity?(base) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decide if enable the support for ‘Lotus::Entity`.

Parameters:

  • base (Class)

Returns:

  • (Boolean)

Since:

  • 0.2.3



39
40
41
42
43
# File 'lib/lotus/validations/attribute_definer.rb', line 39

def self.lotus_entity?(base)
  base.included_modules.any? do |m|
    m.to_s == LOTUS_ENTITY_CLASS_NAME
  end
end

Instance Method Details

#initialize(attributes = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new instance with the given attributes

Examples:

Initialize with Hash

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :name
end

 = Signup.new(name: 'Luca')

Initialize with Hash like

require 'lotus/validations'

class Params
  def initialize(attributes)
    @attributes = Hash[*attributes]
  end

  def to_h
    @attributes.to_h
  end
end

class Signup
  include Lotus::Validations

  attribute :name
end

params = Params.new([:name, 'Luca'])
 = Signup.new(params)

.name # => "Luca"

Parameters:

  • attributes (#to_h) (defaults to: {})

    an Hash like object which contains the attributes

Since:

  • 0.2.2



510
511
512
513
514
515
516
# File 'lib/lotus/validations/attribute_definer.rb', line 510

def initialize(attributes = {})
  @attributes ||= Utils::Attributes.new

  attributes.to_h.each do |key, value|
    public_send("#{ key }=", value) if assign_attribute?(key)
  end
end