Class: GridGenerator::BaseElement

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

Constant Summary collapse

COLOURS =
{
  fill: "#d0d0d0",
  stroke: "#404040"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points:, colour:, opacity: 1) ⇒ BaseElement

Returns a new instance of BaseElement.



8
9
10
11
12
# File 'lib/grid_generator/base_element.rb', line 8

def initialize(points:, colour: , opacity: 1)
  @points = points
  @colour = colour
  @opacity = opacity
end

Instance Attribute Details

#colourObject (readonly)

Returns the value of attribute colour.



14
15
16
# File 'lib/grid_generator/base_element.rb', line 14

def colour
  @colour
end

#opacityObject (readonly)

Returns the value of attribute opacity.



14
15
16
# File 'lib/grid_generator/base_element.rb', line 14

def opacity
  @opacity
end

#pointsObject (readonly)

Returns the value of attribute points.



14
15
16
# File 'lib/grid_generator/base_element.rb', line 14

def points
  @points
end

Instance Method Details

#==(other) ⇒ Object



16
17
18
19
20
21
# File 'lib/grid_generator/base_element.rb', line 16

def ==(other)
  self.class == other.class &&
    self.points == other.points &&
    self.colour == other.colour &&
    self.opacity == other.opacity
end

#as_jsonObject



27
28
29
30
31
32
33
# File 'lib/grid_generator/base_element.rb', line 27

def as_json
  {
    "points_string" => points_string,
    "colour" => colour,
    "opacity" => opacity
  }
end

#points_stringObject



23
24
25
# File 'lib/grid_generator/base_element.rb', line 23

def points_string
  points.map { |p| "#{p[0,0].round},#{p[1,0].round}" }.join(' ')
end

#to_svgObject



35
36
37
# File 'lib/grid_generator/base_element.rb', line 35

def to_svg
  "<polygon points=\"#{points_string}\" style=\"fill:#{colour};stroke:#{COLOURS[:stroke]};stroke-width:1;opacity:#{opacity}\" />"
end