Sha256: b458f7e1277d26e1b71552bdb5097344e949783e5b60ebe577fecd2bab25dc3b

Contents?: true

Size: 1.11 KB

Versions: 40

Compression:

Stored size: 1.11 KB

Contents

module Relaton
  module Render
    class Date
      def initialize(date, options)
        @date = date
        @r = options[:renderer]
      end

      def render
        return @date if @date.nil? || /^\d+$/.match?(@date)

        render1(granularity)
      end

      def render1(format)
        datef = dateparse(format, @r.lang.to_sym)
        case @r.date[format]
        when "to_full_s", "to_long_s", "to_medium_s", "to_short_s"
          datef.send @r.date[format]
        else
          datef.to_additional_s(@r.date[format])
        end
      end

      def granularity
        case @date
        when /^\d+-\d+$/ then "month_year"
        when /^\d+-\d+-\d+$/ then "day_month_year"
        else "date_time"
        end
      end

      def dateparse(format, lang)
        case format
        when "date_time"
          DateTime.parse(@date).localize(lang, timezone: "Zulu")
        when "day_month_year"
          DateTime.parse(@date).localize(lang, timezone: "Zulu").to_date
        when "month_year"
          DateTime.parse("#{@date}-01").localize(lang, timezone: "Zulu").to_date
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
relaton-render-0.5.2 lib/relaton/render/fields/date.rb
relaton-render-0.5.1 lib/relaton/render/fields/date.rb
relaton-render-0.5.0 lib/relaton/render/fields/date.rb
relaton-render-0.4.4 lib/relaton/render/fields/date.rb
relaton-render-0.4.3 lib/relaton/render/fields/date.rb
relaton-render-0.4.2 lib/relaton/render/fields/date.rb
relaton-render-0.4.1 lib/relaton/render/fields/date.rb
relaton-render-0.4.0 lib/relaton/render/fields/date.rb
relaton-render-0.3.10 lib/relaton/render/fields/date.rb
relaton-render-0.3.9 lib/relaton/render/fields/date.rb
relaton-render-0.3.8 lib/relaton/render/fields/date.rb
relaton-render-0.3.7 lib/relaton/render/fields/date.rb
relaton-render-0.3.6.1 lib/relaton/render/fields/date.rb
relaton-render-0.3.6 lib/relaton/render/fields/date.rb
relaton-render-0.3.5.1 lib/relaton/render/fields/date.rb
relaton-render-0.3.5 lib/relaton/render/fields/date.rb
relaton-render-0.3.4 lib/relaton/render/fields/date.rb
relaton-render-0.3.3 lib/relaton/render/fields/date.rb
relaton-render-0.3.2 lib/relaton/render/fields/date.rb
relaton-render-0.3.1 lib/relaton/render/fields/date.rb