Module: Wee::DecorationMixin

Included in:
Component
Defined in:
lib/wee/decoration.rb

Overview

class Decoration

Instance Method Summary collapse

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



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
122
# File 'lib/wee/decoration.rb', line 97

def add_decoration(d)
  if d.global?
    d.next = self.decoration
    self.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 = self.decoration
      self.decoration = d
    else
      # add after last_global
      d.next = last_global.next
      last_global.next = d
    end
  end

  return self
end

#decorationObject



75
# File 'lib/wee/decoration.rb', line 75

def decoration() @decoration || self end

#decoration=(d) ⇒ Object



74
# File 'lib/wee/decoration.rb', line 74

def decoration=(d) @decoration = d end

#each_decorationObject

Iterates over all decorations (note that the component itself is excluded).



80
81
82
83
84
85
86
# File 'lib/wee/decoration.rb', line 80

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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wee/decoration.rb', line 130

def remove_decoration(d)
  if d == self.decoration  # 'd' is in front
    self.decoration = d.next
  else
    last_decoration = self.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_ifObject

Remove all decorations that match the block condition.

Example (removes all decorations of class HaloDecoration):

remove_decoration_if {|d| d.class == HaloDecoration}


155
156
157
158
159
# File 'lib/wee/decoration.rb', line 155

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