Class: Gloo::Objs::Time

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

Constant Summary collapse

KEYWORD =
'time'.freeze
KEYWORD_SHORT =
'time'.freeze
DEFAULT_FORMAT =
'%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

.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/time.rb', line 160

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A reference to a time, but without a date. ' \
      "The default format is: #{DEFAULT_FORMAT}. Note that you " \
      'can put a time into a datetime object. You can also put a ' \
      'Chronic phrase into the time.',
    :messages => [
      'now — Set to the current system time.',
      '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".',
      'hh — Get the hour portion of the time. Put the hour into it.',
      'mm — Get the minute portion of the time. Put the minute into it.',
      'ss — Get the second portion of the time. Put the second into it.',
      'am — Get the am/pm portion of the time. Put "am" or "pm" into it.',
      'format ({fmt}) — Format the time using a strftime-style format string. Default if none given: %H:%M:%S. It will have the formatted string.'
    ],
    :examples => <<~EXAMPLES.strip
      #
      # Show the current time.
      #

      time [can] :
        t [time] :
        on_load [script] :
          tell time.t to now
          show time.t
        next [script] :
          put '1 hour from now' into time.t
          show time.t
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



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

def self.messages
  return super + %w[now add sub hh mm ss am format]
end

.short_typenameObject

The short name of the object type.



25
26
27
# File 'lib/gloo/objs/dt/time.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/time.rb', line 18

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_addObject

Add the given modifier to the time.



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

def msg_add
  modifier = "1 hour"
  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_amObject

Get the AM/PM.



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

def msg_am
  dt = Chronic.parse( self.value )
  @engine.heap.it.set_to dt.strftime( '%p' )
end

#msg_formatObject

Format the time.

Parameters:

  • format (String)

    The format to use.



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

def msg_format
  format = "%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_hhObject

Get the hour.



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

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

#msg_mmObject

Get the minute.



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

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

#msg_nowObject

Set to the current time.



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

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

#msg_ssObject

Get the second.



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

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

#msg_subObject

Subtract the given modifier from the time.



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

def msg_sub
  modifier = "1 hour"
  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.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gloo/objs/dt/time.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, 'Time', 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/time.rb', line 47

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