class Entityjs::Compile

Public Class Methods

coffee_to_js(data) click to toggle source
# File lib/entityjs/compile.rb, line 133
def self.coffee_to_js(data)
  return ParseCoffee.parse(data)
end
data_to_entity(comps, data) click to toggle source

compiles data files into entities

# File lib/entityjs/compile.rb, line 69
def self.data_to_entity(comps, data)
  if comps.is_a? String
    comps = [comps]
  end
  
  #find extension for compiling, or else treat it as json
  ext = comps.select{|i| !File.extname(i).empty? }.first
  
  if ext.nil?
    ext = '.json'
  else
    ext = File.extname(ext)
  end
  
  json = self.to_json(ext, data)
  
  return self.to_entity(comps, json)
end
data_to_js(path, data) click to toggle source
# File lib/entityjs/compile.rb, line 53
def self.data_to_js(path, data)
  js = ''
  
  if self.valid_data?(path)
    comps = path.split('/')
    js = self.data_to_entity(comps, data)
  elsif path.match /\.coffee$/
    js = self.coffee_to_js(data)
  else
    js = data
  end
  
  return js
end
datas_regex() click to toggle source
# File lib/entityjs/compile.rb, line 15
def self.datas_regex
  return /^.*\.#{self.valid_datas.join('|')}$/i
end
path() click to toggle source
# File lib/entityjs/compile.rb, line 23
def self.path
  if @path.nil?
    @path = Dirc.game_root
  end
  
  @path
end
path=(v) click to toggle source
# File lib/entityjs/compile.rb, line 31
def self.path=(v)
  @path = v
end
read_contents(path) click to toggle source

used for stubbing

# File lib/entityjs/compile.rb, line 146
def self.read_contents(path)
  IO.read(self.path+'/'+path)
end
script_to_js(path, data=nil) click to toggle source
# File lib/entityjs/compile.rb, line 44
def self.script_to_js(path, data=nil)
  if data.nil?
    #load
    data = self.read_contents(Config.scripts_folder+'/'+path)
  end
  
  return self.data_to_js(path, data)
end
test_to_js(path, data=nil) click to toggle source
# File lib/entityjs/compile.rb, line 35
def self.test_to_js(path, data=nil)
  if data.nil?
    #load
    data = self.read_contents(Config.tests_folder+'/'+path)
  end
  
  return self.data_to_js(path, data)
end
tmx_to_json(data) click to toggle source
# File lib/entityjs/compile.rb, line 137
def self.tmx_to_json(data)
  return ParseTMX.parse(data)
end
to_entity(comps, json) click to toggle source

converts given comps and json into an entityjs entity

returns an entityjs component
# File lib/entityjs/compile.rb, line 90
def self.to_entity(comps, json)
  
  #remove plurals
  comps.collect! do |i|
    
    #115 for ruby 1.8.7
    if i[-1] == 's' || i[-1] == 115
      i[0..-2]
    else
      i
    end
    
  end
  
  #turn into one long strng
  comps = comps.join(' ')
  
  #output entity
  return "re.e('#{comps}')\n.attr(#{json})"
end
to_json(type, data) click to toggle source

converts the given data to json the type defines the data type example:

to_json('xml', "<map><bob>10</bob></map>")
=> "{"bob":{$:10}}"
# File lib/entityjs/compile.rb, line 116
def self.to_json(type, data)
  
  type.downcase!
  
  case type
    when '.json'
      return data
    when '.tmx'
      return self.tmx_to_json(data)
    when '.xml'
      return self.xml_to_json(data)
    else
      return data
  end
  
end
valid_data?(file) click to toggle source
# File lib/entityjs/compile.rb, line 19
def self.valid_data?(file)
  return file.match(datas_regex) != nil
end
valid_datas() click to toggle source
# File lib/entityjs/compile.rb, line 7
def self.valid_datas
  ['xml', 'json', 'tmx', 'csv', 'yml']
end
valid_scripts() click to toggle source
# File lib/entityjs/compile.rb, line 11
def self.valid_scripts
  ['js', 'coffee'] + self.valid_datas
end
xml_to_json(data) click to toggle source
# File lib/entityjs/compile.rb, line 141
def self.xml_to_json(data)
  return ParseXML.parse(data)     
end