Class: Gloo::Objs::Datetime

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/dt/datetime.rb

Constant Summary collapse

KEYWORD =
'datetime'.freeze
KEYWORD_SHORT =
'dt'.freeze
DEFAULT_FORMAT =
'%Y.%m.%d %I:%M:%S %P'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, #add_children_on_create?, #add_default_children, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



61
62
63
64
65
66
# File 'lib/gloo/objs/dt/datetime.rb', line 61

def self.messages
  return super + %w[now add sub is_today is_future
    is_past is_yesterday is_tomorrow is_this_week
    begin_day end_day begin_week end_week
    begin_month end_month begin_year end_year format]
end

.short_typenameObject

The short name of the object type.



26
27
28
# File 'lib/gloo/objs/dt/datetime.rb', line 26

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



19
20
21
# File 'lib/gloo/objs/dt/datetime.rb', line 19

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_addObject

Add the given modifier to the date.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gloo/objs/dt/datetime.rb', line 71

def msg_add
  modifier = "1 day"
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate
    modifier = data
  end
  
  dt = Chronic.parse( self.value )
  new_value = DtTools.add( dt, modifier )
  self.set_value( new_value )
  @engine.heap.it.set_to self.value
end

#msg_begin_dayObject

Set the value to the beginning of the day.



158
159
160
161
162
# File 'lib/gloo/objs/dt/datetime.rb', line 158

def msg_begin_day
  dt = self.value.to_time.beginning_of_day
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_begin_monthObject

Set the value to the beginning of the month.



104
105
106
107
108
# File 'lib/gloo/objs/dt/datetime.rb', line 104

def msg_begin_month
  dt = self.value.to_time.beginning_of_month
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_begin_weekObject

Set the value to the beginning of the week.



140
141
142
143
144
# File 'lib/gloo/objs/dt/datetime.rb', line 140

def msg_begin_week
  dt = self.value.to_time.beginning_of_week( start_day = :sunday )
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_begin_yearObject

Set the value to the beginning of the year.



122
123
124
125
126
# File 'lib/gloo/objs/dt/datetime.rb', line 122

def msg_begin_year
  dt = self.value.to_time.beginning_of_year
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_end_dayObject

Set the value to the end of the day.



167
168
169
170
171
# File 'lib/gloo/objs/dt/datetime.rb', line 167

def msg_end_day
  dt = self.value.to_time.end_of_day
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_end_monthObject

Set the value to the end of the month.



113
114
115
116
117
# File 'lib/gloo/objs/dt/datetime.rb', line 113

def msg_end_month
  dt = self.value.to_time.end_of_month
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_end_weekObject

Set the value to the end of the week.



149
150
151
152
153
# File 'lib/gloo/objs/dt/datetime.rb', line 149

def msg_end_week
  dt = self.value.to_time.end_of_week( start_day = :sunday )
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_end_yearObject

Set the value to the end of the year.



131
132
133
134
135
# File 'lib/gloo/objs/dt/datetime.rb', line 131

def msg_end_year
  dt = self.value.to_time.end_of_year
  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_formatObject

Format the date and time.

Parameters:

  • format (String)

    The format to use.



241
242
243
244
245
246
247
248
249
250
# File 'lib/gloo/objs/dt/datetime.rb', line 241

def msg_format
  format = "%Y-%m-%d %H:%M:%S"
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate
    format = data
  end
  dt = Chronic.parse( self.value )
  @engine.heap.it.set_to dt.strftime( format )
end

#msg_is_futureObject

Tell the datetime to check if it is in the future.



186
187
188
189
190
# File 'lib/gloo/objs/dt/datetime.rb', line 186

def msg_is_future
  today = DtTools.is_future?( self.value )
  @engine.heap.it.set_to today
  return today
end

#msg_is_pastObject

Tell the datetime to check if it is in the past.



195
196
197
198
199
# File 'lib/gloo/objs/dt/datetime.rb', line 195

def msg_is_past
  today = DtTools.is_past?( self.value )
  @engine.heap.it.set_to today
  return today
end

#msg_is_this_weekObject

Tell the datetime to check if it is this week.



222
223
224
225
226
# File 'lib/gloo/objs/dt/datetime.rb', line 222

def msg_is_this_week
  today = DtTools.is_this_week?( self.value )
  @engine.heap.it.set_to today
  return today
end

#msg_is_todayObject

Tell the datetime to check if it is today.



177
178
179
180
181
# File 'lib/gloo/objs/dt/datetime.rb', line 177

def msg_is_today
  today = DtTools.is_today?( self.value )
  @engine.heap.it.set_to today
  return today
end

#msg_is_tomorrowObject

Tell the datetime to check if it is tomorrow.



213
214
215
216
217
# File 'lib/gloo/objs/dt/datetime.rb', line 213

def msg_is_tomorrow
  today = DtTools.is_tomorrow?( self.value )
  @engine.heap.it.set_to today
  return today
end

#msg_is_yesterdayObject

Tell the datetime to check if it is yesterday.



204
205
206
207
208
# File 'lib/gloo/objs/dt/datetime.rb', line 204

def msg_is_yesterday
  today = DtTools.is_yesterday?( self.value )
  @engine.heap.it.set_to today
  return today
end

#msg_nowObject

Set to the current date and time.



231
232
233
234
# File 'lib/gloo/objs/dt/datetime.rb', line 231

def msg_now
  self.set_value( DateTime.now )
  @engine.heap.it.set_to self.value
end

#msg_subObject

Subtract the given modifier from the date.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gloo/objs/dt/datetime.rb', line 88

def msg_sub
  modifier = "1 day"
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate
    modifier = data
  end

  dt = Chronic.parse( self.value )
  self.set_value( DtTools.sub( dt, modifier ) )
  @engine.heap.it.set_to self.value
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gloo/objs/dt/datetime.rb', line 33

def set_value( new_value )
  if DtTools.is_dt_type? new_value
    self.value = new_value
  else
    self.value = @engine.converter.convert( new_value, 'DateTime', nil )
  end

  if DtTools.is_dt_type? self.value
    self.value = self.value.strftime( DEFAULT_FORMAT )
  end
end

#sql_valueObject

Value for a SQL query.



48
49
50
51
52
# File 'lib/gloo/objs/dt/datetime.rb', line 48

def sql_value
  return nil if self.value.blank?
  
  return Chronic.parse( self.value )
end