Class: DiamondLang::Helpers::Coordinates

Inherits:
Object
  • Object
show all
Defined in:
lib/diamond-lang/helpers/coordinates.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Coordinates

Returns a new instance of Coordinates.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/diamond-lang/helpers/coordinates.rb', line 4

def initialize(*args)
  @coordinates, args = if args.first.is_a? Coordinate
    args.slice_before{|a| a.is_a? Coordinate }
  else
    axes_args = args.take Constants::Axes.length
    [
      axes_args.zip(Constants::Axes).flat_map do |value, axis|
        Coordinate.new axis, value
      end,
      args.drop(axes_args.length+1)
    ]
  end
end

Instance Method Details

#[](axis) ⇒ Object



17
18
19
# File 'lib/diamond-lang/helpers/coordinates.rb', line 17

def [](axis)
  @coordinates.select{|coord| coord.axis == axis}[0]
end

#to_argObject



35
36
37
# File 'lib/diamond-lang/helpers/coordinates.rb', line 35

def to_arg
  @coordinates.map {|coord| coord.to_arg}.join ','
end

#to_fs(format) ⇒ Object



32
33
34
# File 'lib/diamond-lang/helpers/coordinates.rb', line 32

def to_fs(format)
  format.gsub(/{(\w)}/) {|match| self[match[1]].value}
end

#to_sObject



29
30
31
# File 'lib/diamond-lang/helpers/coordinates.rb', line 29

def to_s
  self.to_fs "{x} {y} {z}".freeze
end

#xObject



20
21
22
# File 'lib/diamond-lang/helpers/coordinates.rb', line 20

def x
  self['x'.freeze]
end

#yObject



23
24
25
# File 'lib/diamond-lang/helpers/coordinates.rb', line 23

def y
  self['y'.freeze]
end

#zObject



26
27
28
# File 'lib/diamond-lang/helpers/coordinates.rb', line 26

def z
  self['z'.freeze]
end