Module: Relaxo::Model::Component

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

Overview

Represents an underlying object with changes which can be persisted.

Constant Summary collapse

ALL =
Hash.new(true)

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



97
98
99
100
101
102
103
# 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) ⇒ Object



105
106
107
108
109
110
111
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Boolean

Returns:

  • (Boolean)


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

def changed?(key)
	@changed.include?(key)
end

#clear(key) ⇒ Object



67
68
69
70
# File 'lib/relaxo/model/component.rb', line 67

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



121
122
123
124
125
126
127
128
# 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) ⇒ Object



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

def reload(dataset = @dataset)
	@dataset = dataset
	@changed.clear
	self.load_object
end

#to_hObject



117
118
119
# File 'lib/relaxo/model/component.rb', line 117

def to_h
	@attributes
end

#validate(changeset) ⇒ Object



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

def validate(changeset)
	# Do nothing :)
end