Class: TwitterCldr::Js::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/js/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler

Returns a new instance of Compiler.



17
18
19
20
21
22
23
24
# File 'lib/twitter_cldr/js/compiler.rb', line 17

def initialize(options = {})
  @locales = options[:locales] || TwitterCldr.supported_locales
  @features = options[:features] || implementation_renderers.keys
  @data = options[:features] || data_renderers.keys
  @test_helpers = options[:test_helpers] || test_helper_renderers.keys
  @prerender = options[:prerender].nil? ? true : options[:prerender]
  @source_map = options[:source_map]
end

Instance Attribute Details

#localesObject (readonly)

Returns the value of attribute locales.



15
16
17
# File 'lib/twitter_cldr/js/compiler.rb', line 15

def locales
  @locales
end

Instance Method Details

#compile_bundle(bundle, bundle_elements, bundle_hash, options = {}) ⇒ Object



26
27
28
29
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
# File 'lib/twitter_cldr/js/compiler.rb', line 26

def compile_bundle(bundle, bundle_elements, bundle_hash, options = {})
  contents = ""
  bundle_elements.each do |bundle_element|
    if renderer_const = bundle_hash[bundle_element]
      if bundle[:locale]
        contents << renderer_const.new(:locale => bundle[:locale], :prerender => @prerender).render
      else
        contents << renderer_const.new(:prerender => @prerender).render
      end
    end
  end

  bundle[:contents] = contents
  bundle[:source_map] = @source_map

  result = CoffeeScript.compile(bundle.render, {
    :bare => false,
    :sourceMap => @source_map
  })

  file = if @source_map
    CompiledFile.new(result["js"], result["sourceMap"])
  else
    CompiledFile.new(result)
  end

  file.source = post_process_file(file.source, options)
  file
end

#compile_each(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/twitter_cldr/js/compiler.rb', line 68

def compile_each(options = {})
  @locales.each do |locale|
    bundle = TwitterCldr::Js::Renderers::DataBundle.new
    bundle[:locale] = locale

    bundle[:contents] = data_renderers.inject({}) do |ret, (data_renderer_name, data_renderer_class)|
      data_renderer = data_renderer_class.new(:locale => locale)
      data = data_renderer.get_data
      data.each_pair do |name, value|
        ret[name] = value
      end

      ret
    end.to_json

    file = post_process_file(CoffeeScript.compile(bundle.render), options)

    yield file, TwitterCldr.twitter_locale(locale)
  end
end

#compile_implementation(options = {}) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/twitter_cldr/js/compiler.rb', line 89

def compile_implementation(options = {})
  bundle = TwitterCldr::Js::Renderers::Bundle.new
  bundle[:locale] = TwitterCldr::DEFAULT_LOCALE
  file = compile_bundle(bundle, @features, implementation_renderers, options)

  file.source
end

#compile_test(options = {}) ⇒ Object



97
98
99
100
101
# File 'lib/twitter_cldr/js/compiler.rb', line 97

def compile_test(options = {})
  bundle = TwitterCldr::Js::Renderers::TestBundle.new
  file = compile_bundle(bundle, @test_helpers, test_helper_renderers, options)
  file.source
end

#post_process_file(file_source, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/twitter_cldr/js/compiler.rb', line 56

def post_process_file(file_source, options)
  options[:minify] = true unless options.include?(:minify)
  # required alias definition that adds twitter_cldr to Twitter's static build process
  file_source.gsub!(/\/\*<<module_def>>\s+\*\//, %Q(/*-module-*/\n/*_lib/twitter_cldr_*/))

  if options[:minify]
    file_source = Uglifier.compile(file_source)
  end

  file_source
end