Module: Micro::Attributes

Defined in:
lib/micro/attributes.rb,
lib/micro/attributes/diff.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/accept.rb,
lib/micro/attributes/features/initialize.rb,
lib/micro/attributes/features/accept/strict.rb,
lib/micro/attributes/features/keys_as_symbol.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.7.0'.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
27
# 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_groups
    private_class_method :__attributes_required_add, :__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



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

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

.with_all_featuresObject



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

def self.with_all_features
  Features.all
end

.without(*names) ⇒ Object



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

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

Instance Method Details

#attribute(name) ⇒ Object



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

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

  value = public_send(name)

  block_given? ? yield(value) : value
end

#attribute!(name, &block) ⇒ Object

Raises:

  • (NameError)


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

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

  raise NameError, __attribute_access_error_message(name)
end

#attribute?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def attribute?(name, include_all = false)
  self.class.attribute?(name, include_all)
end

#attributes(*names) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/micro/attributes.rb', line 65

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

  options = names.last.is_a?(Hash) ? names.pop : Kind::Empty::HASH

  names.flatten!

  without_option = Array(options.fetch(:without, Kind::Empty::ARRAY))

  keys = names.empty? ? defined_attributes - without_option.map { |value| __attribute_key(value) } : names - without_option

  data = keys.each_with_object({}) { |key, memo| memo[key] = attribute(key) if attribute?(key) }

  with_option = Array(options.fetch(:with, Kind::Empty::ARRAY))

  unless with_option.empty?
    extra = with_option.each_with_object({}) { |key, memo| memo[__attribute_key(key)] = public_send(key) }

    data.merge!(extra)
  end

  Utils::Hashes.keys_as(options[:keys_as], data)
end

#defined_attributes(option = nil) ⇒ Object



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

def defined_attributes(option = nil)
  return self.class.attributes_by_visibility if option == :by_visibility

  @defined_attributes ||= self.class.attributes
end