Module: XamplExample::ThingAsChild

Included in:
Thing, Things
Defined in:
lib/xamplr/handwritten/example.rb,
lib/xamplr/handwritten/hand-example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#thing_childObject

Returns the value of attribute thing_child.



35
36
37
# File 'lib/xamplr/handwritten/example.rb', line 35

def thing_child
  @thing_child
end

#thing_mapObject

Returns the value of attribute thing_map.



35
36
37
# File 'lib/xamplr/handwritten/example.rb', line 35

def thing_map
  @thing_map
end

Instance Method Details

#add_thing(thing) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xamplr/handwritten/example.rb', line 42

def add_thing(thing)
  index = thing.get_the_index
  if (nil == index) then
    throw "no index thing defined in : " << thing.to_xml
  end

  existing = @thing_map[index]
  remove_thing(existing) if existing

  @children << thing
  @thing_child << thing
  @thing_map[index] = thing
  thing.add_parent(self)
  changed
  return thing
end

#init_thing_as_childObject



37
38
39
40
# File 'lib/xamplr/handwritten/example.rb', line 37

def init_thing_as_child
  @thing_child = []
  @thing_map = {}
end

#new_thing(index) {|thing| ... } ⇒ Object

Yields:

  • (thing)


59
60
61
62
63
64
# File 'lib/xamplr/handwritten/example.rb', line 59

def new_thing(index)
  thing = Thing.new
  thing.set_the_index(index)
  yield(thing) if block_given?
  return add_thing(thing)
end

#remove_thing(thing) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xamplr/handwritten/example.rb', line 66

def remove_thing(thing)
  index = thing.get_the_index
  if (nil == index) then
    throw "no index thing defined in : " << thing.to_xml
  end

  existing = @thing_map[index]
  if existing != thing then
    throw "will only remove the thing if it is already in the thing map"
  end

  @thing_map.delete(index)
  @thing_child.delete(thing)
  @children.delete(thing)
end