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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#configure, #method_missing

Constructor Details

#initialize(*args) ⇒ Set

Returns a new instance of Set.



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

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 Attribute Details

#labelObject



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

def label
  as_(@label)
end

Instance Method Details

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

the way to add items to the set.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

#eachObject



58
59
60
# File 'lib/active_scaffold/data_structures/set.rb', line 58

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

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/active_scaffold/data_structures/set.rb', line 67

def empty?
  @set.empty?
end

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

the way to remove items from the set.



37
38
39
40
41
42
# 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

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

returns the item of the given name.



51
52
53
54
55
# 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

#find_by_names(*names) ⇒ Object

returns an array of items with the provided names



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

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

#lengthObject

returns the number of items in the set



63
64
65
# File 'lib/active_scaffold/data_structures/set.rb', line 63

def length
  @set.length
end