class Crimson::Object

Attributes

added_children[R]
event_handlers[R]
id[R]
node[R]
removed_children[R]
tag[R]

Public Class Methods

new(tag) click to toggle source
Calls superclass method Crimson::Model::new
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/crimson/object.rb, line 184
def ==(other)
  other.is_a?(Object) && other.id == id
end
add(child, at_index = -1) click to toggle source
# 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() { |content| ... } click to toggle source
# File lib/crimson/object.rb, line 154
def breadth_each
  node.breadth_each do |subnode|
    yield(subnode.content)
  end
end
children() click to toggle source
# File lib/crimson/object.rb, line 125
def children
  node.children.map(&:content)
end
commit_tree!(*keys) click to toggle source
# File lib/crimson/object.rb, line 133
def commit_tree!(*keys)
  # TODO: Unsure if this algorithm works for moved objects
  # eg. added, removed, then added again, under the same commit.

  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) click to toggle source
# File lib/crimson/object.rb, line 188
def eql?(other)
  self == other
end
find_descendant(descendent_id) click to toggle source
# File lib/crimson/object.rb, line 172
def find_descendant(descendent_id)
  breadth_each { |descendent| return descendent if descendent_id == descendent.id }
end
hash() click to toggle source
# File lib/crimson/object.rb, line 192
def hash
  id.hash
end
hidden?() click to toggle source
# File lib/crimson/object.rb, line 29
def hidden?
  style.display.to_sym == :none
end
hide() click to toggle source
# File lib/crimson/object.rb, line 37
def hide
  style.display = :none
end
inspect() click to toggle source
# File lib/crimson/object.rb, line 176
def inspect
  to_s
end
move(child, at_index) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
parent() click to toggle source
# File lib/crimson/object.rb, line 77
def parent
  node.parent&.content
end
parent=(new_parent) click to toggle source
# 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() { |content| ... } click to toggle source
# File lib/crimson/object.rb, line 160
def postordered_each
  node.postordered_each do |subnode|
    yield(subnode.content)
  end
end
preordered_each() { |content| ... } click to toggle source
# File lib/crimson/object.rb, line 166
def preordered_each
  node.preordered_each do |subnode|
    yield(subnode.content)
  end
end
remove(child) click to toggle source
# 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
root() click to toggle source
# File lib/crimson/object.rb, line 129
def root
  node.root.content
end
show() click to toggle source
# File lib/crimson/object.rb, line 41
def show
  style.display = :block
end
shown?() click to toggle source
# File lib/crimson/object.rb, line 33
def shown?
  !hidden?
end
siblings() click to toggle source
# File lib/crimson/object.rb, line 121
def siblings
  node.siblings.map(&:content)
end
to_s() click to toggle source
# File lib/crimson/object.rb, line 180
def to_s
  id.to_s
end
un(event, handler = nil) click to toggle source
# 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