Module: Micro::Attributes

Defined in:
lib/micro/attributes.rb,
lib/micro/attributes/with.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/strict_initialize.rb,
lib/micro/attributes/features/activemodel_validations.rb

Defined Under Namespace

Modules: AttributesUtils, Features, With

Constant Summary collapse

VERSION =
'1.2.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.feature(name) ⇒ Object



41
42
43
# File 'lib/micro/attributes.rb', line 41

def self.feature(name)
  self.with(name)
end

.features(*names) ⇒ Object



45
46
47
# File 'lib/micro/attributes.rb', line 45

def self.features(*names)
  names.empty? ? Features.all : Features.with(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'.freeze)
  end
end

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



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

def self.to_initialize(diff: false, activemodel_validations: false)
  features(*Features.options(:initialize, diff, activemodel_validations))
end

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



29
30
31
# File 'lib/micro/attributes.rb', line 29

def self.to_initialize!(diff: false, activemodel_validations: false)
  features(*Features.options(:strict_initialize, diff, activemodel_validations))
end

.with(*names) ⇒ Object



37
38
39
# File 'lib/micro/attributes.rb', line 37

def self.with(*names)
  Features.with(names)
end

.without(*names) ⇒ Object



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

def self.without(*names)
  Features.without(names)
end

Instance Method Details

#attribute(name) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/micro/attributes.rb', line 77

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

  value = public_send(name)

  block_given? ? yield(value) : value
end

#attribute!(name, &block) ⇒ Object

Raises:

  • (NameError)


85
86
87
88
89
# File 'lib/micro/attributes.rb', line 85

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

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

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/micro/attributes.rb', line 73

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

#attributes(*names) ⇒ Object



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

def attributes(*names)
  return __attributes if names.empty?

  names.each_with_object({}) do |name, memo|
    memo[name] = attribute(name) if attribute?(name)
  end
end