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
# File 'lib/active_scaffold/data_structures/set.rb', line 6

def initialize(*args)
  set_values(*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.



16
17
18
19
20
21
22
# File 'lib/active_scaffold/data_structures/set.rb', line 16

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

#eachObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/active_scaffold/data_structures/set.rb', line 56

def empty?
  @set.empty?
end

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

the way to remove items from the set.



26
27
28
29
30
31
# File 'lib/active_scaffold/data_structures/set.rb', line 26

def exclude(*args)
  args.flatten! # allow [] as a param
  args.collect!(&: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) && 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.



40
41
42
43
44
# File 'lib/active_scaffold/data_structures/set.rb', line 40

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



35
36
37
# File 'lib/active_scaffold/data_structures/set.rb', line 35

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

#lengthObject

returns the number of items in the set



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

def length
  @set.length
end

#set_values(*args) ⇒ Object



10
11
12
13
# File 'lib/active_scaffold/data_structures/set.rb', line 10

def set_values(*args)
  @set = []
  add *args
end