Class: Bwoken::Coffeescript

Inherits:
Object
  • Object
show all
Defined in:
lib/bwoken/coffeescript.rb,
lib/bwoken/coffeescript/import_string.rb,
lib/bwoken/coffeescript/github_import_string.rb

Defined Under Namespace

Classes: GithubImportString, ImportString

Class Method Summary collapse

Class Method Details

.coffee_script_sourceObject



13
14
15
16
17
18
19
20
21
# File 'lib/bwoken/coffeescript.rb', line 13

def coffee_script_source
  return @coffeescript if @coffeescript

  @coffeescript = ''
  open(CoffeeScript::Source.bundled_path) do |f|
    @coffeescript << f.read
  end
  @coffeescript
end

.coffeescript_to_javascript(coffee) ⇒ Object



40
41
42
# File 'lib/bwoken/coffeescript.rb', line 40

def coffeescript_to_javascript coffee
  self.context.call 'CoffeeScript.compile', coffee, :bare => true
end

.compile(source, target) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/bwoken/coffeescript.rb', line 31

def compile source, target
  githubs_and_imports, sans_imports = precompile(IO.read source)

  javascript = coffeescript_to_javascript sans_imports.join
  import_strings = githubs_to_imports(githubs_and_imports)

  write import_strings, javascript, :to => target
end

.contextObject



23
24
25
# File 'lib/bwoken/coffeescript.rb', line 23

def context
  @context ||= ExecJS.compile(coffee_script_source)
end

.githubs_to_imports(strings) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/bwoken/coffeescript.rb', line 44

def githubs_to_imports strings
  strings.map do |string|
    obj = import_string_object(string)
    obj.parse
    obj.to_s
  end.join("\n")
end

.import_string_object(string) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/bwoken/coffeescript.rb', line 52

def import_string_object string
  if string =~ /^#github/
    GithubImportString.new(string)
  else
    ImportString.new(string)
  end
end

.precompile(coffeescript) ⇒ Object



27
28
29
# File 'lib/bwoken/coffeescript.rb', line 27

def precompile coffeescript
  coffeescript.lines.partition {|line| line =~ /^#(?:github|import) .*$/}
end

.write(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/bwoken/coffeescript.rb', line 60

def write *args
  to_hash = args.last
  chunks = args[0..-2]

  File.open(to_hash[:to], 'w') do |io|
    chunks.each do |chunk|
      io.puts chunk unless chunk.nil? || chunk == ''
    end
  end
end