Sha256: 0c7c6cfd0a0cf0cf504169a3e7bf5edd75f33bc33173f8b474c89ea863e067a6

Contents?: true

Size: 1.17 KB

Versions: 11

Compression:

Stored size: 1.17 KB

Contents

require 'set'
require 'yaml'

class Spok
  # Internal: Module responsible for loading and collecting calendar definitions
  # from YAML files.
  module Calendars
    @@calendars = {}

    # Public: Add a new calendar.
    #
    # name - A String or Symbol with the name of the calendar.
    # path - A String with the full path for the calendar YAML file.
    #
    # Returns nothing.
    def self.add(name, path)
      @@calendars[name.to_s] = load(name, path)
    end

    # Public: Get a calendar.
    #
    # name - A String or Symbol with the name of the calendar.
    #
    # Raises a `KeyError` if the calender does not exists.
    # Returns a `Set`.
    def self.get(name)
      @@calendars.fetch(name.to_s) { raise KeyError, "Calendar not found: #{name}" }
    end

    # Internal: Preloads existing calendars into memory.
    #
    # Returns nothing.
    def self.preload
      Dir[File.expand_path("config/*.yml", __dir__)].each do |path|
        name = File.basename(path, '.yml')
        add(name, path)
      end
    end

    def self.load(name, path)
      dates = YAML.safe_load(File.read(path), [Date])
      Set.new(dates[name.to_s])
    end

    private_class_method :load
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spok-2.1.8 lib/spok/calendars.rb
spok-2.1.7 lib/spok/calendars.rb
spok-2.1.6 lib/spok/calendars.rb
spok-2.1.5 lib/spok/calendars.rb
spok-2.1.4 lib/spok/calendars.rb
spok-2.1.3 lib/spok/calendars.rb
spok-2.1.2 lib/spok/calendars.rb
spok-2.1.1 lib/spok/calendars.rb
spok-2.1.0 lib/spok/calendars.rb
spok-2.0.1 lib/spok/calendars.rb
spok-2.0.0 lib/spok/calendars.rb