Class: Palco::FileBase

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Gemspec, License

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#emailObject (readonly)

Returns the value of attribute email.



12
13
14
# File 'lib/palco/filebase.rb', line 12

def email
  @email
end

#file_contentObject

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_nameObject (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_dirObject (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_nameObject (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

#createObject

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

Returns:

  • (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

#readObject



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