Module: Evvnt::Attributes
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base, NestedObject
- Defined in:
- lib/evvnt/attributes.rb
Overview
Adds attributes hash plus getters and setters to inherited base class.
Constant Summary collapse
- DATE_STRING_REGEX =
Test if a String is a date string.
/^\d{4}-\d{2}-\d{2}$/
- DATETIME_STRING_REGEX =
Test if a String is a datetime string.
/^\d{4}-\d\d-\d\dT\d\d\:\d\d\:\d\d\+\d\d\:\d\d$/
Instance Attribute Summary collapse
-
#attributes ⇒ Object
The attributes for this given record.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Object
Initialize a new record.
-
#unique_identifier ⇒ Object
The unique identifier for the given record.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
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.
101 102 103 104 105 106 107 108 109 |
# 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 |
Instance Attribute Details
#attributes ⇒ Object
The attributes for this given record
Returns Hash
21 22 23 |
# File 'lib/evvnt/attributes.rb', line 21 def attributes @attributes end |
Instance Method Details
#initialize(attributes = {}) ⇒ Object
Initialize a new record
attributes - A Hash of attributes for the given record. See #method_missing for
more info on how this is handled.
29 30 31 |
# File 'lib/evvnt/attributes.rb', line 29 def initialize(attributes = {}) self.attributes = Hash[attributes.map { |k, v| [k, format_attribute(k, v)] }] end |
#unique_identifier ⇒ Object
The unique identifier for the given record. Tries uuid
followed by id
.
Returns String
46 47 48 |
# File 'lib/evvnt/attributes.rb', line 46 def unique_identifier attributes["uuid"] || attributes["id"] end |