Class: Mapel::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/mapel.rb

Constant Summary collapse

FLAGS =
['', '%', '<', '>', '!', '@', '^']
RE =

Regex parser for geometry strings

/\A(\d*)(?:x(\d+)?)?([-+]\d+)?([-+]\d+)?([%!<>@\^]?)\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Geometry

Returns a new instance of Geometry.

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mapel.rb', line 127

def initialize(*args)
  if (args.length == 1) && (args.first.kind_of?(String))
    raise(ArgumentError, "Invalid geometry string") unless m = RE.match(args.first)
    args = m.to_a[1..5]
  end
  @width = args[0] ? args[0].to_i.round : 0
  @height = args[1] ? args[1].to_i.round : 0
  raise(ArgumentError, "Width must be >= 0") if @width < 0
  raise(ArgumentError, "Height must be >= 0") if @height < 0
  @x = args[2] ? args[2].to_i : 0
  @y = args[3] ? args[3].to_i : 0
  raise(ArgumentError, "Flags must be in: #{FLAGS.inspect}") if args[4] && !FLAGS.include?(args[4])
  @flag = args[4]
end

Instance Attribute Details

#flagObject

Returns the value of attribute flag.



120
121
122
# File 'lib/mapel.rb', line 120

def flag
  @flag
end

#heightObject

Returns the value of attribute height.



120
121
122
# File 'lib/mapel.rb', line 120

def height
  @height
end

#widthObject

Returns the value of attribute width.



120
121
122
# File 'lib/mapel.rb', line 120

def width
  @width
end

#xObject

Returns the value of attribute x.



120
121
122
# File 'lib/mapel.rb', line 120

def x
  @x
end

#yObject

Returns the value of attribute y.



120
121
122
# File 'lib/mapel.rb', line 120

def y
  @y
end

Instance Method Details

#dimensionsObject



142
143
144
# File 'lib/mapel.rb', line 142

def dimensions
  [width, height]
end

#to_s(crop = false) ⇒ Object

Convert object to a geometry string



147
148
149
150
151
152
153
154
155
# File 'lib/mapel.rb', line 147

def to_s(crop = false)
  str = ''
  str << "%g" % @width if @width > 0
  str << 'x' if @height > 0
  str << "%g" % @height if @height > 0
  str << "%+d%+d" % [@x, @y] if (@x != 0 || @y != 0 || crop)
  str << @flag if @flag
  str
end