Class: Ritual::VersionFile

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

Constant Summary collapse

VERSION_REGEXP =
/^\s*VERSION\s*=\s*(\[.*?\])/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, library_name) ⇒ VersionFile



5
6
7
8
9
# File 'lib/ritual/version_file.rb', line 5

def initialize(path, library_name)
  @path = path
  @library_name = library_name
  @value ||= read
end

Instance Attribute Details

#library_nameObject (readonly)

Returns the value of attribute library_name.



11
12
13
# File 'lib/ritual/version_file.rb', line 11

def library_name
  @library_name
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/ritual/version_file.rb', line 11

def path
  @path
end

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/ritual/version_file.rb', line 11

def value
  @value
end

Instance Method Details

#increment(component) ⇒ Object



43
44
45
46
47
# File 'lib/ritual/version_file.rb', line 43

def increment(component)
  (value.size..component).each{|i| value[i] = 0}
  value[component] += 1
  (component+1 ... value.size).each{|i| value[i] = 0}
end

#to_sObject



13
14
15
# File 'lib/ritual/version_file.rb', line 13

def to_s
  value.join('.')
end

#writeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ritual/version_file.rb', line 17

def write
  if File.exist?(path)
    source = File.read(path).sub(VERSION_REGEXP) do |s|
      "#{s[/\s*/]}VERSION = [#{@value.join(', ')}]"
    end
    open(path, 'w') { |f| f.puts source}
  else
    FileUtils.mkdir_p File.dirname(path)
    open(path, 'w') do |file|
      file.puts "        |module \#{module_name}\n        |  VERSION = \#{value.inspect}\n        |\n        |  class << VERSION\n        |    include Comparable\n        |\n        |    def to_s\n        |      join('.')\n        |    end\n        |  end\n        |end\n      EOS\n    end\n  end\nend\n".gsub(/^ *\|/, '')