Class: Relaton::Render::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/render/fields/date.rb

Instance Method Summary collapse

Constructor Details

#initialize(date, options) ⇒ Date

Returns a new instance of Date.



4
5
6
7
8
9
# File 'lib/relaton/render/fields/date.rb', line 4

def initialize(date, options)
  @date = date
  @r = options[:renderer]
  @type = options[:type]
  @bibitem = options[:bibitem]
end

Instance Method Details

#dateparse(format, lang) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/relaton/render/fields/date.rb', line 35

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

#granularityObject



27
28
29
30
31
32
33
# File 'lib/relaton/render/fields/date.rb', line 27

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

#renderObject



11
12
13
14
15
# File 'lib/relaton/render/fields/date.rb', line 11

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

  render1(granularity)
end

#render1(format) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/relaton/render/fields/date.rb', line 17

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