Class: Prefab::JavaScriptStub

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/javascript_stub.rb

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(self)
CAMELS =
{}

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ JavaScriptStub

Returns a new instance of JavaScriptStub.



8
9
10
# File 'lib/prefab/javascript_stub.rb', line 8

def initialize(client = nil)
  @client = client || Prefab.instance
end

Instance Method Details

#bootstrap(context) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/prefab/javascript_stub.rb', line 12

def bootstrap(context)
  configs, warnings = data(context, :bootstrap)
  <<~JS
    window._prefabBootstrap = {
      evaluations: #{JSON.dump(configs)},
      context: #{JSON.dump(context)}
    }
    #{log_warnings(warnings)}
  JS
end

#generate_stub(context, callback = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/prefab/javascript_stub.rb', line 23

def generate_stub(context, callback = nil)
  configs, warnings = data(context, :stub)
  <<~JS
    window.prefab = window.prefab || {};
    window.prefab.config = #{JSON.dump(configs)};
    window.prefab.get = function(key) {
      var value = window.prefab.config[key];
    #{callback && "  #{callback}(key, value);"}
      return value;
    };
    window.prefab.isEnabled = function(key) {
      var value = window.prefab.config[key] === true;
    #{callback && "  #{callback}(key, value);"}
      return value;
    };
    #{log_warnings(warnings)}
  JS
end