Class: RequestMigrations::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/request_migrations/version.rb

Defined Under Namespace

Classes: Constraint

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/request_migrations/version.rb', line 12

def initialize(version)
  raise UnsupportedVersionError, "version is unsupported: #{version}" unless
    version.in?(RequestMigrations.supported_versions)

  @format = RequestMigrations.config.version_format.to_sym
  @value  = case @format
            when :semver
              Semverse::Version.coerce(version)
            when :date
              Date.parse(version)
            when :integer
              version.to_i
            when :float
              version.to_f
            when :string
              version.to_s
            else
              raise InvalidVersionFormatError, "invalid version format: #{@format} (must be one of: #{SUPPORTED_VERSION_FORMATS.join(',')}"
            end
rescue Semverse::InvalidVersionFormat,
       Date::Error
  raise InvalidVersionError, "invalid #{@format} version given: #{version}"
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/request_migrations/version.rb', line 9

def format
  @format
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/request_migrations/version.rb', line 9

def value
  @value
end

Class Method Details

.coerce(version) ⇒ Object



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

def coerce(version)
  version.is_a?(self) ? version : new(version)
end

Instance Method Details

#<=>(other) ⇒ Object



36
# File 'lib/request_migrations/version.rb', line 36

def <=>(other) = @value <=> Version.coerce(other).value

#to_sObject



37
# File 'lib/request_migrations/version.rb', line 37

def to_s       = @value.to_s