Class: Cathode::Version

Inherits:
Object
  • Object
show all
Includes:
ActionDsl, ResourceDsl
Defined in:
lib/cathode/version.rb

Overview

A ‘Version` encapsulates a specific SemVer-compliant version of the API with a set of resources and actions.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceDsl

#_resources

Methods included from ActionDsl

#actions, #custom_actions, #default_actions

Constructor Details

#initialize(version_number, &block) ⇒ Version

Initializes a new version.

Parameters:

  • version_number (String)

    A SemVer-compliant version number.

  • block

    A block defining the version’s resources and actions, and has access to the methods in the ActionDsl and ResourceDsl



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cathode/version.rb', line 71

def initialize(version_number, &block)
  @version = Semantic::Version.new Version.standardize(version_number)

  if Version.all.present?
    @ancestor = Version.all.last
    @_resources = DeepClone.clone @ancestor._resources
    actions.add ancestor.actions.objects
  end

  instance_eval(&block) if block_given?

  Version.all << self
end

Class Attribute Details

.allObject (readonly)

Returns the value of attribute all.



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

def all
  @all
end

Instance Attribute Details

#ancestorObject (readonly)

Returns the value of attribute ancestor.



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

def ancestor
  @ancestor
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.define(version_number, &block) ⇒ Version

Defines a new version.

Parameters:

  • version_number (String, Fixnum, Float)

    A number or string representing a SemVer-compliant version number. If a ‘Fixnum` or `Float` is passed, it will be converted to a string before being evaluated for SemVer compliance, so passing `1.5` is equivalent to passing `’1.5.0’‘.

  • block

    A block defining the version’s resources and actions, and has access to the methods in the ActionDsl and ResourceDsl

Returns:



25
26
27
28
29
30
31
32
33
# File 'lib/cathode/version.rb', line 25

def define(version_number, &block)
  version = Version.find(version_number)
  if version.present?
    version.instance_eval(&block)
  else
    version = self.new(version_number, &block)
  end
  version
end

.exists?(version_number) ⇒ Boolean

Whether a given version exists

Parameters:

  • version_number (String)

    The version to check

Returns:

  • (Boolean)


62
63
64
# File 'lib/cathode/version.rb', line 62

def exists?(version_number)
  find(version_number).present?
end

.find(version_number) ⇒ Version?

Looks up a version by version number.

Parameters:

  • version_number (String)

    The version to find

Returns:

  • (Version, nil)

    The version if found, ‘nil` if there is no such version



53
54
55
56
57
# File 'lib/cathode/version.rb', line 53

def find(version_number)
  Version.all.detect { |v| v.version == standardize(version_number) }
rescue ArgumentError
  nil
end

.standardize(rough_version) ⇒ String

Polyfills a version number snippet to be SemVer-compliant.

Parameters:

  • rough_version (String)

    A version number snippet such as ‘1’ or ‘2.5’

Returns:

  • (String)

    The SemVer-compliant version number



39
40
41
42
43
44
45
46
47
# File 'lib/cathode/version.rb', line 39

def standardize(rough_version)
  version_parts = rough_version.to_s.split '.'
  if version_parts.count < 2
    version_parts << [0, 0]
  elsif version_parts.count < 3
    version_parts << [0]
  end
  version_parts.join '.'
end

Instance Method Details

#action?(resource, action) ⇒ Boolean

Whether an action is defined on a resource on the version.

Parameters:

  • resource (Symbol)

    The resource’s name

  • action (Symbol)

    The action’s name

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
# File 'lib/cathode/version.rb', line 96

def action?(resource, action)
  resource = resource.to_sym
  action = action.to_sym

  return false unless resource?(resource)

  _resources.find(resource).actions.names.include? action
end

#resource?(resource) ⇒ Boolean

Whether a resource is defined on the version.

Parameters:

  • resource (Symbol)

    The resource’s name

Returns:

  • (Boolean)


88
89
90
# File 'lib/cathode/version.rb', line 88

def resource?(resource)
  _resources.names.include? resource.to_sym
end