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, #process_erb, #relativize, #remove, #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

    • :height – Height (in pixels) of a row

    • :filename – Output path of grid.png file



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

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]
  @height = options[:height] || 20
  @filename = options[:filename]
  @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

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
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

Instance Method Details

#generate!Object

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



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
71
72
73
74
# File 'lib/compass/grid_builder.rb', line 42

def generate!
  return false unless self.able_to_generate
  total_width = self.column_width + self.gutter_width
  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 - 1, @height).styles(:fill => "#e8effb")
    end

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

  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 File.dirname(filename)
  logger.record((overwrite ? :overwrite : :create), basename(filename))
  unless options[:dry_run]
    rvg.draw.write(filename)
  else
    true
  end
end

#working_pathObject



37
38
39
# File 'lib/compass/grid_builder.rb', line 37

def working_path
  options[:working_path]
end