Class: Compass::GridBuilder

Inherits:
Object
  • Object
show all
Includes:
Actions
Defined in:
lib/compass/grid_builder.rb

Overview

Uses ImageMagick and RMagick to generate grid.png file

Instance Attribute Summary collapse

Attributes included from Actions

#logger

Instance Method Summary collapse

Methods included from Actions

#basename, #compile, #copy, #directory, #relativize, #separate, #strip_trailing_separator, #write_file

Constructor Details

#initialize(options = {}) ⇒ GridBuilder

Options

  • options

    • :column_width – Width (in pixels) of current grid column

    • :gutter_width – Width (in pixels) of current grid gutter

    • :output_path – Output path of grid.png file



26
27
28
29
30
31
32
33
# File 'lib/compass/grid_builder.rb', line 26

def initialize(options={})
  @able_to_generate = Magick::Long_version rescue false
  return unless @able_to_generate
  @column_width = options[:column_width]
  @gutter_width = options[:gutter_width]
  @output_path  = options[:output_path]
  @options = options
end

Instance Attribute Details

#able_to_generateObject (readonly)

Returns the value of attribute able_to_generate.



19
20
21
# File 'lib/compass/grid_builder.rb', line 19

def able_to_generate
  @able_to_generate
end

#column_widthObject (readonly)

Returns the value of attribute column_width.



19
20
21
# File 'lib/compass/grid_builder.rb', line 19

def column_width
  @column_width
end

#gutter_widthObject (readonly)

Returns the value of attribute gutter_width.



19
20
21
# File 'lib/compass/grid_builder.rb', line 19

def gutter_width
  @gutter_width
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/compass/grid_builder.rb', line 19

def options
  @options
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



19
20
21
# File 'lib/compass/grid_builder.rb', line 19

def output_path
  @output_path
end

Instance Method Details

#generate!Object

generates (overwriting if necessary) grid.png image to be tiled in background



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/compass/grid_builder.rb', line 40

def generate!
  return false unless self.able_to_generate
  total_width = self.column_width + self.gutter_width
  height = 20
  RVG::dpi = 100

  rvg = RVG.new((total_width.to_f/RVG::dpi).in, (height.to_f/RVG::dpi).in).viewbox(0, 0, total_width, height) do |canvas|
    canvas.background_fill = 'white'

    canvas.g do |column|
      column.rect(self.column_width, height).styles(:fill => "#e8effb")
    end

    canvas.g do |baseline|
      baseline.line(0, (height - 1), total_width, (height- 1)).styles(:fill => "#e9e9e9")
    end
  end

  filename = File.join(self.output_path, "grid.png")
  if File.exists?(filename)
    if options[:force]
      overwrite = true
    else
      msg = "#{filename} already exists. Overwrite with --force."
      raise Compass::FilesystemConflict.new(msg)
    end
  end
  directory self.output_path
  logger.record((overwrite ? :overwrite : :create), basename(filename))
  rvg.draw.write(filename)
end

#working_pathObject



35
36
37
# File 'lib/compass/grid_builder.rb', line 35

def working_path
  options[:working_path]
end