Class: Collection
- Defined in:
- lib/lib/collection/lib/atom.rb,
lib/lib/collection/lib/enum.rb,
lib/lib/collection/lib/sort.rb,
lib/lib/collection/collection.rb,
lib/lib/collection/lib/rotate.rb,
lib/lib/collection/lib/selector.rb
Instance Attribute Summary collapse
-
#crystal ⇒ Object
Returns the value of attribute crystal.
Instance Method Summary collapse
- #<<(arg) ⇒ Object
- #[](i, j = nil) ⇒ Object
- #[]=(index, atom) ⇒ Object
- #clone ⇒ Object
- #dup ⇒ Object
-
#each ⇒ Object
include Enumerable.
- #fixed=(bool = true) ⇒ Object
-
#initialize(*args) ⇒ Collection
constructor
A new instance of Collection.
- #length ⇒ Object
- #map(&block) ⇒ Object
- #map! ⇒ Object
- #move(v, n = 1) ⇒ Object
- #move!(v, n = 1) ⇒ Object
- #name=(str) ⇒ Object
- #pop(n = 1) ⇒ Object
- #reject(str = nil, &block) ⇒ Object
- #reject!(str = nil, &block) ⇒ Object
- #remove(arg) ⇒ Object (also: #delete)
- #rotate(*args) ⇒ Object
- #rotate!(d, o = Vector[0,0,0], a) ⇒ Object
- #select(str = nil, &block) ⇒ Object
- #select!(str = nil, &block) ⇒ Object
- #sort(*keys) ⇒ Object
- #sort!(*keys) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #type=(str) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 14 |
# File 'lib/lib/collection/collection.rb', line 10 def initialize *args @collection = [] @tol = 0 [*args].each{ |arg| self << arg } end |
Instance Attribute Details
#crystal ⇒ Object
Returns the value of attribute crystal.
9 10 11 |
# File 'lib/lib/collection/collection.rb', line 9 def crystal @crystal end |
Instance Method Details
#<<(arg) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lib/collection/collection.rb', line 15 def << arg if arg.is_a? Atom @collection << arg unless @collection.include? arg return self elsif arg.is_a? Collection @collection += arg.to_a @collection.uniq! return self elsif arg.is_a? Array @collection += arg if arg.all?{|v| v.is_a? Atom} @collection.uniq! return self end return false end |
#[](i, j = nil) ⇒ Object
34 35 36 |
# File 'lib/lib/collection/collection.rb', line 34 def [] i, j = nil j.nil?? @collection[i] : Collection.new.tap{|t| t << @collection[i,j]} end |
#[]=(index, atom) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/lib/collection/collection.rb', line 37 def []= index, atom if atom.is_a?(Atom) @collection[index]=atom @collection = @collection.uniq.compact true end false end |
#clone ⇒ Object
16 17 18 |
# File 'lib/lib/collection/lib/enum.rb', line 16 def clone Collection.new.tap{|t| t << @collection } end |
#dup ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/lib/collection/lib/enum.rb', line 19 def dup Collection.new.tap do |t| @collection.each do |e| t << e.dup end end end |
#each ⇒ Object
include Enumerable
3 4 5 6 7 8 9 |
# File 'lib/lib/collection/lib/enum.rb', line 3 def each if block_given? @collection.each { |e| yield(e) } else Enumerator.new(self, :each) end end |
#fixed=(bool = true) ⇒ Object
9 10 11 |
# File 'lib/lib/collection/lib/atom.rb', line 9 def fixed= bool = true @collection.each{|atom| atom.fixed = bool} end |
#length ⇒ Object
30 |
# File 'lib/lib/collection/collection.rb', line 30 def length() @collection.length end |
#map(&block) ⇒ Object
13 14 15 |
# File 'lib/lib/collection/lib/enum.rb', line 13 def map &block dup.map!(&block) end |
#map! ⇒ Object
10 11 12 |
# File 'lib/lib/collection/lib/enum.rb', line 10 def map! @collection.map!{|c| yield(c); c} end |
#move(v, n = 1) ⇒ Object
6 7 8 |
# File 'lib/lib/collection/lib/atom.rb', line 6 def move v, n=1 dup.move! v, n end |
#move!(v, n = 1) ⇒ Object
2 3 4 5 |
# File 'lib/lib/collection/lib/atom.rb', line 2 def move! v, n=1 @collection.each{|atom| atom.move! v, n} return self end |
#name=(str) ⇒ Object
15 16 17 |
# File 'lib/lib/collection/lib/atom.rb', line 15 def name= str @collection.each{|atom| atom.name = str} end |
#pop(n = 1) ⇒ Object
31 32 33 |
# File 'lib/lib/collection/collection.rb', line 31 def pop n = 1 @collection.pop n end |
#reject(str = nil, &block) ⇒ Object
15 16 17 |
# File 'lib/lib/collection/lib/selector.rb', line 15 def reject str = nil, &block dup.select! str, &block end |
#reject!(str = nil, &block) ⇒ Object
7 8 9 10 11 |
# File 'lib/lib/collection/lib/selector.rb', line 7 def reject! str = nil, &block @collection.reject!{|v| v.evaluate(str)} unless str.nil? @collection.reject!(&block) if block_given? self end |
#remove(arg) ⇒ Object Also known as: delete
29 30 31 |
# File 'lib/lib/collection/lib/enum.rb', line 29 def remove arg arg.is_a?(Atom)? @collection.delete(arg): @collection.delete_at(arg) end |
#rotate(*args) ⇒ Object
6 7 8 |
# File 'lib/lib/collection/lib/rotate.rb', line 6 def rotate *args dup.rotate! *args end |
#rotate!(d, o = Vector[0,0,0], a) ⇒ Object
2 3 4 5 |
# File 'lib/lib/collection/lib/rotate.rb', line 2 def rotate! d, o = Vector[0,0,0], a @collection.each{|v| v.pos = v.pos.rotate(d.to_v,o.to_v,a)} self end |
#select(str = nil, &block) ⇒ Object
12 13 14 |
# File 'lib/lib/collection/lib/selector.rb', line 12 def select str = nil, &block dup.select! str, &block end |
#select!(str = nil, &block) ⇒ Object
2 3 4 5 6 |
# File 'lib/lib/collection/lib/selector.rb', line 2 def select! str = nil, &block @collection.select!{|v| v.evaluate(str)} unless str.nil? @collection.select!(&block) if block_given? self end |
#sort(*keys) ⇒ Object
30 31 32 |
# File 'lib/lib/collection/lib/sort.rb', line 30 def sort *keys dup.sort! *keys end |
#sort!(*keys) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lib/collection/lib/sort.rb', line 2 def sort! *keys tol = [] tol << keys.pop while keys[-1].is_a? Numeric @collection.sort! do |a,b| result = 0 ks = keys.dup t = tol.dup while result == 0 and ks.length > 0 key = ks.shift t.push @tol if t.length == 0 case key when :type result = (a.type.name <=> b.type.name) when Symbol result = a.send(key) if result.is_a? Numeric result = result.approx_sort(b.send(key), t.pop) else result = result <=> b.send(key) end else result = (key.to_a * a.pos).approx_sort(key.to_a * b.pos, t.pop) end end result end return @collection end |
#to_a ⇒ Object
26 27 28 |
# File 'lib/lib/collection/lib/enum.rb', line 26 def to_a @collection end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/lib/collection/collection.rb', line 45 def to_s String.new.tap{|s| @collection.each{|e| s << e.to_s << "\n"}} end |
#type=(str) ⇒ Object
12 13 14 |
# File 'lib/lib/collection/lib/atom.rb', line 12 def type= str @collection.each{|atom| atom.type = str} end |