Class: CulturalDates::CulturalInterval

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cultural_dates/cultural_interval.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = "", debug = false) ⇒ CulturalInterval

Returns a new instance of CulturalInterval.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cultural_dates/cultural_interval.rb', line 13

def initialize(val="", debug=false)
  if val
    begin
      parse_result = DateStringParser.new.parse_with_debug(val, reporter: Parslet::ErrorReporter::Deepest.new)
      puts "parse_result: #{parse_result.inspect}" if debug
      transformed_result = DateTransform.new.apply(parse_result)
      puts "transformed_result: #{transformed_result.inspect}" if debug
      if transformed_result
        @value = transformed_result
      end
    rescue Parslet::ParseFailed => e
      @value = nil
    end
  else
    @value = nil
  end
  # puts "@value: #{@value}"
end

Class Method Details

.parse(val, debug = false) ⇒ Object



8
9
10
# File 'lib/cultural_dates/cultural_interval.rb', line 8

def parse(val, debug=false)
  return CulturalInterval.new(val, debug)
end

Instance Method Details

#begin_intervalObject Also known as: beginning_interval, beginning



73
74
75
76
77
# File 'lib/cultural_dates/cultural_interval.rb', line 73

def begin_interval
  from = earliest || :unknown
  to = earliest_definite || :unknown
  EDTF::Interval.new(from, to)
end

#botbObject



32
33
34
# File 'lib/cultural_dates/cultural_interval.rb', line 32

def botb
  @botb ||= CulturalDate.edtf @value[:botb]
end

#boteObject



36
37
38
# File 'lib/cultural_dates/cultural_interval.rb', line 36

def bote
  @bote ||= CulturalDate.edtf @value[:bote]
end

#definite_intervalObject



98
99
100
101
102
# File 'lib/cultural_dates/cultural_interval.rb', line 98

def definite_interval
  from = earliest_definite || :unknown
  to = latest_definite || :unknown
  EDTF::Interval.new(from, to)
end

#earliestObject



49
50
51
52
# File 'lib/cultural_dates/cultural_interval.rb', line 49

def earliest
   return nil if botb.unknown?
   botb.earliest
end

#earliest_definiteObject



59
60
61
62
63
64
# File 'lib/cultural_dates/cultural_interval.rb', line 59

def earliest_definite
   return nil if eotb.unknown? && bote.unknown?
   return latest_definite if eotb.unknown?
   return eotb.earliest if eotb == bote
   eotb.latest
end

#end_intervalObject Also known as: ending_interval, ending



82
83
84
85
86
# File 'lib/cultural_dates/cultural_interval.rb', line 82

def end_interval
  from = latest_definite || :unknown
  to = latest || :unknown
  EDTF::Interval.new(from, to)
end

#eotbObject



40
41
42
# File 'lib/cultural_dates/cultural_interval.rb', line 40

def eotb
  @eotb ||= CulturalDate.edtf @value[:eotb]
end

#eoteObject



44
45
46
# File 'lib/cultural_dates/cultural_interval.rb', line 44

def eote
  @eote ||= CulturalDate.edtf @value[:eote]
end

#latestObject



54
55
56
57
# File 'lib/cultural_dates/cultural_interval.rb', line 54

def latest
   return nil if eote.unknown?
   eote.latest
end

#latest_definiteObject



66
67
68
69
70
71
# File 'lib/cultural_dates/cultural_interval.rb', line 66

def latest_definite
   return nil if bote.unknown? && eotb.unknown?
   return earliest_definite if bote.unknown?
   return bote.latest if eotb == bote
   bote.earliest
end

#possible_intervalObject



90
91
92
93
94
# File 'lib/cultural_dates/cultural_interval.rb', line 90

def possible_interval
  from = earliest || :unknown
  to = latest || :unknown
  EDTF::Interval.new(from, to)
end

#to_definite_edtfObject



109
110
111
# File 'lib/cultural_dates/cultural_interval.rb', line 109

def to_definite_edtf 
  definite_interval.edtf
end

#to_edtfObject



105
106
107
# File 'lib/cultural_dates/cultural_interval.rb', line 105

def to_edtf 
  possible_interval.edtf
end

#to_sString

Generate a textual representation of the timeframe of the period.

Returns:

  • (String)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cultural_dates/cultural_interval.rb', line 115

def to_s

  # Handle special "throughout" case
  if (eotb.known? && bote.known?) && !(botb.known? || eote.known?) && eotb == bote
    return "throughout #{eotb}"
  end

  # Handle special "throughout, until" case
  if (eotb.known? && bote.known? && eote.known?) && !botb.known? && eotb == bote
    return "throughout #{eotb} until no later than #{eote}"
  end

  # Handle special "on" case
  if (botb.known? && eotb.known? && bote.known? && eote.known?) && 
     (botb == eotb && bote == eote && botb == eote) &&
     botb.earliest == botb.latest
     return "on #{botb}"
  end

  first_string = ""
  if botb.known? && eotb.known?
    if botb == eotb
      first_string = botb
    else
      first_string = "sometime between #{botb} and #{eotb}"
    end
  elsif botb.known?
    first_string = "after #{botb}"
  elsif eotb.known?
    first_string = "by #{eotb}"
  end

  second_string = nil
  if bote.known? && eote.known?
    if bote == eote
      second_string = bote
    else
      second_string = "sometime between #{bote} and #{eote}"
    end
  elsif bote.known?
    second_string = "at least #{bote}"
  elsif eote.known?
    second_string = "no later than #{eote}"
  end

  [first_string,second_string].compact.join(" until ").strip

end