Class: Versionable::Versions
- Inherits:
-
Object
- Object
- Versionable::Versions
- Defined in:
- lib/versionable/versions.rb
Overview
A container for all versions of a given module.
Constant Summary collapse
- COMPARISON_REGEX =
/^(<|<=|=|>=|>)\s+((?:0|[1-9]\d*)(?:\.(?:0|[1-9]\d*))*)$/
Instance Method Summary collapse
-
#build(version_number, &block) ⇒ Object
Build and store a new version with the given number.
-
#find(version_requirement) ⇒ Object
Find the maximal version satisifying the given requirement.
-
#initialize(versioned_module) ⇒ Versions
constructor
Construct a Versions recording versions of the passed module.
Constructor Details
#initialize(versioned_module) ⇒ Versions
Construct a Versions recording versions of the passed module.
7 8 9 |
# File 'lib/versionable/versions.rb', line 7 def initialize(versioned_module) @latest_version = versioned_module end |
Instance Method Details
#build(version_number, &block) ⇒ Object
Build and store a new version with the given number.
Versions must be built in increasing order: if version_number is not greater than the previous version, an ArgumentError is raised. The initial version number is always 0.
If a block parameter is passed, it is included in the created version. If this is the first time a block is passed, then record the previous version as the default one. Once build is called with a block, subsequent versions must also pass a block or a VersioningError will be raised.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/versionable/versions.rb', line 19 def build(version_number, &block) version_number = VersionNumber.new(version_number) raise ArgumentError.new "Can't bump to #{version_number} from #{latest_version_number}" unless latest_version_number < version_number raise VersioningError.new "Once called with a block, all subsequent versions must pass a block." if default_version and not block if block and default_version.nil? self.default_version = latest_version self.latest_version = latest_version.dup versions[latest_version_number] = default_version else versions[latest_version_number] = latest_version.dup end self.latest_version_number = version_number latest_version.module_eval &block if block end |
#find(version_requirement) ⇒ Object
Find the maximal version satisifying the given requirement.
The requirement may be a version string such as “1.0.7”, or a comparator followed by a version such as “< 3.0”.
Returns the matching module if found, nil otherwise.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/versionable/versions.rb', line 43 def find(version_requirement) if version_requirement =~ VersionNumber::VERSION_NUMBER_REGEX versions[VersionNumber.new(version_requirement)] elsif version_requirement =~ Versions::COMPARISON_REGEX comparator, version_requirement = $1, VersionNumber.new($2) comparator = '==' if comparator == '=' # stick that in your pipe and smoke it. comparator = comparator.to_sym match = (versions.keys + [latest_version_number]).select { |v| v.send comparator, version_requirement }.max versions[match] else nil end end |