class ActiveScaffold::DataStructures::Set

Attributes

label[W]

Public Class Methods

new(*args) click to toggle source
# File lib/active_scaffold/data_structures/set.rb, line 11
def initialize(*args)
  @set = []
  self.add *args
end

Public Instance Methods

<<(*args)
Alias for: add
[](name)
Alias for: find_by_name
add(*args) click to toggle source

the way to add items to the set.

# File lib/active_scaffold/data_structures/set.rb, line 17
def add(*args)
  args.flatten! # allow [] as a param

  index = args.pop if args.last().is_a? Numeric

  args.each { |arg|
    arg = arg.to_sym if arg.is_a? String
    unless @set.include? arg # avoid duplicates
      if index.nil?
        @set << arg
      else
        @set.insert(index,arg)
        index += 1
      end
    end
  }
end
Also aliased as: <<
each() { |i| ... } click to toggle source
# File lib/active_scaffold/data_structures/set.rb, line 58
def each
  @set.each {|i| yield i }
end
empty?() click to toggle source
# File lib/active_scaffold/data_structures/set.rb, line 67
def empty?
  @set.empty?
end
exclude(*args) click to toggle source

the way to remove items from the set.

# File lib/active_scaffold/data_structures/set.rb, line 37
def exclude(*args)
  args.flatten! # allow [] as a param
  args.collect! { |a| a.to_sym } # symbolize the args
  # check respond_to? :to_sym, ActionColumns doesn't respond to to_sym
  @set.reject! { |c| c.respond_to? :to_sym and args.include? c.to_sym } # reject all items specified
end
Also aliased as: remove
find_by_name(name) click to toggle source

returns the item of the given name.

# File lib/active_scaffold/data_structures/set.rb, line 51
def find_by_name(name)
  # this works because of `def item.=='
  item = @set.find { |c| c == name }
  item
end
Also aliased as: []
find_by_names(*names) click to toggle source

returns an array of items with the provided names

# File lib/active_scaffold/data_structures/set.rb, line 46
def find_by_names(*names)
  @set.find_all { |item| names.include? item }
end
label() click to toggle source
# File lib/active_scaffold/data_structures/set.rb, line 7
def label
  as_(@label)
end
length() click to toggle source

returns the number of items in the set

# File lib/active_scaffold/data_structures/set.rb, line 63
def length
  @set.length
end
remove(*args)
Alias for: exclude