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_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]
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.



163
164
165
166
167
# File 'lib/gloo/objs/dt/datetime.rb', line 163

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
109
110
111
112
# File 'lib/gloo/objs/dt/datetime.rb', line 104

def msg_begin_month
  # dt = DtTools.beginning_of_month( self.value.to_time )
  dt = self.value.to_time.beginning_of_month
  self.set_value dt
  @engine.heap.it.set_to dt

  self.set_value dt
  @engine.heap.it.set_to dt
end

#msg_begin_weekObject

Set the value to the beginning of the week.



145
146
147
148
149
# File 'lib/gloo/objs/dt/datetime.rb', line 145

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.



127
128
129
130
131
# File 'lib/gloo/objs/dt/datetime.rb', line 127

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.



172
173
174
175
176
# File 'lib/gloo/objs/dt/datetime.rb', line 172

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.



117
118
119
120
121
122
# File 'lib/gloo/objs/dt/datetime.rb', line 117

def msg_end_month
  # dt = DtTools.end_of_month( self.value.to_time )
  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.



154
155
156
157
158
# File 'lib/gloo/objs/dt/datetime.rb', line 154

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.



136
137
138
139
140
# File 'lib/gloo/objs/dt/datetime.rb', line 136

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

#msg_is_futureObject

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



191
192
193
194
195
# File 'lib/gloo/objs/dt/datetime.rb', line 191

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.



200
201
202
203
204
# File 'lib/gloo/objs/dt/datetime.rb', line 200

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.



227
228
229
230
231
# File 'lib/gloo/objs/dt/datetime.rb', line 227

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.



182
183
184
185
186
# File 'lib/gloo/objs/dt/datetime.rb', line 182

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.



218
219
220
221
222
# File 'lib/gloo/objs/dt/datetime.rb', line 218

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.



209
210
211
212
213
# File 'lib/gloo/objs/dt/datetime.rb', line 209

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.



236
237
238
239
# File 'lib/gloo/objs/dt/datetime.rb', line 236

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