Class: Palco::FileBase
- Inherits:
-
Object
- Object
- Palco::FileBase
- Defined in:
- lib/palco/filebase.rb
Overview
Public: this is a generic file class used to provide some common methods such as writing contents in a text file or reading or comparing a content.
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#file_content ⇒ Object
Returns the value of attribute file_content.
-
#full_path_name ⇒ Object
readonly
Returns the value of attribute full_path_name.
-
#root_dir ⇒ Object
readonly
Returns the value of attribute root_dir.
-
#user_name ⇒ Object
readonly
Returns the value of attribute user_name.
Instance Method Summary collapse
-
#create ⇒ Object
Public: creates a new file.
- #exists? ⇒ Boolean
- #find(string) ⇒ Object
-
#initialize(root_dir, name) ⇒ FileBase
constructor
A new instance of FileBase.
- #read ⇒ Object
- #write(content) ⇒ Object
Constructor Details
#initialize(root_dir, name) ⇒ FileBase
Returns a new instance of FileBase.
17 18 19 20 21 22 23 24 |
# File 'lib/palco/filebase.rb', line 17 def initialize(root_dir, name) @full_path_name = File.join(Dir.pwd, root_dir, name) conf = Git.global_config @user_name = conf["user.name"] @email = conf["user.email"] @file_content = "" end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
12 13 14 |
# File 'lib/palco/filebase.rb', line 12 def email @email end |
#file_content ⇒ Object
Returns the value of attribute file_content.
15 16 17 |
# File 'lib/palco/filebase.rb', line 15 def file_content @file_content end |
#full_path_name ⇒ Object (readonly)
Returns the value of attribute full_path_name.
13 14 15 |
# File 'lib/palco/filebase.rb', line 13 def full_path_name @full_path_name end |
#root_dir ⇒ Object (readonly)
Returns the value of attribute root_dir.
10 11 12 |
# File 'lib/palco/filebase.rb', line 10 def root_dir @root_dir end |
#user_name ⇒ Object (readonly)
Returns the value of attribute user_name.
11 12 13 |
# File 'lib/palco/filebase.rb', line 11 def user_name @user_name end |
Instance Method Details
#create ⇒ Object
Public: creates a new file.
Please note that it’s up to every subclass to fill the file_content
Example
file = Palco::File.new('test', 'a_file')
a.create
Returns
Returns true if the file can be created with the proper content or
false otherwise
64 65 66 |
# File 'lib/palco/filebase.rb', line 64 def create self.write(@file_content) end |
#exists? ⇒ Boolean
26 27 28 |
# File 'lib/palco/filebase.rb', line 26 def exists? File.exists?(@full_path_name) end |
#find(string) ⇒ Object
48 49 50 51 |
# File 'lib/palco/filebase.rb', line 48 def find(string) text = self.read text.include?(string) end |
#read ⇒ Object
30 31 32 33 34 |
# File 'lib/palco/filebase.rb', line 30 def read f= File.open(@full_path_name) content = f.read content end |
#write(content) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/palco/filebase.rb', line 36 def write(content) begin f=File.open(@full_path_name, "w") f.write(content) f.close true rescue Exception false end end |