Sha256: 08699e79aebc7cdf6171830a3cd8b458c76760f9a5f814061d313a605e880b35

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

require 'active_support'
require 'active_support/core_ext'

module WorkGuide
  class Guide
    attr_accessor :description, :cycle, :done_at, :priority

    class << self
      def create(args)
        guide = new(args)
        all << guide
        save
        guide
      end

      def all
        @all ||= Storage.new(:guide).load.map { |h| new(h.symbolize_keys) }
      end

      def save
        Storage.new(:guide).store(to_json)
      end

      private

      def to_json
        all.map(&:to_h).to_json
      end
    end

    def initialize(
          description: ,
          cycle: 'daily',
          done_at: nil,
          priority: 'medium'
        )
      @description = description
      @cycle = cycle
      if done_at
        @done_at = Time.parse(done_at)
      end
      @priority = priority
    end

    def should_do?
      if done_at
        case cycle
        when 'hourly'
          1.hour.since(done_at).beginning_of_hour.past?
        when 'daily'
          1.day.since(done_at).beginning_of_day.past?
        when 'weekly'
          1.week.since(done_at).beginning_of_day.past?
        when 'monthly'
          1.month.since(done_at).beginning_of_month.past?
        else
          raise ArgumentError, "Unknown cycle: #{cycle}"
        end
      else
        true
      end
    end

    def priority_rate
      case priority
      when 'high'  ; 1
      when 'medium'; 2
      when 'low'   ; 3
      else         ; 9
      end
    end

    def to_h
      {
        description: description,
        cycle: cycle,
        done_at: done_at,
        priority: priority,
      }
    end

    def to_s
      "[#{cycle}/#{priority}] #{description} (#{done_at || '--'})"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
work_guide-1.2.0 lib/work_guide/guide.rb
work_guide-1.1.1 lib/work_guide/guide.rb
work_guide-1.1.0 lib/work_guide/guide.rb
work_guide-1.0.2 lib/work_guide/guide.rb
work_guide-1.0.1 lib/work_guide/guide.rb
work_guide-1.0.0 lib/work_guide/guide.rb