Module: Wee::DecorationMixin
- Included in:
- Component
- Defined in:
- lib/wee/decoration.rb
Overview
class Decoration
Instance Attribute Summary collapse
-
#decoration ⇒ Object
Returns the value of attribute decoration.
Instance Method Summary collapse
-
#add_decoration(d) ⇒ Object
Adds decoration
dto the decoration chain. -
#each_decoration ⇒ Object
Iterates over all decorations (note that the component itself is excluded).
-
#remove_decoration(d) ⇒ Object
Remove decoration
dfrom the decoration chain. -
#remove_decoration_if ⇒ Object
Remove all decorations that match the block condition.
Instance Attribute Details
#decoration ⇒ Object
Returns the value of attribute decoration.
74 75 76 |
# File 'lib/wee/decoration.rb', line 74 def decoration @decoration end |
Instance Method Details
#add_decoration(d) ⇒ Object
Adds decoration d to the decoration chain.
A global decoration is added in front of the decoration chain, a local decoration is added in front of all other local decorations but after all global decorations.
Returns: self
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/wee/decoration.rb', line 96 def add_decoration(d) if d.global? d.next = @decoration @decoration = d else last_global = nil each_decoration {|i| if i.global? last_global = i else break end } if last_global.nil? # no global decorations specified -> add in front d.next = @decoration @decoration = d else # add after last_global d.next = last_global.next last_global.next = d end end return self end |
#each_decoration ⇒ Object
Iterates over all decorations (note that the component itself is excluded).
79 80 81 82 83 84 85 |
# File 'lib/wee/decoration.rb', line 79 def each_decoration # :yields: decoration d = @decoration while d and d != self yield d d = d.next end end |
#remove_decoration(d) ⇒ Object
Remove decoration d from the decoration chain.
Returns the removed decoration or nil if it did not exist in the decoration chain.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/wee/decoration.rb', line 129 def remove_decoration(d) if d == @decoration # 'd' is in front @decoration = d.next else last_decoration = @decoration next_decoration = nil loop do return nil if last_decoration == self or last_decoration.nil? next_decoration = last_decoration.next break if d == next_decoration last_decoration = next_decoration end last_decoration.next = d.next end d.next = nil # decoration 'd' no longer is an owner of anything! return d end |
#remove_decoration_if ⇒ Object
Remove all decorations that match the block condition.
Example (removes all decorations of class HaloDecoration):
remove_decoration_if {|d| d.class == HaloDecoration}
154 155 156 157 158 |
# File 'lib/wee/decoration.rb', line 154 def remove_decoration_if # :yields: decoration to_remove = [] each_decoration {|d| to_remove << d if yield d} to_remove.each {|d| remove_decoration(d)} end |