Class: Entityjs::Compile

Inherits:
Object
  • Object
show all
Defined in:
lib/entityjs/compile.rb

Class Method Summary collapse

Class Method Details

.coffee_to_js(data) ⇒ Object



133
134
135
# File 'lib/entityjs/compile.rb', line 133

def self.coffee_to_js(data)
  return ParseCoffee.parse(data)
end

.data_to_entity(comps, data) ⇒ Object

compiles data files into entities



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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_regexObject



15
16
17
# File 'lib/entityjs/compile.rb', line 15

def self.datas_regex
  return /^.*\.#{self.valid_datas.join('|')}$/i
end

.pathObject



23
24
25
26
27
28
29
# File 'lib/entityjs/compile.rb', line 23

def self.path
  if @path.nil?
    @path = Dirc.game_root
  end
  
  @path
end

.path=(v) ⇒ Object



31
32
33
# File 'lib/entityjs/compile.rb', line 31

def self.path=(v)
  @path = v
end

.read_contents(path) ⇒ Object

used for stubbing



146
147
148
# File 'lib/entityjs/compile.rb', line 146

def self.read_contents(path)
  IO.read(self.path+'/'+path)
end

.script_to_js(path, data = nil) ⇒ Object



44
45
46
47
48
49
50
51
# 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) ⇒ Object



35
36
37
38
39
40
41
42
# 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) ⇒ Object



137
138
139
# File 'lib/entityjs/compile.rb', line 137

def self.tmx_to_json(data)
  return ParseTMX.parse(data)
end

.to_entity(comps, json) ⇒ Object

converts given comps and json into an entityjs entity returns an entityjs component



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object

converts the given data to json the type defines the data type example: to_json(‘xml’, “<map><bob>10</bob></map>”)

> “”bob“:{$:10}”



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/entityjs/compile.rb', line 19

def self.valid_data?(file)
  return file.match(datas_regex) != nil
end

.valid_datasObject



7
8
9
# File 'lib/entityjs/compile.rb', line 7

def self.valid_datas
  ['xml', 'json', 'tmx', 'csv', 'yml']
end

.valid_scriptsObject



11
12
13
# File 'lib/entityjs/compile.rb', line 11

def self.valid_scripts
  ['js', 'coffee'] + self.valid_datas
end

.xml_to_json(data) ⇒ Object



141
142
143
# File 'lib/entityjs/compile.rb', line 141

def self.xml_to_json(data)
  return ParseXML.parse(data)     
end