Class: Cot::Frame

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

Instance Attribute Summary collapse

Attributes included from FrameClassMethods

#attr_methods, #inverted_mappings, #mappings, #value_blocks

Instance Method Summary collapse

Methods included from FrameClassMethods

enum, property, search_property

Constructor Details

#initialize(payload = {}) ⇒ Frame

Returns a new instance of Frame.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cot/frame.rb', line 9

def initialize(payload = {})
  @errors = {}

  @data = convert_keys payload

  @data.each do |k, v|
    if self.class.value_blocks[k]
      block = self.class.value_blocks[k]
      @data[k] = instance_exec(v, &block)
    end
  end
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/cot/frame.rb', line 51

def []=(key, value)
  if self.class.value_blocks[key]
    block = self.class.value_blocks[key]
    value = instance_exec(value, &block)
  end
  @data[convert_key key] = value
end

#defined_propertiesObject



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

def defined_properties
  self.class.attr_methods
end

#exists?Boolean

Returns:

  • (Boolean)


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

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

#inverted_properties_mappingObject



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

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

#inverted_search_mappingsObject



43
44
45
# File 'lib/cot/frame.rb', line 43

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

#properties_mappingObject



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

def properties_mapping
  self.class.mappings
end

#search_mappingsObject



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

def search_mappings
  self.class.search_mappings
end

#serializable_hash(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cot/frame.rb', line 67

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



63
64
65
# File 'lib/cot/frame.rb', line 63

def to_json
  serializable_hash.to_json
end

#valid?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cot/frame.rb', line 59

def valid?
  errors.empty?
end