module Evvnt::Attributes

Adds attributes hash plus getters and setters to inherited base class.

Constants

DATETIME_STRING_REGEX

Test if a String is a datetime string.

DATE_STRING_REGEX

Test if a String is a date string.

Attributes

attributes[R]

The attributes for this given record

Returns Hash

Public Class Methods

new(attributes = {}) click to toggle source

Initialize a new record

attributes - A Hash of attributes for the given record. See {method_missing} for

more info on how this is handled.
# File lib/evvnt/attributes.rb, line 29
def initialize(attributes = {})
  self.attributes = Hash[attributes.map { |k, v| [k, format_attribute(k, v)] }]
end

Public Instance Methods

attributes=(hash) click to toggle source

Set or change the attributes for this record

hash - A Hash of attributes for this record.

Returns Hash

# File lib/evvnt/attributes.rb, line 39
def attributes=(hash)
  @attributes = Hash(hash).with_indifferent_access
end
unique_identifier() click to toggle source

The unique identifier for the given record. Tries uuid followed by id.

Returns String

# File lib/evvnt/attributes.rb, line 46
def unique_identifier
  attributes["uuid"] || attributes["id"]
end

Private Instance Methods

format_array_attribute(key, value) click to toggle source
# File lib/evvnt/attributes.rb, line 72
def format_array_attribute(key, value)
  Array(value).map do |attributes|
    unless Evvnt.const_defined?(key.singularize.classify)
      raise ArgumentError, "Unknown object type: #{key}"
    end
    Evvnt.const_get(key.singularize.classify).new(attributes)
  end
end
format_attribute(key, value) click to toggle source
# File lib/evvnt/attributes.rb, line 52
def format_attribute(key, value)
  case value
  when String
    format_string_attribute(value)
  when Array
    format_array_attribute(key, value)
  when Hash
    format_hash_attribute(key, value)
  else
    value
  end
end
format_hash_attribute(key, value) click to toggle source
# File lib/evvnt/attributes.rb, line 65
def format_hash_attribute(key, value)
  unless Evvnt.const_defined?(key.singularize.classify)
    raise ArgumentError, "Unknown object type: #{key}"
  end
  Evvnt.const_get(key.singularize.classify).new(value)
end
format_string_attribute(value) click to toggle source
# File lib/evvnt/attributes.rb, line 81
def format_string_attribute(value)
  case value
  when DATE_STRING_REGEX
    value.to_date
  when DATETIME_STRING_REGEX
    value.to_datetime
  else
    value
  end
end
method_missing(method_name, *args) click to toggle source

Overrides method missing to catch undefined methods. If method_name is one of the keys on attributes, returns the value of that attribute. If method_name is not one of attributes, passes up the chain to super.

method_name - Symbol of the name of the method we're testing for. args - Array of arguments send with the original mesage. block - Proc of code passed with original message.

# File lib/evvnt/attributes.rb, line 101
def method_missing(method_name, *args)
  setter    = method_name.to_s.ends_with?('=')
  attr_name = method_name.to_s.gsub(/=$/, "")
  if setter
    attributes[attr_name] = args.first
  else
    attributes[attr_name]
  end
end
respond_to_missing?(method, *) click to toggle source

Improve code instrospect. Allows `respond_to?` for dynamically added attribute methods.

method - A Symbol with the method name

Returns Boolean

Calls superclass method
# File lib/evvnt/attributes.rb, line 118
def respond_to_missing?(method, *)
  attributes.stringify_keys.keys.include?(method.to_s) || super
end