Class: Inkan

Inherits:
Object
  • Object
show all
Defined in:
lib/inkan.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Inkan

Returns a new instance of Inkan.



27
28
29
30
31
32
33
34
# File 'lib/inkan.rb', line 27

def initialize(file)
  @file = file
  
  # Set Defaults
  @credit         = 'Generated by Inkan'
  @comment_prefix = '#'
  @comment_suffix = ''
end

Instance Attribute Details

#comment_prefixObject

Returns the value of attribute comment_prefix.



4
5
6
# File 'lib/inkan.rb', line 4

def comment_prefix
  @comment_prefix
end

#comment_suffixObject

Returns the value of attribute comment_suffix.



4
5
6
# File 'lib/inkan.rb', line 4

def comment_suffix
  @comment_suffix
end

#creditObject

Returns the value of attribute credit.



4
5
6
# File 'lib/inkan.rb', line 4

def credit
  @credit
end

Class Method Details

.legitimate?(file) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
# File 'lib/inkan.rb', line 6

def self.legitimate?(file)
  legit = false
  
  File.open(file) do |file|
    first_line = file.gets
    legit = !first_line[/\s#{sha(file.read)}\s*\n$/].nil?
  end
  
  legit
end

.seal(file) {|inkan| ... } ⇒ Object

Yields:

  • (inkan)


17
18
19
20
21
# File 'lib/inkan.rb', line 17

def self.seal(file)
  inkan = new(file)
  yield inkan
  inkan.seal
end

.sha(content) ⇒ Object



23
24
25
# File 'lib/inkan.rb', line 23

def self.sha(content)
  Digest::SHA1.hexdigest(content)
end

Instance Method Details



36
37
38
# File 'lib/inkan.rb', line 36

def print(string)
  file_content << string
end

#puts(string) ⇒ Object



40
41
42
# File 'lib/inkan.rb', line 40

def puts(string)
  file_content << string << "\n"
end

#sealObject



44
45
46
47
48
49
# File 'lib/inkan.rb', line 44

def seal
  File.open(@file, 'w') do |f|
    f.puts "#{comment_prefix} #{credit}. #{sha} #{comment_suffix}"
    f.print file_content
  end
end