Class: Cot::Frame
- Inherits:
-
Object
- Object
- Cot::Frame
- Includes:
- ActiveModel::Dirty
- Defined in:
- lib/cot/frame.rb
Class Attribute Summary collapse
-
.attr_methods ⇒ Object
Returns the value of attribute attr_methods.
-
.inverted_mappings ⇒ Object
Returns the value of attribute inverted_mappings.
-
.inverted_search_mappings ⇒ Object
Returns the value of attribute inverted_search_mappings.
-
.mappings ⇒ Object
Returns the value of attribute mappings.
-
.search_mappings ⇒ Object
Returns the value of attribute search_mappings.
Class Method Summary collapse
-
.enum(name, args = {}) ⇒ Object
TODO: Create an enum declaration that will automagically map a symbol to another value (such as an int) so that the user of the library doesn’t need to know what number scheduled status is (for example).
- .property(name, args = {}) ⇒ Object
- .search_property(name, args = {}) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #defined_properties ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(payload = {}) ⇒ Frame
constructor
A new instance of Frame.
- #inverted_properties_mapping ⇒ Object
- #inverted_search_mappings ⇒ Object
- #properties_mapping ⇒ Object
- #search_mappings ⇒ Object
- #serializable_hash ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(payload = {}) ⇒ Frame
Returns a new instance of Frame.
8 9 10 |
# File 'lib/cot/frame.rb', line 8 def initialize(payload={}) @data = convert_keys payload end |
Class Attribute Details
.attr_methods ⇒ Object
Returns the value of attribute attr_methods.
4 5 6 |
# File 'lib/cot/frame.rb', line 4 def attr_methods @attr_methods end |
.inverted_mappings ⇒ Object
Returns the value of attribute inverted_mappings.
4 5 6 |
# File 'lib/cot/frame.rb', line 4 def inverted_mappings @inverted_mappings end |
.inverted_search_mappings ⇒ Object
Returns the value of attribute inverted_search_mappings.
4 5 6 |
# File 'lib/cot/frame.rb', line 4 def inverted_search_mappings @inverted_search_mappings end |
.mappings ⇒ Object
Returns the value of attribute mappings.
4 5 6 |
# File 'lib/cot/frame.rb', line 4 def mappings @mappings end |
.search_mappings ⇒ Object
Returns the value of attribute search_mappings.
4 5 6 |
# File 'lib/cot/frame.rb', line 4 def search_mappings @search_mappings end |
Class Method Details
.enum(name, args = {}) ⇒ Object
TODO: Create an enum declaration that will automagically map a symbol to another value (such as an int) so that the user of the library doesn’t need to know what number scheduled status is (for example)
46 47 48 |
# File 'lib/cot/frame.rb', line 46 def self.enum(name, args={}) raise "enum is not yet implemented" end |
.property(name, args = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cot/frame.rb', line 50 def self.property(name, args={}) @mappings ||= {} @attr_methods ||= [] @search_mappings ||= {} key = args[:from] @mappings[key.to_sym] = name if key @search_mappings[name] = key ? key : name if args[:searchable] attr_methods << name.to_sym define_method name do self[name] end define_method "#{name}=" do |value| send("#{name}_will_change!") unless value == self[name] self[name] = value end define_attribute_method name end |
.search_property(name, args = {}) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/cot/frame.rb', line 36 def self.search_property(name, args={}) @search_mappings ||= {} key = args[:from] ? args[:from] : name @search_mappings[name] = key end |
Instance Method Details
#[](key) ⇒ Object
70 71 72 |
# File 'lib/cot/frame.rb', line 70 def [](key) @data[convert_key key] end |
#[]=(key, value) ⇒ Object
74 75 76 |
# File 'lib/cot/frame.rb', line 74 def []=(key, value) @data[convert_key key] = value end |
#defined_properties ⇒ Object
17 18 19 |
# File 'lib/cot/frame.rb', line 17 def defined_properties self.class.attr_methods end |
#exists? ⇒ Boolean
12 13 14 15 |
# File 'lib/cot/frame.rb', line 12 def exists? #TODO: Have this key off a defined primary key instead of defaulting to id id end |
#inverted_properties_mapping ⇒ Object
24 25 26 |
# File 'lib/cot/frame.rb', line 24 def inverted_properties_mapping self.class.inverted_mappings ||= properties_mapping.invert end |
#inverted_search_mappings ⇒ Object
32 33 34 |
# File 'lib/cot/frame.rb', line 32 def inverted_search_mappings self.class.inverted_search_mappings ||= search_mappings.invert end |
#properties_mapping ⇒ Object
20 21 22 |
# File 'lib/cot/frame.rb', line 20 def properties_mapping self.class.mappings end |
#search_mappings ⇒ Object
28 29 30 |
# File 'lib/cot/frame.rb', line 28 def search_mappings self.class.search_mappings end |
#serializable_hash ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/cot/frame.rb', line 82 def serializable_hash attrs = {} defined_properties.each do |m| attrs[inverted_properties_mapping.fetch(m,m)] = self[m] end attrs end |
#to_json ⇒ Object
78 79 80 |
# File 'lib/cot/frame.rb', line 78 def to_json serializable_hash.to_json end |