Module: Micro::Attributes::Macros

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

Instance Method Summary collapse

Instance Method Details

#__attribute(name) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/micro/attributes/macros.rb', line 18

def __attribute(name)
  return false if attribute?(name)

  __attributes.add(name)
  attr_reader(name)

  return true
end

#__attributesObject



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

def __attributes
  @__attributes ||= Set.new
end

#__attributes_defaultsObject



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

def __attributes_defaults
  @__attributes_defaults ||= {}
end

#attribute(arg) ⇒ Object



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

def attribute(arg)
  return __attribute(arg.to_s) unless arg.is_a?(Hash)

  arg.each do |key, value|
    name = key.to_s
    __attributes_defaults[name] = value if __attribute(name)
  end
end

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#attributes(*args) ⇒ Object



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

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

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

#attributes_data(arg) {|normalized_params.merge!(nil_params).merge!(__attributes_defaults)| ... } ⇒ Object

Yields:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/micro/attributes/macros.rb', line 42

def attributes_data(arg)
  normalized_params = arg.keys.each_with_object({}) do |key, memo|
    memo[key.to_s] = arg[key]
  end

  undefineds = (self.attributes - normalized_params.keys)
  nil_params =
    undefineds.each_with_object({}) { |name, memo| memo[name] = nil }

  yield(
    normalized_params.merge!(nil_params).merge!(__attributes_defaults)
  )
end