Class: Prigner::Version

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

Overview

The objective of this class is to implement various ideas proposed by the Semantic Versioning Specification (see reference).

Constant Summary collapse

FILE =
Pathname.new(__FILE__).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Version

Basic initialization of the attributes using a single hash.



57
58
59
60
61
62
# File 'lib/prigner.rb', line 57

def initialize(attributes = {})
  attributes.each do |attribute, value|
    send("#{attribute}=", value) if respond_to? "#{attribute}="
  end
  @timestamp = attributes[:timestamp]
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



52
53
54
# File 'lib/prigner.rb', line 52

def date
  @date
end

#tagObject

Returns the value of attribute tag.



52
53
54
# File 'lib/prigner.rb', line 52

def tag
  @tag
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



54
55
56
# File 'lib/prigner.rb', line 54

def timestamp
  @timestamp
end

Class Method Details

.currentObject



92
93
94
95
96
97
# File 'lib/prigner.rb', line 92

def current
  yaml = FILE.readlines[0..3].
           join("").
           gsub(/\#@ /,'')
  new(YAML.load(yaml))
end

.to_sObject



99
100
101
102
# File 'lib/prigner.rb', line 99

def to_s
  name.match(/(.*?)::.*/)
  "#{$1} v#{current.tag} (#{current.date})"
end

Instance Method Details

#numberingObject

The numbering of the major, minor and patch values.



65
66
67
68
69
70
71
72
73
# File 'lib/prigner.rb', line 65

def numbering
  self.tag.split(".").map do |key|
    if key.match(/^(\d{1,})(\w+).*$/)
      [ $1.to_i, $2 ]
    else
      key.to_i
    end
  end.flatten
end

#save!Object



82
83
84
85
86
87
88
89
# File 'lib/prigner.rb', line 82

def save!
  source = FILE.readlines
  source[0..3] = self.to_hash.to_yaml.to_s.gsub(/^/, '#@ ')
  FILE.open("w+") do |file|
    file << source.join("")
  end
  self
end

#to_hashObject



75
76
77
78
79
80
# File 'lib/prigner.rb', line 75

def to_hash
  [:tag, :date, :timestamp].inject({}) do |hash, key|
    hash[key] = send(key)
    hash
  end
end