Class: Puppet::Pops::Types::PTimestampType

Inherits:
PAbstractTimeDataType show all
Defined in:
lib/puppet/pops/types/p_timestamp_type.rb

Constant Summary collapse

DEFAULT =
PTimestampType.new(nil, nil)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAbstractTimeDataType

#_assignable?, #convert_arg, #eql?, #from, #hash, #initialize, #intersect?, #merge, #numeric_from, #numeric_to, #to, #unbounded?

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #eql?, #hash, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

This class inherits a constructor from Puppet::Pops::Types::PAbstractTimeDataType

Class Method Details

.new_function(type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/pops/types/p_timestamp_type.rb', line 12

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_timestamp, type.loader) do
    local_types do
      type 'Formats = Variant[String[2],Array[String[2], 1]]'
    end

    dispatch :now do
    end

    dispatch :from_seconds do
      param 'Variant[Integer,Float]', :seconds
    end

    dispatch :from_string do
      param           'String[1]', :string
      optional_param  'Formats',   :format
      optional_param  'String[1]', :timezone
    end

    dispatch :from_string_hash do
      param <<-TYPE, :hash_arg
        Struct[{
          string => String[1],
          Optional[format] => Formats,
          Optional[timezone] => String[1]
        }]
      TYPE
    end

    def now
      Time::Timestamp.now
    end

    def from_string(string, format = :default, timezone = nil)
      Time::Timestamp.parse(string, format, timezone)
    end

    def from_string_hash(args_hash)
      Time::Timestamp.from_hash(args_hash)
    end

    def from_seconds(seconds)
      Time::Timestamp.new((seconds * Time::NSECS_PER_SEC).to_i)
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



6
7
8
9
10
# File 'lib/puppet/pops/types/p_timestamp_type.rb', line 6

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarType',
               'from' => { KEY_TYPE => POptionalType.new(PTimestampType::DEFAULT), KEY_VALUE => nil },
               'to' => { KEY_TYPE => POptionalType.new(PTimestampType::DEFAULT), KEY_VALUE => nil })
end

Instance Method Details

#generalizeObject



59
60
61
# File 'lib/puppet/pops/types/p_timestamp_type.rb', line 59

def generalize
  DEFAULT
end

#impl_classObject



63
64
65
# File 'lib/puppet/pops/types/p_timestamp_type.rb', line 63

def impl_class
  Time::Timestamp
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/puppet/pops/types/p_timestamp_type.rb', line 67

def instance?(o, guard = nil)
  o.is_a?(Time::Timestamp) && o >= @from && o <= @to
end