Class: Cot::Frame

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

Class Attribute Summary collapse

Instance 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
11
# File 'lib/cot/frame.rb', line 8

def initialize(payload = {})
  @errors = {}
  @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

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



72
73
74
# File 'lib/cot/frame.rb', line 72

def errors
  @errors
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)



48
49
50
# File 'lib/cot/frame.rb', line 48

def self.enum(_name, _args = {})
  fail 'enum is not yet implemented'
end

.property(name, args = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cot/frame.rb', line 52

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



38
39
40
41
42
43
# File 'lib/cot/frame.rb', line 38

def self.search_property(name, args = {})
  @search_mappings ||= {}

  key = args[:from] ? args[:from] : name
  @search_mappings[name] = key
end

Instance Method Details

#[](key) ⇒ Object



74
75
76
# File 'lib/cot/frame.rb', line 74

def [](key)
  @data[convert_key key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @data[convert_key key] = value
end

#defined_propertiesObject



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

def defined_properties
  self.class.attr_methods
end

#exists?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/cot/frame.rb', line 13

def exists?
  # TODO: Have this key off a defined primary key instead of defaulting to id
  id
end

#inverted_properties_mappingObject



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

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

#inverted_search_mappingsObject



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

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

#properties_mappingObject



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

def properties_mapping
  self.class.mappings
end

#search_mappingsObject



30
31
32
# File 'lib/cot/frame.rb', line 30

def search_mappings
  self.class.search_mappings
end

#serializable_hash(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cot/frame.rb', line 90

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



86
87
88
# File 'lib/cot/frame.rb', line 86

def to_json
  serializable_hash.to_json
end

#valid?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/cot/frame.rb', line 82

def valid?
  errors.empty?
end