Class: Cot::Frame

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Dirty
Defined in:
lib/cot/frame.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_methodsObject

Returns the value of attribute attr_methods.



4
5
6
# File 'lib/cot/frame.rb', line 4

def attr_methods
  @attr_methods
end

.inverted_mappingsObject

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_mappingsObject

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

.mappingsObject

Returns the value of attribute mappings.



4
5
6
# File 'lib/cot/frame.rb', line 4

def mappings
  @mappings
end

.search_mappingsObject

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_propertiesObject



17
18
19
# File 'lib/cot/frame.rb', line 17

def defined_properties
  self.class.attr_methods
end

#exists?Boolean

Returns:

  • (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_mappingObject



25
26
27
# File 'lib/cot/frame.rb', line 25

def inverted_properties_mapping
  self.class.inverted_mappings ||= properties_mapping.invert
end

#inverted_search_mappingsObject



33
34
35
# File 'lib/cot/frame.rb', line 33

def inverted_search_mappings
  self.class.inverted_search_mappings ||= search_mappings.invert
end

#properties_mappingObject



21
22
23
# File 'lib/cot/frame.rb', line 21

def properties_mapping
  self.class.mappings
end

#search_mappingsObject



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(options = {})
  attrs = {}
  properties_list = defined_properties
  if options[:only]
    properties_list &= Array(options[:only]).map(&:to_sym)
  elsif options[:except]
    properties_list -= Array(options[:except]).map(&:to_sym)
  end
  properties_list.each do |m|
    attrs[inverted_properties_mapping.fetch(m, m)] = self[m]
  end
  attrs
end

#to_jsonObject



79
80
81
# File 'lib/cot/frame.rb', line 79

def to_json
  serializable_hash.to_json
end