Class: Inkan

Inherits:
Object show all
Defined in:
lib/inkan/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.


31
32
33
34
35
36
37
38
# File 'lib/inkan/inkan.rb', line 31

def initialize(file)
  @file = file

  # Set Defaults
  @credit         = 'Generated by Inkan'
  @comment        = '#'
  @comment_suffix = ''
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment


2
3
4
# File 'lib/inkan/inkan.rb', line 2

def comment
  @comment
end

#comment_suffixObject

Returns the value of attribute comment_suffix


2
3
4
# File 'lib/inkan/inkan.rb', line 2

def comment_suffix
  @comment_suffix
end

#creditObject

Returns the value of attribute credit


2
3
4
# File 'lib/inkan/inkan.rb', line 2

def credit
  @credit
end

Class Method Details

.legitimate?(filename) ⇒ Boolean

Returns:

  • (Boolean)

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

def self.legitimate?(filename)
  File.open(filename) do |file|
    file_content = file.read
    seal, content = if file_content[/\A#!/]
      hashbang, seal, remaining_content = file_content.split("\n", 3)
      [seal, "#{hashbang}\n#{remaining_content}"]
    else
      file_content.split("\n", 2)
    end

    !seal[/\s#{sha(content || '')}\s*$/].nil?
  end
end

.renderObject


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

def self.render
  new(nil).tap {|inkan| yield inkan }.render
end

.seal(file) ⇒ Object


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

def self.seal(file)
  new(file).tap {|inkan| yield inkan }.seal
end

.sha(content) ⇒ Object


26
27
28
29
# File 'lib/inkan/inkan.rb', line 26

def self.sha(content)
  require 'digest/sha1'
  Digest::SHA1.hexdigest(content)
end

Instance Method Details


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

def print(string)
  file_content << string
end

#puts(string) ⇒ Object


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

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

#renderObject


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

def render
  if file_content[/\A#!/]
    hashbang, remaining_content = file_content.split("\n", 2)
    "#{hashbang}\n#{render_seal}\n#{remaining_content}"
  else
    "#{render_seal}\n#{file_content}"
  end
end

#sealObject


48
49
50
51
52
# File 'lib/inkan/inkan.rb', line 48

def seal
  File.open(@file, 'w') do |f|
    f.print render
  end
end