Class: DiamondLang::Helpers::Coordinate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(axis, v, relative = v.is_a?(String)&&v.include?('~')) ⇒ Coordinate

Returns a new instance of Coordinate.



5
6
7
8
9
10
# File 'lib/diamond-lang/helpers/coordinate.rb', line 5

def initialize(axis, v, relative=v.is_a?(String)&&v.include?('~'))
  raise Errors::InvalidAxis.new(axis) unless Constants::Axes.include? (axis = axis.to_s.downcase)
  @axis = axis
  self.value=(v)
  @relative = relative
end

Instance Attribute Details

#axisObject (readonly)

Returns the value of attribute axis.



4
5
6
# File 'lib/diamond-lang/helpers/coordinate.rb', line 4

def axis
  @axis
end

Instance Method Details

#+(amount) ⇒ Object



21
22
23
# File 'lib/diamond-lang/helpers/coordinate.rb', line 21

def +(amount)
  @value += amount
end

#-(amount) ⇒ Object



24
25
26
# File 'lib/diamond-lang/helpers/coordinate.rb', line 24

def -(amount)
  @value -= amount
end

#_valueObject



30
31
32
# File 'lib/diamond-lang/helpers/coordinate.rb', line 30

def _value
  @value
end

#_value=(value) ⇒ Object



27
28
29
# File 'lib/diamond-lang/helpers/coordinate.rb', line 27

def _value=(value)
  @value = value
end

#inspectObject



41
42
43
# File 'lib/diamond-lang/helpers/coordinate.rb', line 41

def inspect
  "#{self.class.to_s}(" + @axis + ": " + value + ")"
end

#to_argObject



37
38
39
40
# File 'lib/diamond-lang/helpers/coordinate.rb', line 37

def to_arg
  raise Errors::RelativeCordinateConvertedToArgument if @relative
  "#{@axis}=#{@value}".freeze
end

#valueObject Also known as: to_s



33
34
35
# File 'lib/diamond-lang/helpers/coordinate.rb', line 33

def value
  (@relative ? '~' : '') + @value.to_s
end

#value=(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/diamond-lang/helpers/coordinate.rb', line 11

def value=(value)
  @value = if value.is_a? Integer
    value
  elsif value.is_a?(String) && /^(?<rel>~)?(?<number>-?\d+)?$/ =~ value
    @relative = rel
    number.to_i
  else
    raise Errors::InvalidCoordinateValue.new value
  end
end