Module: Micro::Attributes

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

Defined Under Namespace

Modules: Diff, Features, Utils, With

Constant Summary collapse

VERSION =
'2.1.1'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/micro/attributes.rb', line 13

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

  base.class_eval do
    private_class_method :__attributes, :__attribute_reader
    private_class_method :__attribute_assign, :__attributes_data_to_assign
  end

  def base.inherited(subclass)
    subclass.__attributes_set_after_inherit__(self.__attributes_data__)

    subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses'.freeze)
  end
end

.with(*names) ⇒ Object



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

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

.with_all_featuresObject



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

def self.with_all_features
  Features.all
end

.without(*names) ⇒ Object



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

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

Instance Method Details

#attribute(name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/micro/attributes.rb', line 44

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

  value = public_send(name)

  block_given? ? yield(value) : value
end

#attribute!(name, &block) ⇒ Object

Raises:

  • (NameError)


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

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

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

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#attributes(*names) ⇒ Object



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

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

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

#defined_attributesObject



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

def defined_attributes
  @defined_attributes ||= self.class.attributes
end