Sha256: 782c333caf77538e3e5c0bb123ee86eb9aa7b81883de93d6ae92890479d59245

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

require "hiccup/convenience"
require "hiccup/core_ext/fixnum"


module Hiccup
  module Humanizable
    include Convenience



    def humanize(format: "%Y-%m-%d")
      case kind
      when :never;      start_date.strftime(format)
      when :weekly;     weekly_humanize
      when :monthly;    monthly_humanize
      when :annually;   yearly_humanize
      else;             "Invalid"
      end
    end



  private



    def weekly_humanize
      weekdays_sentece = weekly_pattern.map(&:humanize).to_sentence
      if skip == 1 || weekly_pattern.length == 1
        sentence("Every", ordinal, weekdays_sentece)
      else
        sentence(weekdays_sentece, "of every", ordinal, "week")
      end
    end

    def monthly_humanize
      monthly_occurrences = monthly_pattern.map(&method(:monthly_occurrence_to_s)).to_sentence
      sentence("The", monthly_occurrences, "of every", ordinal, "month")
    end

    def yearly_humanize
      sentence("Every", ordinal, "year on", self.start_date.strftime('%B'), self.start_date.strftime('%e').strip)
    end



    def monthly_occurrence_to_s(monthly_occurrence)
      if monthly_occurrence.is_a?(Array)
        _skip, weekday = monthly_occurrence
        ordinal = _skip.human_ordinalize
        sentence(ordinal, weekday.humanize)
      else
        monthly_occurrence < 0 ? "last day" : monthly_occurrence.ordinalize
      end
    end

    def ordinal
      skip && skip.human_ordinalize(1 => nil, 2 => "other")
    end

    def sentence(*array)
      array.compact.join(" ")
    end



  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hiccup-0.6.0 lib/hiccup/humanizable.rb
hiccup-0.5.20 lib/hiccup/humanizable.rb
hiccup-0.5.19 lib/hiccup/humanizable.rb
hiccup-0.5.18 lib/hiccup/humanizable.rb
hiccup-0.5.17 lib/hiccup/humanizable.rb
hiccup-0.5.16 lib/hiccup/humanizable.rb
hiccup-0.5.15 lib/hiccup/humanizable.rb