Sha256: 83b4a28ba8af689ff563e948bb106e4a428d18c98c043a2ee4dac2523d9c8551

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 KB

Contents

# -*- coding: utf-8 -*-
require 'date'
require 'yaml'

module Hiroshimarb
  # イベントを表現するクラス
  class Event
    attr_accessor :start_datetime, :end_datetime, :url, :title

    class << self
      def all
        @events || load_yaml
      end

      def recent
        all.last
      end

      def resource_file_path
        root = File.join(File.dirname(__FILE__), "..", "..")
        File.join(root, filename)
      end

      def filename
        ENV["resource"] || File.join("resource", "event.yaml")
      end

      def new_with_hash(hash)
        event = Event.new
        event.title = hash["title"]
        event.url = hash["url"]
        event.date_parse hash["date"]
        event
      end

      def load_yaml
        resource_file = resource_file_path
        events = YAML.parse_file(resource_file).to_ruby.map do |event_hash|
          Event.new_with_hash event_hash
        end
        events.sort { |a,b| a.start_datetime <=> b.end_datetime }
      end
    end

    # 2012-12-01 14:00 - 18:00 のような形式を処理する
    def date_parse(datetime_str)
      start_str, end_str = datetime_str.split(' - ')
      date_str = start_str.split(' ')[0]
      end_str = "#{date_str} #{end_str}"
      self.start_datetime = DateTime.parse(start_str + " JST")
      self.end_datetime = DateTime.parse(end_str + " JST")
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hiroshimarb-0.2.13 lib/hiroshimarb/event.rb
hiroshimarb-0.2.12 lib/hiroshimarb/event.rb
hiroshimarb-0.2.10 lib/hiroshimarb/event.rb
hiroshimarb-0.2.9 lib/hiroshimarb/event.rb
hiroshimarb-0.2.8 lib/hiroshimarb/event.rb
hiroshimarb-0.2.7 lib/hiroshimarb/event.rb
hiroshimarb-0.2.6 lib/hiroshimarb/event.rb