Module: Ti::Utils

Instance Method Summary collapse

Instance Method Details

#append_to_router(name, type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/ti/utils.rb', line 57

def append_to_router(name, type)
  router_contents = File.read(location.join("app/app.coffee"))

  if router_contents.include?(type.capitalize)
    contents = router_contents.sub( "#{type.capitalize}:", "#{type.capitalize}:\n    #{name.capitalize}: {}" )
  else
    contents = router_contents.sub("#{get_app_name} =", "#{get_app_name} =\n\t#{type.capitalize}:\n    #{name.capitalize}: {}\n")
  end
  File.open(location.join("app/app.coffee"), 'w') { |f| f.write(contents) }
end

#base_locationObject Also known as: location



82
83
84
# File 'lib/ti/utils.rb', line 82

def base_location
  @location ||= Pathname.new(Dir.pwd)
end

#create_directories(*dirs) ⇒ Object



35
36
37
38
39
40
# File 'lib/ti/utils.rb', line 35

def create_directories(*dirs)
  dirs.each do |dir|
    log "Creating the #{dir} directory."
    FileUtils.mkdir_p(location.join(dir))
  end
end

#create_new_file(name, file = nil) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/ti/utils.rb', line 4

def create_new_file(name, file=nil)
  log "Creating #{name}"
  contents = file.nil? ? '' : File.read(file)
  unless File.file?(location.join(name))
    File.open(location.join(name), 'w') { |f| f.write(contents) }
  end
end

#create_with_template(name, template_location, contents = {}) ⇒ Object



51
52
53
54
55
# File 'lib/ti/utils.rb', line 51

def create_with_template(name, template_location, contents={})
  template    = templates("#{template_location}.erb")
  eruby       = Erubis::Eruby.new(File.read(template))
  File.open(location.join(name.gsub(/^\//, '')), 'w') { |f| f.write(eruby.result(contents))}
end

#error(msg) ⇒ Object



78
79
80
# File 'lib/ti/utils.rb', line 78

def error(msg)
  ::Ti::Logger.error(msg)
end

#get_app_nameObject



12
13
14
15
16
17
# File 'lib/ti/utils.rb', line 12

def get_app_name
  config  = File.open("tiapp.xml")
  doc     = ::Nokogiri::XML(config)
  config.close
  doc.xpath('ti:app/name').text
end

#log(msg) ⇒ Object



74
75
76
# File 'lib/ti/utils.rb', line 74

def log(msg)
  ::Ti::Logger.report(msg)
end

#remove_directories(*names) ⇒ Object



43
44
45
46
47
48
# File 'lib/ti/utils.rb', line 43

def remove_directories(*names)
  names.each do |name|
    log "Removing #{name} directory."
    FileUtils.rm_rf(location.join(name))
  end
end

#remove_files(*files) ⇒ Object



19
20
21
22
23
24
# File 'lib/ti/utils.rb', line 19

def remove_files(*files)
  files.each do |file|
    log "Removing #{file} file."
    FileUtils.rm(location.join(file))
  end
end

#templates(path) ⇒ Object



69
70
71
# File 'lib/ti/utils.rb', line 69

def templates(path)
  ::Ti.root.join('ti/templates').join(path)
end

#touch(*filenames) ⇒ Object



27
28
29
30
31
32
# File 'lib/ti/utils.rb', line 27

def touch(*filenames)
  filenames.each do |filename|
    log "Creating #{filename} file."
    FileUtils.touch(location.join(filename))
  end
end

#underscore(string) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/ti/utils.rb', line 88

def underscore(string)
  string.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end