Class: QEDProject::Project

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/qedproject/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#render_template_to_file

Constructor Details

#initialize(project_path, options = {}) ⇒ Project

Creates a new Project instance. Options:

:libs : A list of js libraries to include, Can be :backbone, :jquery, :knockout
:jammit: boolean, should Jammit be included?
:coffeescript : Should CoffeeScript support be included?
:sass           Should sass support be included?


31
32
33
34
35
# File 'lib/qedproject/project.rb', line 31

def initialize(project_path, options = {})
  self.path = project_path
  self.process_options(options)
  self.set_paths
end

Instance Attribute Details

#coffeescriptObject

Returns the value of attribute coffeescript.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def coffeescript
  @coffeescript
end

#css_pathObject

Returns the value of attribute css_path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def css_path
  @css_path
end

#images_pathObject

Returns the value of attribute images_path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def images_path
  @images_path
end

#jammitObject

Returns the value of attribute jammit.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def jammit
  @jammit
end

#js_pathObject

Returns the value of attribute js_path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def js_path
  @js_path
end

#libsObject

Returns the value of attribute libs.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def libs
  @libs
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def path
  @path
end

#sassObject

Returns the value of attribute sass.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def sass
  @sass
end

#testingObject

Returns the value of attribute testing.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def testing
  @testing
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/qedproject/project.rb', line 9

def verbose
  @verbose
end

Class Method Details

.create(project_path, options = {}) ⇒ Object

convenience method to create a new project. Simply call create with the project path and the options.



13
14
15
# File 'lib/qedproject/project.rb', line 13

def self.create(project_path, options= {})
   self.new(project_path, options).generate
end

Instance Method Details

#add_testingObject

includes the Jasmine BDD framework for javascript testing



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/qedproject/project.rb', line 113

def add_testing
  FileUtils.mkdir_p File.join(self.path, "spec"), :verbose => self.verbose
  FileUtils.cp_r File.join(self.vendor_root, "jasmine", "lib"), 
                 File.join(self.path, "spec", "lib"), :verbose => self.verbose


  render_template_to_file "suite.html", File.join(self.path, "spec", "suite.html"), binding
  if self.coffeescript
    render_template_to_file "sampleSpec.coffee", File.join(self.path, "spec", "sampleSpec.coffee"), binding
  else
    render_template_to_file "sampleSpec.js", File.join(self.path, "spec", "sampleSpec.js"), binding
  end

end

#create_asset_fileObject



164
165
166
167
# File 'lib/qedproject/project.rb', line 164

def create_asset_file
  render_template_to_file("assets.yml", File.join(self.path, "config", "assets.yml"), binding)
  puts "Created #{File.join(self.path, 'config', 'assets.yml')}" if self.verbose
end

#create_guardfileObject



169
170
171
172
# File 'lib/qedproject/project.rb', line 169

def create_guardfile
  render_template_to_file "Guardfile", File.join(self.path, "Guardfile"), binding
  puts "Created #{ File.join(self.path, "Guardfile")}" if self.verbose
end

#create_indexObject



174
175
176
177
178
# File 'lib/qedproject/project.rb', line 174

def create_index
  @project = self
  render_template_to_file "index.html", File.join(self.path, "public", "index.html"), binding
  puts "Created #{ File.join(self.path, "public", "index.html")}" if self.verbose
end

#create_project_skeletonObject

Set up the main project skeleton project

public/
  assets/           * only with assets
  images/
  javascripts/      * only without assets
  stylesheets/      * only without assets
config/
  assets.yml        * optional
coffeescripts/      * optional
sass/               * optional
spec/               * optional
Guardfile           * optional


141
142
143
144
145
146
147
148
149
150
151
# File 'lib/qedproject/project.rb', line 141

def create_project_skeleton
  ::FileUtils.mkdir_p( self.path, :verbose => self.verbose)
  ::FileUtils.mkdir_p( File.join(self.path, "public"), :verbose => self.verbose)
  ::FileUtils.mkdir_p( File.join(self.path, self.images_path), :verbose => self.verbose)
  ::FileUtils.mkdir_p( File.join(self.path, "tmp"), :verbose => self.verbose)
  ::FileUtils.mkdir_p( File.join(self.path, self.js_path), :verbose => self.verbose)
  ::FileUtils.mkdir_p( File.join(self.path, self.css_path), :verbose => self.verbose)
  ::FileUtils.mkdir_p( File.join(self.path, "config"), :verbose => self.verbose) if self.needs_config_folder?
  ::FileUtils.mkdir_p( File.join(self.path, "coffeescripts"), :verbose => self.verbose) if self.coffeescript
  ::FileUtils.mkdir_p( File.join(self.path, "sass"), :verbose => self.verbose) if self.sass
