Class: Mapel::Geometry

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

Constant Summary collapse

FLAGS =
['', '%', '<', '>', '!', '@']
RFLAGS =
{
  '%' => :percent,
  '!' => :aspect,
  '<' => :<,
  '>' => :>,
  '@' => :area
}
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)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mapel.rb', line 147

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]
    args[4] = args[4] ? RFLAGS[args[4]] : nil
  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
  @flag = (args[4] && RFLAGS.has_value?(args[4])) ? args[4] : nil
end

Instance Attribute Details

#flagObject

Returns the value of attribute flag.



133
134
135
# File 'lib/mapel.rb', line 133

def flag
  @flag
end

#heightObject

Returns the value of attribute height.



133
134
135
# File 'lib/mapel.rb', line 133

def height
  @height
end

#widthObject

Returns the value of attribute width.



133
134
135
# File 'lib/mapel.rb', line 133

def width
  @width
end

#xObject

Returns the value of attribute x.



133
134
135
# File 'lib/mapel.rb', line 133

def x
  @x
end

#yObject

Returns the value of attribute y.



133
134
135
# File 'lib/mapel.rb', line 133

def y
  @y
end

Instance Method Details

#dimensionsObject



162
163
164
# File 'lib/mapel.rb', line 162

def dimensions
  [width, height]
end

#to_s(crop = false) ⇒ Object

Convert object to a geometry string



167
168
169
170
171
172
173
174
# File 'lib/mapel.rb', line 167

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 << (RFLAGS.respond_to?(:key) ? RFLAGS.key(@flag) : RFLAGS.index(@flag)).to_s
end