Class: Cassie::Schema::Version

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

Constant Summary collapse

PARTS =
[:major, :minor, :patch, :build].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_number, description = nil, id = nil, executor = nil, executed_at = nil) ⇒ Version

Returns a new instance of Version.



23
24
25
26
27
28
29
# File 'lib/cassie/schema/version.rb', line 23

def initialize(version_number, description=nil, id=nil, executor=nil, executed_at=nil)
  @parts = build_parts(version_number)
  @description    = description
  @id             = id
  @executor       = executor
  @executed_at    = executed_at
end

Instance Attribute Details

#buildObject (readonly)

The build part of the semantic version



55
56
57
# File 'lib/cassie/schema/version.rb', line 55

def build
  parts[3].to_i
end

#descriptionString

The description of the changes introduced in this version

Returns:

  • (String)


14
15
16
# File 'lib/cassie/schema/version.rb', line 14

def description
  @description
end

#executed_atDateTime

The time this version was migrated up

Returns:

  • (DateTime)


20
21
22
# File 'lib/cassie/schema/version.rb', line 20

def executed_at
  @executed_at
end

#executorString

The OS username of the user that migrated this version up

Returns:

  • (String)


17
18
19
# File 'lib/cassie/schema/version.rb', line 17

def executor
  @executor
end

#idCassandra::TimeUuid

The version uuid, if persisted

Returns:

  • (Cassandra::TimeUuid)


8
9
10
# File 'lib/cassie/schema/version.rb', line 8

def id
  @id
end

#majorObject (readonly)

The major part of the semantic version



37
38
39
# File 'lib/cassie/schema/version.rb', line 37

def major
  parts[0].to_i
end

#migrationCassie::Schema::Migration? (readonly)

The migration associated with this version

Returns:



114
115
116
117
118
119
120
# File 'lib/cassie/schema/version.rb', line 114

def migration
  @migration ||= begin
    migration_class_name.constantize.new
  rescue NameError
    nil
  end
end

#minorObject (readonly)

The minor part of the semantic version



43
44
45
# File 'lib/cassie/schema/version.rb', line 43

def minor
  parts[1].to_i
end

#partsArray<Fixnum>

The major, minor, patch, and build parts making up the semantic version

Returns:

  • (Array<Fixnum>)


11
12
13
# File 'lib/cassie/schema/version.rb', line 11

def parts
  @parts
end

#patchObject (readonly)

The patch part of the semantic version



49
50
51
# File 'lib/cassie/schema/version.rb', line 49

def patch
  parts[2].to_i
end

Instance Method Details

#<=>(other) ⇒ Object

Compares versions by semantic version number



81
82
83
84
85
86
87
88
89
90
# File 'lib/cassie/schema/version.rb', line 81

def <=>(other)
  case other
  when Version
    Gem::Version.new(self.number) <=> Gem::Version.new(other.number)
  when String
    Gem::Version.new(self.number) <=> Gem::Version.new(other)
  else
    nil
  end
end

#hashObject



99
100
101
# File 'lib/cassie/schema/version.rb', line 99

def hash
  parts.hash
end

#migration_class_nameObject

The migration class name, as implied by the version number

Examples:

1.2.3

migration_class_name
#=> "Migration_1_2_3_0"


107
108
109
# File 'lib/cassie/schema/version.rb', line 107

def migration_class_name
  "Migration_#{major}_#{minor}_#{patch}_#{build}"
end

#next(bump_type = nil) ⇒ Version

Builds a new version, wiht a version number incremented from this object’s version. Does not propogate any other attributes

Parameters:

  • bump_type (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (bump_type):

  • :build (Symbol)

    Bump the build version

  • :patch (Symbol)

    Bump the patch version, set build to 0

  • :minor (Symbol)

    Bump the minor version, set patch and build to 0

  • :major (Symbol)

    Bump the major version, set minor, patch, and build to 0

  • nil (nil)

    Default, bumps patch, sets build to 0

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cassie/schema/version.rb', line 67

def next(bump_type=nil)
  bump_type ||= :patch
  bump_index = PARTS.index(bump_type.to_sym)

  # 0.2.1 - > 0.2
  bumped_parts = parts.take(bump_index + 1)
  # 0.2 - > 0.3
  bumped_parts[bump_index] = bumped_parts[bump_index].to_i + 1
  # 0.3 - > 0.3.0.0
  bumped_parts += [0]*(PARTS.length - (bump_index + 1))
  self.class.new(bumped_parts.join('.'))
end

#numberObject



31
32
33
# File 'lib/cassie/schema/version.rb', line 31

def number
  parts.join('.')
end

#recorded?Boolean

Returns indicating whether the version has been persisted in the schema metatdata versions persistence store.

Returns:

  • (Boolean)

    indicating whether the version has been persisted in the schema metatdata versions persistence store.



95
96
97
# File 'lib/cassie/schema/version.rb', line 95

def recorded?
  !!self.id
end

#to_hObject



122
123
124
125
126
127
128
129
130
# File 'lib/cassie/schema/version.rb', line 122

def to_h
  {
    id: id,
    number: number,
    description: description,
    executor: executor,
    executed_at: executed_at
  }
end

#to_sObject



132
133
134
# File 'lib/cassie/schema/version.rb', line 132

def to_s
  number
end