Module: SinatraHelpers::Sprockets

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

Defined Under Namespace

Modules: Erb Classes: Config

Constant Summary collapse

CONTENT_TYPE =
"text/javascript; charset=utf-8"

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/sprockets.rb', line 16

def app
  @app
end

Class Method Details

.[](config_attribute) ⇒ Object



38
39
40
# File 'lib/sinatra_helpers/sprockets.rb', line 38

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

.compile(src_file_name) ⇒ Object



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

def compile(src_file_name)
  secretary_params = SinatraHelpers::Sprockets.secretary do |params|
    params[:source_files] = [src_file_name]
  end
  compiled_src = Sprockets::Secretary.new(secretary_params).concatenation.to_s
  if SinatraHelpers.page_cache?(SinatraHelpers::Sprockets.app)
    pub_path = File.join(SinatraHelpers::Sprockets.app.public, SinatraHelpers::Sprockets[:hosted_root])
    FileUtils.mkdir_p(pub_path)
    hosted = File.join(pub_path, File.basename(src_file_name))
    File.open(hosted, "w") do |file|
      file.write(compiled_src)
    end
  end
  compiled_src
end

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

Yields:

  • (instance_variable_get("@config"))


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

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

.registered(app) ⇒ Object



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
# File 'lib/sinatra_helpers/sprockets.rb', line 42

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

  app.get "/sprockets-sinatra-helper-test" do
    if SinatraHelpers::Sprockets.config
      "The sprockets sinatra helper is configured and ready :)"
    else
      "The sprockets sinatra helper is NOT configured and ready :("
    end
  end
  
  app.get "#{SinatraHelpers::Sprockets[:hosted_root]}/*.js" do
    src_name = params['splat'].first.to_s
    src_file_path = File.join([
      SinatraHelpers::Sprockets[:src_root], "#{src_name}.js"
    ])
    src_path = File.join(app.root, src_file_path)

    content_type CONTENT_TYPE

    if File.exists?(src_path)
      SinatraHelpers::Sprockets.compile(src_file_path)
    else
      halt SinatraHelpers::HTTP_STATUS[:not_found]
    end
  end    
end

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

Yields:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sinatra_helpers/sprockets.rb', line 26

def secretary
  if instance_variable_get("@secretary").nil?
    instance_variable_set("@secretary", {
      :root => app.root,
      :load_path => config.load_path,
      :expand_paths => config.expand_paths
    })
  end
  yield instance_variable_get("@secretary") if block_given?
  instance_variable_get("@secretary")      
end