Class: ActiveScaffold::DataStructures::Set
- Includes:
- Configurable, Enumerable
- Defined in:
- lib/active_scaffold/data_structures/set.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#add(*args) ⇒ Object
(also: #<<)
the way to add items to the set.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#exclude(*args) ⇒ Object
(also: #remove)
the way to remove items from the set.
-
#find_by_name(name) ⇒ Object
(also: #[])
returns the item of the given name.
-
#find_by_names(*names) ⇒ Object
returns an array of items with the provided names.
-
#initialize(*args) ⇒ Set
constructor
A new instance of Set.
-
#length ⇒ Object
returns the number of items in the set.
Methods included from Configurable
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 |
#each ⇒ Object
43 44 45 |
# File 'lib/active_scaffold/data_structures/set.rb', line 43 def each @set.each {|i| yield i } end |
#empty? ⇒ 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 |
#length ⇒ Object
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 |