Class: Gloo::Objs::Date

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

Constant Summary collapse

KEYWORD =
'date'.freeze
KEYWORD_SHORT =
'date'.freeze
DEFAULT_FORMAT =
'%Y.%m.%d'.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

.doc_dataObject

Get the object's documentation data.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/gloo/objs/dt/date.rb', line 160

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A reference to a date, but without time. The ' \
      "default format is: #{DEFAULT_FORMAT}. Note that you can " \
      'put a date into a datetime object. You can also put a ' \
      'Chronic phrase into the date.',
    :messages => [
      'now — Set to the current system date.',
      'add ({amount}) — Add the amount to the date. The amount will be in the form of "1 day" or "3 months".',
      'sub ({amount}) — Subtract the amount from the date. The amount will be in the form of "1 day" or "2 weeks".',
      'dd — Get the day portion of the date. Put the day number into it.',
      'mm — Get the month portion of the date. Put the month number into it.',
      'yy — Get the 2 digit year portion of the date. Put the 2 digit year into it.',
      'yyyy — Get the 4 digit year portion of the date. Put the 4 digit year into it.',
      'format ({fmt}) — Format the date using a strftime-style format string. Default if none given: %Y-%m-%d. It will have the formatted string.'
    ],
    :examples => <<~EXAMPLES.strip
      #
      # Show the current date.
      #

      date [can] :
        d [date] :
        on_load [script] :
          tell date.d to now
          show date.d
        next [script] :
          put 'tomorrow' into date.d
          show date.d
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



60
61
62
# File 'lib/gloo/objs/dt/date.rb', line 60

def self.messages
  return super + %w[now add sub mm dd yy yyyy format]
end

.short_typenameObject

The short name of the object type.



25
26
27
# File 'lib/gloo/objs/dt/date.rb', line 25

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



18
19
20
# File 'lib/gloo/objs/dt/date.rb', line 18

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_addObject

Add the given modifier to the date.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gloo/objs/dt/date.rb', line 75

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_ddObject

Get the day.



116
117
118
119
# File 'lib/gloo/objs/dt/date.rb', line 116

def msg_dd
  dt = Chronic.parse( self.value )
  @engine.heap.it.set_to "#{dt.day}".rjust(2, '0')
end

#msg_formatObject

Format the date.

Parameters:

  • format (String)

    The format to use.



142
143
144
145
146
147
148
149
150
151
# File 'lib/gloo/objs/dt/date.rb', line 142

def msg_format
  format = "%Y-%m-%d"
  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_mmObject

Get the month.



108
109
110
111
# File 'lib/gloo/objs/dt/date.rb', line 108

def msg_mm
  dt = Chronic.parse( self.value )
  @engine.heap.it.set_to "#{dt.month}".rjust(2, '0')
end

#msg_nowObject

Set to the current date.



67
68
69
70
# File 'lib/gloo/objs/dt/date.rb', line 67

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.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gloo/objs/dt/date.rb', line 92

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

#msg_yyObject

Get the year (2 digit).



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

def msg_yy
  dt = Chronic.parse( self.value )
  @engine.heap.it.set_to dt.year.to_s[-2..-1]
end

#msg_yyyyObject

Get the year.



124
125
126
127
# File 'lib/gloo/objs/dt/date.rb', line 124

def msg_yyyy
  dt = Chronic.parse( self.value )
  @engine.heap.it.set_to dt.year.to_s
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



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

def set_value( new_value )
  if DtTools.is_dt_type? new_value
    self.value = new_value
  else
    self.value = @engine.converter.convert( new_value, 'Date', 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.



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

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