Class: Palco::Base
- Inherits:
-
Object
- Object
- Palco::Base
- Defined in:
- lib/palco/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#generated ⇒ Object
readonly
Returns the value of attribute generated.
-
#items_list ⇒ Object
readonly
Returns the value of attribute items_list.
-
#project_name ⇒ Object
readonly
Public: creates a new base palco instance.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
Instance Method Summary collapse
- #can_generate? ⇒ Boolean
-
#destroy ⇒ Object
Public: destroy the skelethon removing all files and all directories.
-
#generate ⇒ Object
Public: generates the skelethon.
- #generated? ⇒ Boolean
-
#initialize(name = nil, items_list = []) ⇒ Base
constructor
A new instance of Base.
- #valid? ⇒ Boolean
Constructor Details
#initialize(name = nil, items_list = []) ⇒ Base
Returns a new instance of Base.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/palco/base.rb', line 37 def initialize(name=nil, items_list=[]) @project_name = name @items_list = items_list @generated = false if @project_name.nil? or @items_list.size==0 @valid = false else @valid = true end end |
Instance Attribute Details
#generated ⇒ Object (readonly)
Returns the value of attribute generated.
35 36 37 |
# File 'lib/palco/base.rb', line 35 def generated @generated end |
#items_list ⇒ Object (readonly)
Returns the value of attribute items_list.
33 34 35 |
# File 'lib/palco/base.rb', line 33 def items_list @items_list end |
#project_name ⇒ Object (readonly)
Public: creates a new base palco instance.
The idea is that specialized classes extending Palco::Base will be aware about the list of files and directories to be created, meanwhile the Palco::Base takes this list without any clue about their meaning.
name - the name of the Sinatra project items_list - an array containing a list of hashes with name of the file to
be created and a second param telling the code code it the item is either
a file or a directory.
Example
base = Palco::Base.new('test_one',[{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
Returns
A new Palco::Base object
32 33 34 |
# File 'lib/palco/base.rb', line 32 def project_name @project_name end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
34 35 36 |
# File 'lib/palco/base.rb', line 34 def valid @valid end |
Instance Method Details
#can_generate? ⇒ Boolean
103 104 105 |
# File 'lib/palco/base.rb', line 103 def can_generate? ! File.directory?(File.join(Dir.pwd, @project_name)) end |
#destroy ⇒ Object
Public: destroy the skelethon removing all files and all directories.
Also the base directory has been removed as well.
Please note that you’re not supposed to call destroy for a non generated skelethon. The method returns false in this case.
Example
base = Palco::Base.new('test_one',[{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
base.generate
base.destroy
Returns
True if all items in the item list has been removed or false otherwise.
93 94 95 96 97 98 99 100 101 |
# File 'lib/palco/base.rb', line 93 def destroy if ! self.generated? and self.can_generate? return false end FileUtils.rm_rf(@project_name) @valid = false @generated= false true end |
#generate ⇒ Object
Public: generates the skelethon
Example
base = Palco::Base.new('test_one',[{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
base.generate
Returns
True if every item in the item list has been created of false otherwise.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/palco/base.rb', line 58 def generate if ! self.can_generate? return false end FileUtils.mkdir(File.join(Dir.pwd, @project_name)) @items_list.each do |item| fullname = File.join(Dir.pwd, @project_name, item[:name]) isfile = item[:file] if isfile FileUtils.touch(fullname) else FileUtils.mkdir(fullname) end end @generated = true return true end |
#generated? ⇒ Boolean
111 112 113 |
# File 'lib/palco/base.rb', line 111 def generated? return self.generated end |
#valid? ⇒ Boolean
107 108 109 |
# File 'lib/palco/base.rb', line 107 def valid? return self.valid end |