Class: Kamaze::Project::Version
Overview
Describe version using a YAML file.
Instance Attribute Summary collapse
- #data_loaded ⇒ Hash readonly protected
-
#file_name ⇒ Pathname|String
readonly
Get filepath used to parse version (YAML file).
Class Method Summary collapse
-
.file_name ⇒ Pathname
Get default filename.
Instance Method Summary collapse
-
#attr_set(attr_name, attr_value) ⇒ Array
protected
Define attribute (as
ro
attr) and set value. -
#initialize(file_name = self.class.file_name) ⇒ Version
constructor
A new instance of Version.
- #load_file ⇒ Hash protected
- #to_h ⇒ Hash
-
#to_path ⇒ String
Return the path as a String.
- #to_s ⇒ String
-
#valid? ⇒ Boolean
Denote version has enough (mninimal) attributes defined.
Constructor Details
#initialize(file_name = self.class.file_name) ⇒ Version
Returns a new instance of Version.
25 26 27 28 29 |
# File 'lib/kamaze/project/version.rb', line 25 def initialize(file_name = self.class.file_name) @file_name = file_name.freeze @data_loaded = self.load_file .map { |k, v| self.attr_set(k, v) }.to_h end |
Instance Attribute Details
#data_loaded ⇒ Hash (readonly, protected)
73 74 75 |
# File 'lib/kamaze/project/version.rb', line 73 def data_loaded @data_loaded end |
#file_name ⇒ Pathname|String (readonly)
Get filepath used to parse version (YAML file).
22 23 24 |
# File 'lib/kamaze/project/version.rb', line 22 def file_name @file_name end |
Class Method Details
.file_name ⇒ Pathname
Get default filename.
65 66 67 |
# File 'lib/kamaze/project/version.rb', line 65 def file_name Pathname.new(__dir__).join('version.yml') end |
Instance Method Details
#attr_set(attr_name, attr_value) ⇒ Array (protected)
Define attribute (as ro
attr) and set value.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/kamaze/project/version.rb', line 87 def attr_set(attr_name, attr_value) inflector = Dry::Inflector.new attr_name = inflector.underscore(attr_name.to_s) self.singleton_class.class_eval do attr_accessor attr_name protected "#{attr_name}=" end self.__send__("#{attr_name}=", attr_value.freeze) [attr_name, attr_value.freeze].freeze end |
#load_file ⇒ Hash (protected)
76 77 78 79 80 |
# File 'lib/kamaze/project/version.rb', line 76 def load_file YAML.load_file(file_name) || {} rescue Errno::ENOENT {} end |
#to_h ⇒ Hash
49 50 51 |
# File 'lib/kamaze/project/version.rb', line 49 def to_h data_loaded.clone.freeze end |
#to_path ⇒ String
Return the path as a String.
57 58 59 |
# File 'lib/kamaze/project/version.rb', line 57 def to_path file_name.to_s end |
#to_s ⇒ String
44 45 46 |
# File 'lib/kamaze/project/version.rb', line 44 def to_s [major, minor, patch].join('.') end |
#valid? ⇒ Boolean
Denote version has enough (mninimal) attributes defined.
34 35 36 37 38 39 40 41 |
# File 'lib/kamaze/project/version.rb', line 34 def valid? ![:major, :minor, :patch] .map { |method| public_send(method) } .map { |v| v.to_s.empty? ? nil : v } .include?(nil) rescue NameError false end |