Class: Crimson::Object
- Inherits:
-
Model
- Object
- SimpleDelegator
- Model
- Crimson::Object
show all
- Defined in:
- lib/crimson/object.rb
Instance Attribute Summary collapse
Attributes inherited from Model
#local, #observers, #revision_number, #revisions
Instance Method Summary
collapse
Methods inherited from Model
#add_observer, #apply_changes!, #changed?, #changes, #commit!, #master, #modify, #new_changes, #notify_observers, #reload!, #remove_observer, #rollback!
Constructor Details
#initialize(tag) ⇒ Object
Returns a new instance of Object.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/crimson/object.rb', line 13
def initialize(tag)
super()
@id = :"object_#{Utilities.generate_id}"
@tag = tag.to_sym
@node = Tree::TreeNode.new(id, self)
@event_handlers = Mash.new
@added_children = Set.new
@removed_children = Set.new
self.style = Mash.new
show
end
|
Instance Attribute Details
#added_children ⇒ Object
Returns the value of attribute added_children.
11
12
13
|
# File 'lib/crimson/object.rb', line 11
def added_children
@added_children
end
|
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
11
12
13
|
# File 'lib/crimson/object.rb', line 11
def event_handlers
@event_handlers
end
|
Returns the value of attribute id.
11
12
13
|
# File 'lib/crimson/object.rb', line 11
def id
@id
end
|
Returns the value of attribute node.
11
12
13
|
# File 'lib/crimson/object.rb', line 11
def node
@node
end
|
#removed_children ⇒ Object
Returns the value of attribute removed_children.
11
12
13
|
# File 'lib/crimson/object.rb', line 11
def removed_children
@removed_children
end
|
Returns the value of attribute tag.
11
12
13
|
# File 'lib/crimson/object.rb', line 11
def tag
@tag
end
|
Instance Method Details
#==(other) ⇒ Object
184
185
186
|
# File 'lib/crimson/object.rb', line 184
def ==(other)
other.is_a?(Object) && other.id == id
end
|
#add(child, at_index = -1)) ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/crimson/object.rb', line 88
def add(child, at_index = -1)
raise ArgumentError unless child.is_a?(Crimson::Object)
raise ArgumentError if children.include?(child)
node.add(child.node, at_index)
self[:children] = children.map(&:id)
added_children << child
end
|
#breadth_each ⇒ Object
154
155
156
157
158
|
# File 'lib/crimson/object.rb', line 154
def breadth_each
node.breadth_each do |subnode|
yield(subnode.content)
end
end
|
125
126
127
|
# File 'lib/crimson/object.rb', line 125
def children
node.children.map(&:content)
end
|
#commit_tree!(*keys) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/crimson/object.rb', line 133
def commit_tree!(*keys)
breadth_each do |object|
observers.keys.each do |observer|
object.added_children.each do |child|
observer.observe(child)
end
object.removed_children.each do |child|
observer.unobserve(child)
end
end
object.added_children.clear
object.removed_children.clear
object.commit!(*keys)
end
end
|
#eql?(other) ⇒ Boolean
188
189
190
|
# File 'lib/crimson/object.rb', line 188
def eql?(other)
self == other
end
|
#find_descendant(descendent_id) ⇒ Object
172
173
174
|
# File 'lib/crimson/object.rb', line 172
def find_descendant(descendent_id)
breadth_each { |descendent| return descendent if descendent_id == descendent.id }
end
|
192
193
194
|
# File 'lib/crimson/object.rb', line 192
def hash
id.hash
end
|
#hidden? ⇒ Boolean
29
30
31
|
# File 'lib/crimson/object.rb', line 29
def hidden?
style.display.to_sym == :none
end
|
37
38
39
|
# File 'lib/crimson/object.rb', line 37
def hide
style.display = :none
end
|
176
177
178
|
# File 'lib/crimson/object.rb', line 176
def inspect
to_s
end
|
#move(child, at_index) ⇒ Object
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/crimson/object.rb', line 110
def move(child, at_index)
raise ArgumentError unless child.is_a?(Crimson::Object)
raise ArgumentError unless children.include?(child)
remove(child)
add(child, at_index)
added_children.delete(child)
removed_children.delete(child)
end
|
#on(event, handler = nil, &block) ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/crimson/object.rb', line 53
def on(event, handler = nil, &block)
raise ArgumentError unless handler.nil? || handler.is_a?(Method) || handler.is_a?(Proc)
event_handlers[event] = [] unless event_handlers[event]
event_handlers[event] << handler unless handler.nil?
event_handlers[event] << block if block_given?
self[:events] = event_handlers.keys
commit!(:events)
end
|
#on_event(message) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/crimson/object.rb', line 45
def on_event(message)
unless event_handlers.key?(message.event)
raise ArgumentError, "[Object] Trying to handle unknown event '#{message.event}' for '#{id}'."
end
event_handlers[message.event].each { |functor| functor.call(message.data) }
end
|
77
78
79
|
# File 'lib/crimson/object.rb', line 77
def parent
node.parent&.content
end
|
#parent=(new_parent) ⇒ Object
81
82
83
84
85
86
|
# File 'lib/crimson/object.rb', line 81
def parent=(new_parent)
raise ArgumentError unless new_parent.nil? || new_parent.is_a?(Crimson::Object)
parent&.remove(self)
new_parent&.add(self)
end
|
#postordered_each ⇒ Object
160
161
162
163
164
|
# File 'lib/crimson/object.rb', line 160
def postordered_each
node.postordered_each do |subnode|
yield(subnode.content)
end
end
|
#preordered_each ⇒ Object
166
167
168
169
170
|
# File 'lib/crimson/object.rb', line 166
def preordered_each
node.preordered_each do |subnode|
yield(subnode.content)
end
end
|
#remove(child) ⇒ Object
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/crimson/object.rb', line 99
def remove(child)
raise ArgumentError unless child.is_a?(Crimson::Object)
raise ArgumentError unless children.include?(child)
node.remove!(child.node)
self[:children] = children.map(&:id)
removed_children << child
end
|
129
130
131
|
# File 'lib/crimson/object.rb', line 129
def root
node.root.content
end
|
41
42
43
|
# File 'lib/crimson/object.rb', line 41
def show
style.display = :block
end
|
#shown? ⇒ Boolean
33
34
35
|
# File 'lib/crimson/object.rb', line 33
def shown?
!hidden?
end
|
121
122
123
|
# File 'lib/crimson/object.rb', line 121
def siblings
node.siblings.map(&:content)
end
|
180
181
182
|
# File 'lib/crimson/object.rb', line 180
def to_s
id.to_s
end
|
#un(event, handler = nil) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/crimson/object.rb', line 64
def un(event, handler = nil)
raise ArgumentError unless event_handlers.key?(event)
event_handlers[event].delete(handler) if handler
if event_handlers[event].empty? || handler.nil?
event_handlers.delete(event)
end
self[:events] = event_handlers.keys
commit!(:events)
end
|