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
The attributes for this given record
Returns Hash
Public Class Methods
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
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
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
# 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
# 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
# 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
# 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
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
Improve code instrospect. Allows `respond_to?` for dynamically added attribute methods.
method - A Symbol with the method name
Returns Boolean
# File lib/evvnt/attributes.rb, line 118 def respond_to_missing?(method, *) attributes.stringify_keys.keys.include?(method.to_s) || super end