Module: Petra::Util::ExtendedAttributeAccessors::ClassMethods

Defined in:
lib/petra/util/extended_attribute_accessors.rb

Instance Method Summary collapse

Instance Method Details

#extended_attr_accessor(name, **options) ⇒ Object

TODO: Keep track of available accessors on class level TODO: Keep track of the actual values on instance (or class instance) level



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/petra/util/extended_attribute_accessors.rb', line 16

def extended_attr_accessor(name, **options)
  singleton = options.fetch(:singleton, false)
  methods_method, definer_method = :instance_methods, :define_method
  methods_method, definer_method = :singleton_methods, :define_singleton_method if singleton

  unless send(methods_method).include?(:extended_attribute_accessors)
    send(definer_method, :__extended_attribute_accessors__) do |group = :general|
      (@extended_attribute_accessors ||= {})[group.to_sym] ||= {}
    end

    send(definer_method, :extended_attribute_accessors) do |group = :general, only: nil|
      result = __extended_attribute_accessors__(group)
      return result unless only

      result.each_with_object({}) do |(k, v), h|
        h[k] = v.slice(Array(only).map(:to_sym))
      end
    end
  end

  group = options.fetch(:group, :general)

  send(definer_method, name) do
    accessor = extended_attribute_accessors(group)[name.to_sym] || {}
    accessor[:value] || accessor[:default]
  end

  define_method("#{name}=") do |value|
    self[name] = value
  end
end