Class: Cathode::Version
- Inherits:
-
Object
- Object
- Cathode::Version
- 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
-
.all ⇒ Object
readonly
Returns the value of attribute all.
Instance Attribute Summary collapse
-
#ancestor ⇒ Object
readonly
Returns the value of attribute ancestor.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
-
.define(version_number, &block) ⇒ Version
Defines a new version.
-
.exists?(version_number) ⇒ Boolean
Whether a given version exists.
-
.find(version_number) ⇒ Version?
Looks up a version by version number.
-
.standardize(rough_version) ⇒ String
Polyfills a version number snippet to be SemVer-compliant.
Instance Method Summary collapse
-
#action?(resource, action) ⇒ Boolean
Whether an action is defined on a resource on the version.
-
#initialize(version_number, &block) ⇒ Version
constructor
Initializes a new version.
-
#resource?(resource) ⇒ Boolean
Whether a resource is defined on the version.
Methods included from ResourceDsl
Methods included from ActionDsl
#actions, #custom_actions, #default_actions
Constructor Details
#initialize(version_number, &block) ⇒ Version
Initializes a new version.
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
.all ⇒ Object (readonly)
Returns the value of attribute all.
14 15 16 |
# File 'lib/cathode/version.rb', line 14 def all @all end |
Instance Attribute Details
#ancestor ⇒ Object (readonly)
Returns the value of attribute ancestor.
8 9 10 |
# File 'lib/cathode/version.rb', line 8 def ancestor @ancestor end |
#version ⇒ Object (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.
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
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.
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.
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.
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.
88 89 90 |
# File 'lib/cathode/version.rb', line 88 def resource?(resource) _resources.names.include? resource.to_sym end |