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

#initializeSet

Returns a new instance of Set.



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

def initialize(*)
  set_values(*)
end

Dynamic Method Handling

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

Instance Method Details

#+(other) ⇒ Object



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

def +(other)
  self.class.new(@set, *other)
end

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

the way to add items to the set.



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

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



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

def each(&)
  @set.each(&)
end

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

the way to remove items from the set.



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

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.



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

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

#find_by_names(*names) ⇒ Object

returns an array of items with the provided names



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

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

#initialize_dup(other) ⇒ Object



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

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

#set_valuesObject



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

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