Module: Relaxo::Model::Component

Defined in:
lib/relaxo/model/component.rb

Overview

Represents an underlying object with changes which can be persisted.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

The attributes specified/loaded from the dataset:



52
53
54
# File 'lib/relaxo/model/component.rb', line 52

def attributes
  @attributes
end

#changedObject (readonly)

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



55
56
57
# File 'lib/relaxo/model/component.rb', line 55

def changed
  @changed
end

#datasetObject (readonly)

The dataset this document is currently bound to:



49
50
51
# File 'lib/relaxo/model/component.rb', line 49

def dataset
  @dataset
end

Class Method Details

.included(child) ⇒ Object



30
31
32
33
# File 'lib/relaxo/model/component.rb', line 30

def self.included(child)
	# $stderr.puts "#{self} included -> #{child} extend Base"
	child.send(:extend, Base)
end

Instance Method Details

#[](name) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/relaxo/model/component.rb', line 92

def [] name
	if self.class.properties.include? name
		self.send(name)
	else
		raise KeyError.new(name)
	end
end

#[]=(name, value) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/relaxo/model/component.rb', line 100

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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/relaxo/model/component.rb', line 67

def assign(primative_attributes, only = :all)
	enumerator = primative_attributes

	if only == :all
		enumerator = enumerator.select{|key, value| self.class.properties.include? key.to_sym}
	elsif only.respond_to? :include?
		enumerator = enumerator.select{|key, value| only.include? key.to_sym}
	end

	enumerator.each do |key, value|
		key = key.to_sym

		klass = self.class.properties[key]

		if klass
			# This might raise a validation error
			value = klass.convert_from_primative(@dataset, value)
		end

		self[key] = value
	end
	
	return self
end

#clear(key) ⇒ Object



62
63
64
65
# File 'lib/relaxo/model/component.rb', line 62

def clear(key)
	@changed.delete(key)
	@attributes.delete(key)
end

#initialize(dataset, object = nil, changed = {}, **attributes) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# 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

#load_objectObject



116
117
118
119
120
121
122
123
# File 'lib/relaxo/model/component.rb', line 116

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

#reloadObject



57
58
59
60
# File 'lib/relaxo/model/component.rb', line 57

def reload
	@changed.clear
	self.load_object
end

#to_hashObject



112
113
114
# File 'lib/relaxo/model/component.rb', line 112

def to_hash
	@attributes
end

#validateObject



108
109
110
# File 'lib/relaxo/model/component.rb', line 108

def validate
	# Do nothing :)
end