Class: App::Version
- Inherits:
-
Object
- Object
- App::Version
- Includes:
- Comparable
- Defined in:
- lib/app_version/app_version.rb
Overview
Application Version main class
Instance Attribute Summary collapse
-
#branch ⇒ String
Git Branch information.
-
#build ⇒ String
Build number.
-
#build_date ⇒ String
Build Date.
-
#commiter ⇒ String
Git Commiter information.
-
#committer ⇒ Object
Returns the value of attribute committer.
-
#format ⇒ String
Format information.
-
#major ⇒ String
Major Version number.
-
#meta ⇒ String
Semantic version meta info.
-
#milestone ⇒ String
Milestone information.
-
#minor ⇒ String
Minor Version number.
-
#patch ⇒ String
Patch number.
Class Method Summary collapse
-
.load(path) ⇒ Object
Loads the version information from a YAML file.
-
.parse(version) ⇒ App:Version
Parses a version string to create an instance of the Version class.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Combined Compare operator for version.
-
#initialize(args = nil) ⇒ Version
constructor
Creates a new instance of the Version class using information in the passed Hash to construct the version number.
- #sem_ver_format ⇒ Object
-
#to_s ⇒ String
Generate version string.
Constructor Details
#initialize(args = nil) ⇒ Version
Creates a new instance of the Version class using information in the passed Hash to construct the version number.
Version.new(:major => 1, :minor => 0) #=> "1.0"
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/app_version/app_version.rb', line 40 def initialize(args = nil) if args && args.is_a?(Hash) args.keys.reject { |key| key.is_a?(Symbol) }.each { |key| args[key.to_sym] = args.delete(key) } [:major, :minor].each do |param| fail ArgumentError.new("The #{param} parameter is required") if args[param].blank? end @major = args[:major].to_s @minor = args[:minor].to_s @patch = args[:patch].to_s unless args[:patch].blank? @meta = args[:meta].to_s unless args[:meta].blank? @milestone = args[:milestone].to_s unless args[:milestone].blank? @build = args[:build].to_s unless args[:build].blank? @branch = args[:branch].to_s unless args[:branch].blank? @committer = args[:committer].to_s unless args[:committer].blank? @format = args[:format].to_s unless args[:format].blank? unless args[:build_date].blank? b_date = case args[:build_date] when 'git-revdate' get_revdate_from_git else args[:build_date].to_s end @build_date = Date.parse(b_date) end @build = case args[:build] when 'svn' get_build_from_subversion when 'git-revcount' get_revcount_from_git when 'git-hash' get_hash_from_git when nil, '' nil else args[:build].to_s end end end |
Instance Attribute Details
#branch ⇒ String
Git Branch information
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def branch @branch end |
#build ⇒ String
Build number
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def build @build end |
#build_date ⇒ String
Build Date
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def build_date @build_date end |
#commiter ⇒ String
Git Commiter information
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def commiter @commiter end |
#committer ⇒ Object
Returns the value of attribute committer.
27 28 29 |
# File 'lib/app_version/app_version.rb', line 27 def committer @committer end |
#format ⇒ String
Format information
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def format @format end |
#major ⇒ String
Major Version number
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def major @major end |
#meta ⇒ String
Semantic version meta info
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def @meta end |
#milestone ⇒ String
Milestone information
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def milestone @milestone end |
#minor ⇒ String
Minor Version number
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def minor @minor end |
#patch ⇒ String
Patch number
23 24 25 |
# File 'lib/app_version/app_version.rb', line 23 def patch @patch end |
Class Method Details
.load(path) ⇒ Object
Loads the version information from a YAML file.
113 114 115 |
# File 'lib/app_version/app_version.rb', line 113 def self.load(path) App::Version.new YAML.load(File.open(path)) end |
.parse(version) ⇒ App:Version
Parses a version string to create an instance of the Version class.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/app_version/app_version.rb', line 88 def self.parse(version) m = version.match(/(\d+)\.(\d+)(?:\.(\d+))?(?:-([\w.\d]+))?(?:\sM(\d+))?(?:\s\((\d+)\))?(?:\sof\s(\w+))?(?:\sby\s(\w+))?(?:\son\s(\S+))?/) fail ArgumentError.new("The version '#{version}' is unparsable") if m.nil? version = App::Version.new major: m[1], minor: m[2], patch: m[3], meta: m[4], milestone: m[5], build: m[6], branch: m[7], committer: m[8] if m[9] && m[9] != '' date = Date.parse(m[9]) version.build_date = date end version end |
Instance Method Details
#<=>(other) ⇒ Integer
Combined Compare operator for version
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/app_version/app_version.rb', line 123 def <=>(other) # if !self.build.nil? && !other.build.nil? # return self.build <=> other.build # end %w(build major minor patch milestone branch meta committer build_date).each do |meth| rhs = send(meth) || -1 lhs = other.send(meth) || -1 ret = lhs <=> rhs return ret unless ret == 0 end 0 end |
#sem_ver_format ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/app_version/app_version.rb', line 139 def sem_ver_format @@sem_ver_format = "#{major}.#{minor}" @@sem_ver_format << ".#{patch}" unless patch.blank? @@sem_ver_format << "-#{}" unless .blank? @@sem_ver_format << "+#{build}" unless build.blank? @@sem_ver_format end |
#to_s ⇒ String
Generate version string
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/app_version/app_version.rb', line 152 def to_s if defined? @format str = eval(@format.to_s.inspect) else str = "#{major}.#{minor}" str << ".#{patch}" unless patch.blank? str << "-#{}" unless .blank? str << " M#{milestone}" unless milestone.blank? str << " (#{build})" unless build.blank? str << " of #{branch}" unless branch.blank? str << " by #{committer}" unless committer.blank? str << " on #{build_date}" unless build_date.blank? end str end |