Class: Jcompiler::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/jcompiler/provider.rb

Instance Method Summary collapse

Constructor Details

#initializeProvider

Returns a new instance of Provider.



2
3
4
# File 'lib/jcompiler/provider.rb', line 2

def initialize
  @context = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/jcompiler/provider.rb', line 38

def method_missing(name, *args, &block)
  if block
    @context << %{"\\"#{name}\\":\#{#{block.call}}"}
  else
    @context << %{"\\"#{name}\\":\#{(#{args[0]}).to_json}"}
  end
end

Instance Method Details

#array!(arg_src, &block) ⇒ Object



18
19
20
21
22
# File 'lib/jcompiler/provider.rb', line 18

def array!(arg_src, &block)
  item_name = block.parameters[0][1].to_s
  src = block.call(nil)
  %{"[\#{(#{arg_src}).map { |#{item_name}| #{src} }.join(',')}]"}
end

#extract!(obj_name, *key_names) ⇒ Object



32
33
34
35
36
# File 'lib/jcompiler/provider.rb', line 32

def extract!(obj_name, *key_names)
  key_names.each do |key_name|
    send(key_name, "#{obj_name}.#{key_name}")
  end
end

#number!(num) ⇒ Object



24
25
26
# File 'lib/jcompiler/provider.rb', line 24

def number!(num)
  %{#{num}.to_json}
end

#object!(arg_src = nil, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jcompiler/provider.rb', line 6

def object!(arg_src = nil, &block)
  pre_context = @context
  @context = []
  srcs, subst_src = if arg_src
                     [block.call(nil), "#{block.parameters[0][1]}=(#{arg_src});"]
                   else
                     [block.call, '']
                   end
  @context = pre_context
  %{"{\#{#{subst_src}[#{srcs.join(',')}].join(',')}}"}
end

#string!(str) ⇒ Object



28
29
30
# File 'lib/jcompiler/provider.rb', line 28

def string!(str)
  %{#{str}.to_json}
end