lib/release_dove/release.rb in release_dove-0.1.0 vs lib/release_dove/release.rb in release_dove-0.2.0

- old
+ new

@@ -4,35 +4,49 @@ attr_reader :id, :date, :version, :header, :content CHANGELOG = './CHANGELOG.md' TAG = /^.*(?<header>\[Unreleased\]|\[(?<version>\d+\.\d+\.\d+)\].*(?<date>\d{4}\-\d{2}\-\d{2}))$/i - class << self - def all - return @all if @all + def initialize(id, content) + @id = id + @content = content - @all = [] - releases.each { |*args| @all << new(*args) } + assign_other_attributes + end - @all + def assign_other_attributes + return unless TAG =~ content + + @version = $LAST_MATCH_INFO[:version] + @header = $LAST_MATCH_INFO[:header] + @date = begin + Date.parse $LAST_MATCH_INFO[:date].to_s + rescue ArgumentError + nil + end + end + + def ==(other) + id == other.id + end + + class << self + def all + @all ||= releases.map { |*args| new(*args) } end def take all.first end - alias last take - def first all.last end def size all.size end - alias count size - alias length size def find(id) id = id.to_i releases = all length = releases.length @@ -40,51 +54,37 @@ i = length - id releases.fetch(i) end + alias count size + alias length size + alias last take + private def releases return to_enum(:releases) unless block_given? log_content, log_indices = read_from_file - log_indices.each_with_index do |position, i| - next_position = log_indices[i + 1] - length = next_position ? next_position - position : log_content.length - position + log_indices.each_with_index do |pos, i| id = log_indices.size - i - content = log_content[position, length] + next_pos = log_indices[i + 1] || log_content.size + content = log_content[pos, next_pos - pos] + yield id, content end end def read_from_file file = File.open(CHANGELOG, 'rb', encoding: 'utf-8') content = file.read - indices = content.enum_for(:scan, TAG).map { Regexp.last_match.begin(0) } file.close + indices = content.enum_for(:scan, TAG).map { Regexp.last_match.begin(0) } + [content, indices] end - end - - def initialize(id, content) - @id = id - @content = content - - return unless TAG =~ content - - @version = $LAST_MATCH_INFO[:version] - @header = $LAST_MATCH_INFO[:header] - @date = begin - Date.parse $LAST_MATCH_INFO[:date].to_s - rescue ArgumentError - nil - end - end - - def ==(other) - id == other.id end end