end

#create_rakefileObject



180
181
182
183
# File 'lib/qedproject/project.rb', line 180

def create_rakefile
  render_template_to_file "Rakefile", File.join(self.path, "Rakefile"), binding
  puts "Created #{ File.join(self.path, "Rakefile")}" if self.verbose
end

#css_assetsObject

Convenience method for showing all of the css files that the project should contain. Good for creating asset lists and loading files in rendered templates



54
55
56
57
58
59
60
61
62
# File 'lib/qedproject/project.rb', line 54

def css_assets
  libs.inject([]) do |original, lib|
    if QEDProject::Libraries::Base.libs[lib].respond_to?(:css_files)
      original += QEDProject::Libraries::Base.libs[lib].css_files 
    else
      original
    end
  end
end

#generateObject

Start the project generation. Create the folder Get all of the libraries create an assets file if needed add the testing folder if needed create a guardfile if needed



102
103
104
105
106
107
108
109
110
# File 'lib/qedproject/project.rb', line 102

def generate
  self.create_project_skeleton
  self.create_index                             
  self.get_libraries if self.libs
  self.create_asset_file if self.jammit
  self.add_testing if self.testing
  self.create_guardfile if self.needs_guardfile?
  self.create_rakefile
end

#get_librariesObject

Loop through the libraries the user added and run their generate methods which pulls in the additional optional files.



156
157
158
159
160
161
162
# File 'lib/qedproject/project.rb', line 156

def get_libraries
  libs.each do |lib|
    library = QEDProject::Libraries::Base.libs[lib]
    l = library.new(self)
    l.generate!
  end
end

#js_assetsObject

Convenience method for showing all of the JavaScript files that the project should contain. Good for creating asset lists and loading files in rendered templates



41
42
43
44
45
46
47
48
49
# File 'lib/qedproject/project.rb', line 41

def js_assets
  libs.inject([]) do |original, lib|
    if QEDProject::Libraries::Base.libs[lib].respond_to?(:js_files)
       original += QEDProject::Libraries::Base.libs[lib].js_files 
    else
      original
    end
  end 
end

#needs_config_folder?Boolean

Only jammit needs a config folder for now.

Returns:

  • (Boolean)


92
93
94
# File 'lib/qedproject/project.rb', line 92

def needs_config_folder?
  self.jammit
end

#needs_guardfile?Boolean

We need a guardfile if the user is using jammit, sass, or coffeescript.

Returns:

  • (Boolean)


87
88
89
# File 'lib/qedproject/project.rb', line 87

def needs_guardfile?
  self.jammit || self.sass || self.coffeescript
end

#process_options(options) ⇒ Object

Sets instance methods with values from the options hash.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/qedproject/project.rb', line 65

def process_options(options)
  self.libs = options[:libs] || []
  
  libs.each do |lib|
    raise QEDProject::BadLibraryError, "#{lib} is not a valid library" unless QEDProject::Libraries::Base.libs.include? lib
  end
  
  self.jammit = options[:jammit]
  self.sass = options[:sass]
  self.coffeescript = options[:coffeescript]
  self.verbose = options[:verbose]
  self.testing = options[:testing]
end

#set_pathsObject

Set up the basic paths we’ll use throughout



80
81
82
83
84
# File 'lib/qedproject/project.rb', line 80

def set_paths
   self.images_path = File.join("public", "images")
   self.js_path = self.jammit ? "javascripts" : File.join("public", "javascripts")
   self.css_path = self.jammit ? "stylesheets" : File.join("public", "stylesheets")
end

#template_rootObject



17
18
19
# File 'lib/qedproject/project.rb', line 17

def template_root
  File.expand_path("../../../templates", __FILE__)
end

#vendor_rootObject



21
22
23
# File 'lib/qedproject/project.rb', line 21

def vendor_root
  File.expand_path("../../../vendor", __FILE__)
end