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, #respond_to_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.



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

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



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

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

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/active_scaffold/data_structures/set.rb', line 60

def empty?
  @set.empty?
end

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

the way to remove items from the set.



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

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.



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

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



39
40
41
# File 'lib/active_scaffold/data_structures/set.rb', line 39

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

#initialize_dup(other) ⇒ Object



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

def initialize_dup(other)
  @set = other.set.dup
end

#lengthObject

returns the number of items in the set



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

def length
  @set.length
end

#set_values(*args) ⇒ Object



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

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