Module: Micro::Attributes::Macros

Defined in:
lib/micro/attributes/macros.rb

Instance Method Summary collapse

Instance Method Details

#__attribute(name) ⇒ Object



14
15
16
17
# File 'lib/micro/attributes/macros.rb', line 14

def __attribute(name)
  __attributes.add(name)
  attr_reader(name)
end

#__attribute_data(name, value, allow_to_override) ⇒ Object



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

def __attribute_data(name, value, allow_to_override)
  has_attribute = attribute?(name)
  __attribute(name) unless has_attribute
  __attributes_data[name] = value if allow_to_override || !has_attribute
end

#__attribute_data!(arg, allow_to_override:) ⇒ Object



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

def __attribute_data!(arg, allow_to_override:)
  return __attribute_data(arg.to_s, nil, allow_to_override) unless arg.is_a?(Hash)

  arg.each { |key, value| __attribute_data(key.to_s, value, allow_to_override) }
end

#__attributesObject



6
7
8
# File 'lib/micro/attributes/macros.rb', line 6

def __attributes
  @__attributes ||= Set.new
end

#__attributes_dataObject



19
20
21
# File 'lib/micro/attributes/macros.rb', line 19

def __attributes_data
  @__attributes_data ||= {}
end

#attribute(arg) ⇒ Object



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

def attribute(arg)
  __attribute_data!(arg, allow_to_override: false)
end

#attribute!(arg) ⇒ Object



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

def attribute!(arg)
  __attribute_data!(arg, allow_to_override: true)
end

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/micro/attributes/macros.rb', line 10

def attribute?(name)
  __attributes.member?(name.to_s)
end

#attributes(*args) ⇒ Object



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

def attributes(*args)
  return __attributes.to_a if args.empty?

  args.flatten.each { |arg| attribute(arg) }
end

#attributes!(*args) ⇒ Object



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

def attributes!(*args)
  args.flatten.each { |arg| attribute!(arg) }
end

#attributes_data(arg) ⇒ Object



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

def attributes_data(arg)
  __attributes_data.merge(
    Utils.hash_argument!(arg)
         .each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
  )
end