Module: Micro::Attributes

Defined in:
lib/micro/attributes.rb,
lib/micro/attributes/macros.rb,
lib/micro/attributes/version.rb,
lib/micro/attributes/features.rb,
lib/micro/attributes/features/diff.rb,
lib/micro/attributes/attributes_utils.rb,
lib/micro/attributes/features/initialize.rb,
lib/micro/attributes/features/activemodel_validations.rb

Defined Under Namespace

Modules: AttributesUtils, Features

Constant Summary collapse

VERSION =
"0.11.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.features(*names) ⇒ Object



32
33
34
# File 'lib/micro/attributes.rb', line 32

def self.features(*names)
  Features.fetch(names)
end

.included(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/micro/attributes.rb', line 10

def self.included(base)
  base.extend(::Micro::Attributes.const_get(:Macros))

  base.class_eval do
    private_class_method :__attributes_data, :__attributes
    private_class_method :__attributes_def, :__attributes_set
    private_class_method :__attribute_reader, :__attribute_set
  end

  def base.inherited(subclass)
    subclass.attributes(self.attributes_data({}))
    subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses')
  end
end

.to_initialize(diff: false, activemodel_validations: false) ⇒ Object



25
26
27
28
29
30
# File 'lib/micro/attributes.rb', line 25

def self.to_initialize(diff: false, activemodel_validations: false)
  options = [:initialize]
  options << :diff if diff
  options << :activemodel_validations if activemodel_validations
  features(*options)
end

Instance Method Details

#attribute(name) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/micro/attributes.rb', line 58

def attribute(name)
  return unless attribute?(name)

  value = public_send(name)

  block_given? ? yield(value) : value
end

#attribute!(name, &block) ⇒ Object

Raises:

  • (NameError)


66
67
68
69
70
# File 'lib/micro/attributes.rb', line 66

def attribute!(name, &block)
  attribute(name) { |name| return block ? block[name] : name }

  raise NameError, "undefined attribute `#{name}"
end

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/micro/attributes.rb', line 54

def attribute?(name)
  self.class.attribute?(name)
end

#attributesObject



50
51
52
# File 'lib/micro/attributes.rb', line 50

def attributes
  __attributes
end