Class: ActiveScaffold::DataStructures::Set

Inherits:
Object
  • Object
show all
Includes:
Configurable, Enumerable
Defined in:
lib/active_scaffold/data_structures/set.rb

Direct Known Subclasses

ActionColumns

Instance Method Summary collapse

Methods included from Configurable

#configure, #method_missing

Constructor Details

#initialize(*args) ⇒ Set

Returns a new instance of Set.



6
7
8
9
# File 'lib/active_scaffold/data_structures/set.rb', line 6

def initialize(*args)
  @set = []
  self.add *args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveScaffold::Configurable

Instance Method Details

#add(*args) ⇒ Object Also known as: <<

the way to add items to the set.



12
13
14
15
16
17
18
# File 'lib/active_scaffold/data_structures/set.rb', line 12

def add(*args)
  args.flatten! # allow [] as a param
  args.each { |arg|
    arg = arg.to_sym if arg.is_a? String
    @set << arg unless @set.include? arg # avoid duplicates
  }
end

#eachObject



43
44
45
# File 'lib/active_scaffold/data_structures/set.rb', line 43

def each
  @set.each {|i| yield i }
end

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/active_scaffold/data_structures/set.rb', line 52

def empty?
  @set.empty?
end

#exclude(*args) ⇒ Object Also known as: remove

the way to remove items from the set.



22
23
24
25
26
27
# File 'lib/active_scaffold/data_structures/set.rb', line 22

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

#find_by_name(name) ⇒ Object Also known as: []

returns the item of the given name.



36
37
38
39
40
# File 'lib/active_scaffold/data_structures/set.rb', line 36

def find_by_name(name)
  # this works because of `def item.=='
  item = @set.find { |c| c == name }
  item
end

#find_by_names(*names) ⇒ Object

returns an array of items with the provided names



31
32
33
# File 'lib/active_scaffold/data_structures/set.rb', line 31

def find_by_names(*names)
  @set.find_all { |item| names.include? item }
end

#lengthObject

returns the number of items in the set



48
49
50
# File 'lib/active_scaffold/data_structures/set.rb', line 48

def length
  @set.length
end