class Cot::Frame

Attributes

errors[RW]

Public Class Methods

new(payload = nil) click to toggle source
# File lib/cot/frame.rb, line 9
def initialize(payload = nil)
  @errors = {}

  @data = convert_keys payload

  add_value_blocks

  add_missing_blocks
end

Public Instance Methods

[](key) click to toggle source
# File lib/cot/frame.rb, line 51
def [](key)
  @data[convert_key key]
end
[]=(key, value) click to toggle source
# 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_properties() click to toggle source
# File lib/cot/frame.rb, line 23
def defined_properties
  self.class.attr_methods || []
end
exists?() click to toggle source
# File lib/cot/frame.rb, line 19
def exists?
  public_send self.class.primary_key
end
inverted_properties_mapping() click to toggle source
# File lib/cot/frame.rb, line 39
def inverted_properties_mapping
  self.class.inverted_mappings ||= properties_mapping.invert
end
inverted_search_mappings() click to toggle source
# File lib/cot/frame.rb, line 47
def inverted_search_mappings
  self.class.inverted_search_mappings ||= search_mappings.invert
end
missing_blocks() click to toggle source
# File lib/cot/frame.rb, line 35
def missing_blocks
  self.class.missing_blocks || {}
end
properties_mapping() click to toggle source
# File lib/cot/frame.rb, line 27
def properties_mapping
  self.class.mappings || {}
end
search_mappings() click to toggle source
# File lib/cot/frame.rb, line 43
def search_mappings
  self.class.search_mappings
end
serializable_hash(options = {}) click to toggle source
# 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_json() click to toggle source
# File lib/cot/frame.rb, line 67
def to_json
  serializable_hash.to_json
end
valid?() click to toggle source
# File lib/cot/frame.rb, line 63
def valid?
  errors.empty?
end
value_blocks() click to toggle source
# File lib/cot/frame.rb, line 31
def value_blocks
  self.class.value_blocks || {}
end

Private Instance Methods

add_missing_blocks() click to toggle source
# File lib/cot/frame.rb, line 84
def add_missing_blocks
  defined_properties.each do |prop|
    if self[prop].nil? && missing_blocks[prop]
      block = missing_blocks[prop]
      self[prop] = instance_exec(&block)
    end
  end
end
add_value_blocks() click to toggle source
# File lib/cot/frame.rb, line 93
def add_value_blocks
  @data.each do |k, v|
    if value_blocks[k]
      block = value_blocks[k]
      @data[k] = instance_exec(v, &block)
    end
  end
end
convert_key(key) click to toggle source
# File lib/cot/frame.rb, line 102
def convert_key(key)
  key = key.to_sym
  properties_mapping.fetch(key, key)
end
convert_keys(hash) click to toggle source
# File lib/cot/frame.rb, line 107
def convert_keys(hash)
  return {} unless hash
  {}.tap do |ret|
    hash.each_pair { |k, v| ret[convert_key k] = v }
  end
end