Module: SinatraHelpers::Less

Defined in:
lib/sinatra_helpers/less.rb,
lib/sinatra_helpers/less/erb.rb,
lib/sinatra_helpers/less/config.rb

Defined Under Namespace

Modules: Erb Classes: Config

Constant Summary collapse

CONTENT_TYPE =
"text/css"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.appObject (readonly)

Returns the value of attribute app.



16
17
18
# File 'lib/sinatra_helpers/less.rb', line 16

def app
  @app
end

Class Method Details

.[](config_attribute) ⇒ Object



26
27
28
# File 'lib/sinatra_helpers/less.rb', line 26

def [](config_attribute)
  config.send(config_attribute)
end

.compile(css_name, file_paths) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sinatra_helpers/less.rb', line 69

def compile(css_name, file_paths)
  compiled_less = file_paths.collect do |file_path|
    Less::Engine.new(File.new(file_path)).to_css
  end.join("\n")
  compiled_less.delete!("\n") if use_compression?
  if SinatraHelpers.page_cache?(SinatraHelpers::Less.app)
    pub_path = File.join(SinatraHelpers::Less.app.public, SinatraHelpers::Less[:hosted_root])
    FileUtils.mkdir_p(pub_path)
    hosted = File.join(pub_path, "#{css_name}.css")
    File.open(hosted, "w") do |file|
      file.write(compiled_less)
    end
  end
  compiled_less
end

.config {|instance_variable_get("@config")| ... } ⇒ Object

Yields:

  • (instance_variable_get("@config"))


18
19
20
21
22
23
24
# File 'lib/sinatra_helpers/less.rb', line 18

def config
  if instance_variable_get("@config").nil?
    instance_variable_set("@config", SinatraHelpers::Less::Config.new)
  end
  yield instance_variable_get("@config") if block_given?
  instance_variable_get("@config")
end

.registered(app) ⇒ Object



30
31
32
33
34
35
36
37
38
39
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
# File 'lib/sinatra_helpers/less.rb', line 30

def registered(app)
  instance_variable_set("@app", app)

  app.get "/less-sinatra-helper-test" do
    if SinatraHelpers::Less.config
      "The LESS sinatra helper is configured and ready :)"
    else
      "The LESS sinatra helper is NOT configured and ready :("
    end
  end

  app.get "#{SinatraHelpers::Less[:hosted_root]}/*.css" do
    file_name = params['splat'].first.to_s
    less_path = File.join([
      app.root, SinatraHelpers::Less[:src_root], "#{file_name}.less"
    ])
    css_path = File.join([
      app.root, SinatraHelpers::Less[:src_root], "#{file_name}.css"
    ])

    content_type CONTENT_TYPE

    if File.exists?(less_path)
      SinatraHelpers::Less.compile(file_name, [less_path])
    elsif File.exists?(css_path)
      SinatraHelpers::Less.compile(file_name, [css_path])
    elsif SinatraHelpers::Less[:concat].include?(file_name)
      less_paths = SinatraHelpers::Less[:concat][file_name].collect do |concat_name|
        File.join(app.root, SinatraHelpers::Less[:src_root], "#{concat_name}.less")
      end.select do |less_path|
        File.exists?(less_path)
      end
      SinatraHelpers::Less.compile(file_name, less_paths)
    else
      halt SinatraHelpers::HTTP_STATUS[:not_found]
    end
  end    
end

.use_compression?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/sinatra_helpers/less.rb', line 85

def use_compression?
  SinatraHelpers::Less[:compress]
end