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, #primary_key

Instance Method Summary collapse

Methods included from FrameClassMethods

enum, property, search_property

Constructor Details

#initialize(payload = nil) ⇒ Frame

Returns a new instance of Frame.



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

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

  @data = convert_keys payload

  add_value_blocks

  add_missing_blocks
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



51
52
53
# File 'lib/cot/frame.rb', line 51

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

#[]=(key, value) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/cot/frame.rb', line 55

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

#defined_propertiesObject



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

def defined_properties
  self.class.attr_methods || []
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  public_send self.class.primary_key
end

#inverted_properties_mappingObject



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

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

#inverted_search_mappingsObject



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

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

#missing_blocksObject



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

def missing_blocks
  self.class.missing_blocks || {}
end

#properties_mappingObject



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

def properties_mapping
  self.class.mappings || {}
end

#search_mappingsObject



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

def search_mappings
  self.class.search_mappings
end

#serializable_hash(options = {}) ⇒ Object



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

def serializable_hash(options = {})
  attrs = {}
  properties_list = defined_properties
  properties_list &= Array(options[:only]).map(&:to_sym) if options[:only]
  properties_list -= Array(options[:except]).map(&:to_sym)
  properties_list.each do |m|
    attrs[inverted_properties_mapping.fetch(m, m)] = self[m]
  end
  attrs
end

#to_jsonObject



67
68
69
# File 'lib/cot/frame.rb', line 67

def to_json
  serializable_hash.to_json
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  errors.empty?
end

#value_blocksObject



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

def value_blocks
  self.class.value_blocks || {}
end