Class: Volute::VoluteArray

Inherits:
Array
  • Object
show all
Defined in:
lib/volute.rb

Overview

Subclassing Array to add a #filter and a #remove method, so that things like

all_the_volutes = volutes
volutes_about_class_x = volutes(x)

volutes.remove(x)
  # just removed all the volutes referring class or attribute x

Instance Method Summary collapse

Instance Method Details

#filter(arg) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/volute.rb', line 139

def filter(arg)

  select { |args, block|

    classes = args.select { |a| a.is_a?(Class) }

    if args.include?(arg)
      true
    elsif arg.is_a?(Class)
      (arg.ancestors & classes).size > 0
    elsif arg.is_a?(Module)
      (arg.constants.collect { |c| arg.const_get(c) } & classes).size > 0
    #elsif arg.is_a?(Symbol)
      # already handled by the initial if
    else
      false
    end
  }
end

#remove(arg) ⇒ Object



159
160
161
162
163
164
# File 'lib/volute.rb', line 159

def remove(arg)

  filtered = filter(arg)

  reject! { |volute| filtered.include?(volute) }
end