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(options = {}) ⇒ 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)
47 48 49 |
# File 'lib/cot/frame.rb', line 47 def self.enum(_name, _args = {}) fail 'enum is not yet implemented' end |
.property(name, args = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cot/frame.rb', line 51 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
37 38 39 40 41 42 |
# File 'lib/cot/frame.rb', line 37 def self.search_property(name, args = {}) @search_mappings ||= {} key = args[:from] ? args[:from] : name @search_mappings[name] = key end |
Instance Method Details
#[](key) ⇒ Object
71 72 73 |
# File 'lib/cot/frame.rb', line 71 def [](key) @data[convert_key key] end |
#[]=(key, value) ⇒ Object
75 76 77 |
# File 'lib/cot/frame.rb', line 75 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
25 26 27 |
# File 'lib/cot/frame.rb', line 25 def inverted_properties_mapping self.class.inverted_mappings ||= properties_mapping.invert end |
#inverted_search_mappings ⇒ Object
33 34 35 |
# File 'lib/cot/frame.rb', line 33 def inverted_search_mappings self.class.inverted_search_mappings ||= search_mappings.invert end |
#properties_mapping ⇒ Object
21 22 23 |
# File 'lib/cot/frame.rb', line 21 def properties_mapping self.class.mappings end |
#search_mappings ⇒ Object
29 30 31 |
# File 'lib/cot/frame.rb', line 29 def search_mappings self.class.search_mappings end |
#serializable_hash(options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cot/frame.rb', line 83 def serializable_hash( = {}) attrs = {} properties_list = defined_properties if [:only] properties_list &= Array([:only]).map(&:to_sym) elsif [:except] properties_list -= Array([:except]).map(&:to_sym) end properties_list.each do |m| attrs[inverted_properties_mapping.fetch(m, m)] = self[m] end attrs end |
#to_json ⇒ Object
79 80 81 |
# File 'lib/cot/frame.rb', line 79 def to_json serializable_hash.to_json end |