module Relaxo::Model::Component

Represents an underlying object with changes which can be persisted.

Constants

ALL

Attributes

attributes[R]

The attributes specified/loaded from the dataset:

changed[R]

Attributes that have been changed or de-serialized from the dataset:

dataset[R]

The dataset this document is currently bound to:

Public Class Methods

included(child) click to toggle source
# File lib/relaxo/model/component.rb, line 30
def self.included(child)
        # $stderr.puts "#{self} included -> #{child} extend Base"
        child.send(:extend, Base)
end
new(dataset, object = nil, changed = {}, **attributes) click to toggle source
# File lib/relaxo/model/component.rb, line 35
def initialize(dataset, object = nil, changed = {}, **attributes)
        @dataset = dataset
        
        # The object from the dataset:
        @object = object
        
        # Underlying attributes from the dataset:
        @attributes = attributes
        
        # Contains non-primitve attributes and changes:
        @changed = changed
end

Public Instance Methods

[](name) click to toggle source
# File lib/relaxo/model/component.rb, line 97
def [] name
        if self.class.properties.include? name
                self.send(name)
        else
                raise KeyError.new(name)
        end
end
[]=(name, value) click to toggle source
# File lib/relaxo/model/component.rb, line 105
def []= name, value
        if self.class.properties.include? name
                self.send("#{name}=", value)
        else
                raise KeyError.new(name)
        end
end
assign(primative_attributes, only = ALL) click to toggle source
# File lib/relaxo/model/component.rb, line 74
def assign(primative_attributes, only = ALL)
        primative_attributes.each do |key, value|
                key = key.to_sym
                case mapping = only[key]
                when true
                        # Assign from primitive value:
                        if klass = self.class.properties[key]
                                value = klass.convert_from_primative(@dataset, value)
                        end
                        
                        self[key] = value
                when false, nil
                        # Ignore:
                        next
                else
                        # Nested assignment:
                        self[key].assign(value, mapping)
                end
        end
        
        return self
end
changed?(key) click to toggle source
# File lib/relaxo/model/component.rb, line 57
def changed?(key)
        @changed.include?(key)
end
clear(key) click to toggle source
# File lib/relaxo/model/component.rb, line 67
def clear(key)
        @changed.delete(key)
        @attributes.delete(key)
end
load_object() click to toggle source
# File lib/relaxo/model/component.rb, line 121
def load_object
        if @object
                attributes = MessagePack.load(@object.data, symbolize_keys: true)
                
                # We prefer existing @attributes over ones loaded from data. This allows the API to load from an object, but specify new attributes.
                @attributes = attributes.merge(@attributes)
        end
end
reload(dataset = @dataset) click to toggle source
# File lib/relaxo/model/component.rb, line 61
def reload(dataset = @dataset)
        @dataset = dataset
        @changed.clear
        self.load_object
end
to_h() click to toggle source
# File lib/relaxo/model/component.rb, line 117
def to_h
        @attributes
end
validate(changeset) click to toggle source
# File lib/relaxo/model/component.rb, line 113
def validate(changeset)
        # Do nothing :)
end

Protected Instance Methods

dump() click to toggle source

Flatten all changes and return a serialized version of the object.

# File lib/relaxo/model/component.rb, line 133
def dump
        flatten!
        
        MessagePack.dump(@attributes)
end
flatten!() click to toggle source

Moves values from `@changed` into `@attributes`.

# File lib/relaxo/model/component.rb, line 140
def flatten!
        self.class.properties.each do |key, klass|
                begin
                        if @changed.include?(key)
                                if klass
                                        @attributes[key] = klass.convert_to_primative(@changed.delete(key))
                                else
                                        @attributes[key] = @changed.delete(key)
                                end
                        elsif !@attributes.include?(key) and klass.respond_to?(:default)
                                @attributes[key] = klass.default
                        end
                rescue
                        raise SerializationError, "Failed to flatten #{key.inspect} of type #{klass.inspect}!"
                end
        end

        # Non-specific properties:
        @changed.each do |key, value|
                @attributes[key] = value
        end

        @changed = {}
end