Class: Lucy::JavascriptGenerator
- Inherits:
-
Object
- Object
- Lucy::JavascriptGenerator
- Defined in:
- lib/lucy.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #init_namespace? ⇒ Boolean
-
#initialize {|_self| ... } ⇒ JavascriptGenerator
constructor
A new instance of JavascriptGenerator.
- #to_a ⇒ Object
- #to_js ⇒ Object
- #write(key, obj) ⇒ Object (also: #[]=)
- #write_raw(data) ⇒ Object (also: #<<)
Constructor Details
#initialize {|_self| ... } ⇒ JavascriptGenerator
Returns a new instance of JavascriptGenerator.
31 32 33 34 35 |
# File 'lib/lucy.rb', line 31 def initialize @keyed = [] @raw = [] yield self if block_given? end |
Instance Attribute Details
#global ⇒ Object
61 62 63 |
# File 'lib/lucy.rb', line 61 def global @global || DEFAULT_GLOBAL end |
#init_namespace ⇒ Object
65 66 67 |
# File 'lib/lucy.rb', line 65 def init_namespace "if (!#{global}.#{namespace}) { #{namespace} = {}; }" end |
#namespace ⇒ Object
57 58 59 |
# File 'lib/lucy.rb', line 57 def namespace @namespace || DEFAULT_NAMESPACE end |
Instance Method Details
#init_namespace? ⇒ Boolean
69 70 71 |
# File 'lib/lucy.rb', line 69 def init_namespace? @init_namespace.nil? ? true : @init_namespace end |
#to_a ⇒ Object
47 48 49 50 51 |
# File 'lib/lucy.rb', line 47 def to_a (init_namespace? ? [init_namespace] : []) + @keyed.map do |key, value| "#{namespace}.#{[key].flatten.join('.')} = #{value};" end + @raw end |
#to_js ⇒ Object
53 54 55 |
# File 'lib/lucy.rb', line 53 def to_js to_a.join("\n\n") end |
#write(key, obj) ⇒ Object Also known as: []=
37 38 39 |
# File 'lib/lucy.rb', line 37 def write(key, obj) @keyed << [key, ActiveSupport::JSON.encode(obj)] end |
#write_raw(data) ⇒ Object Also known as: <<
42 43 44 |
# File 'lib/lucy.rb', line 42 def write_raw(data) @raw << data end |