Module: Kinda::Composable

Includes:
Core
Defined in:
lib/composable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



55
56
57
# File 'lib/composable.rb', line 55

def children
  @children
end

Instance Method Details

#__parent_equal__(new_parent) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/composable.rb', line 37

def __parent_equal__(new_parent)
  if old_parent = @parent
    old_parent.children.delete(self)
    old_parent.try(:child_removed, self)
  end

  @parent = new_parent
  
  if new_parent
    new_parent.children << self
    new_parent.try(:child_added, self)
  end
end

#add_child(child) ⇒ Object Also known as: <<



8
9
10
11
# File 'lib/composable.rb', line 8

def add_child(child)
  child.parent = self
  child
end

#child(klass = nil) ⇒ Object



51
52
53
# File 'lib/composable.rb', line 51

def child(klass=nil)
  children.find { |child| klass.nil? || child.kind_of?(klass) }
end

#children?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/composable.rb', line 59

def children?
  !children.empty?
end

#parent(klass = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/composable.rb', line 19

def parent(klass=nil)
  result = @parent
  if klass
    until result.nil? || result.kind_of?(klass) do
      result = result.parent
    end
  end
  result
end

#parent=(new_parent) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/composable.rb', line 29

def parent=(new_parent)
  if respond_to?(:fire)
    fire(:parent) { __parent_equal__(new_parent) }
  else
    __parent_equal__(new_parent)
  end
end

#remove_child(child) ⇒ Object



15
16
17
# File 'lib/composable.rb', line 15

def remove_child(child)
  child.parent = nil
end