module PDC::Resource::Attributes::ClassMethods

Public Instance Methods

attribute(name, metadata) click to toggle source
# File lib/pdc/resource/attributes.rb, line 21
def attribute(name, metadata)
  attr_exists = attributes_metadata.key?(name)
  attributes_metadata[name] ||= default_metadata
  attributes_metadata[name].merge!(metadata)

  define_methods_in_container(name) unless attr_exists
end
attribute_parser(name) click to toggle source
# File lib/pdc/resource/attributes.rb, line 33
def attribute_parser(name)
  # do not add to attributes of the class if not already present
  metadata = attributes_metadata.fetch(name, default_metadata)
  metadata[:parser]
end
attributes(*names) click to toggle source

define attributes on Model using attributes :attribute_name, attribute_name_2 …

# File lib/pdc/resource/attributes.rb, line 15
def attributes(*names)
  return attributes_metadata.keys if names.empty?
  names.each { |n| attributes_metadata[n] ||= default_metadata }
  define_methods_in_container(names)
end
attributes_metadata() click to toggle source
# File lib/pdc/resource/attributes.rb, line 29
def attributes_metadata
  @attributes_metadata ||= HashWithIndifferentAccess.new(primary_key => default_metadata)
end

Private Instance Methods

default_metadata() click to toggle source
# File lib/pdc/resource/attributes.rb, line 41
def default_metadata
  { parser: ValueParser }
end
define_methods_in_container(names) click to toggle source
# File lib/pdc/resource/attributes.rb, line 45
def define_methods_in_container(names)
  instance_method_container.module_eval do
    Array.wrap(names).each do |name|
      define_method(name) do
        attribute(name)
      end
    end
  end
end
instance_method_container() click to toggle source

By adding instance methods via an included module, they become overridable with “super”. see: thepugautomatic.com/2013/07/dsom/

# File lib/pdc/resource/attributes.rb, line 58
def instance_method_container
  unless @instance_method_container
    @instance_method_container = Module.new
    include @instance_method_container
  end
  @instance_method_container
end