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)



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_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



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

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

#inverted_search_mappingsObject



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

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

#properties_mappingObject



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

def properties_mapping
  self.class.mappings
end

#search_mappingsObject



28
29
30
# File 'lib/cot/frame.rb', line 28

def search_mappings
  self.class.search_mappings
end

#serializable_hashObject



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_jsonObject



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

def to_json
  serializable_hash.to_json
end