Class: Puppet::Pops::Types::PAbstractTimeDataType

Inherits:
PScalarType show all
Defined in:
lib/puppet/pops/types/p_timespan_type.rb

Direct Known Subclasses

PTimespanType, PTimestampType

Constant Summary

Constants inherited from PScalarType

Puppet::Pops::Types::PScalarType::DEFAULT

Constants inherited from PAnyType

Puppet::Pops::Types::PAnyType::DEFAULT

Instance Method Summary collapse

Methods inherited from PScalarType

#instance?, register_ptype, #roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #generalize, #instance?, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, new_function, #normalize, #really_instance?, register_ptype, #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

#initialize(from, to = nil) ⇒ PAbstractTimeDataType

Returns a new instance of PAbstractTimeDataType.

Parameters:

  • from (AbstractTime)

    lower bound for this type. Nil or :default means unbounded

  • to (AbstractTime) (defaults to: nil)

    upper bound for this type. Nil or :default means unbounded

Raises:

  • (ArgumentError)


8
9
10
11
12
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 8

def initialize(from, to = nil)
  @from = convert_arg(from, true)
  @to = convert_arg(to, false)
  raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{@from}, #{@to}" unless @from <= @to
end

Instance Method Details

#_assignable?(o, guard) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 96

def _assignable?(o, guard)
  instance_of?(o.class) && numeric_from <= o.numeric_from && numeric_to >= o.numeric_to
end

#convert_arg(arg, min) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 59

def convert_arg(arg, min)
  case arg
  when impl_class
    arg
  when Hash
    impl_class.from_hash(arg)
  when nil, :default
    min ? -Float::INFINITY : Float::INFINITY
  when String
    impl_class.parse(arg)
  when Integer
    impl_class.new(arg * Time::NSECS_PER_SEC)
  when Float
    impl_class.new(arg * Time::NSECS_PER_SEC)
  else
    raise ArgumentError, "Unable to create a #{impl_class.name} from a #{arg.class.name}" unless arg.nil? || arg == :default

    nil
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 51

def eql?(o)
  self.class == o.class && @from == o.numeric_from && @to == o.numeric_to
end

#fromFloat, Integer

Returns the lower bound of the numeric range or ‘nil` if no lower bound is set.

Returns:

  • (Float, Integer)


25
26
27
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 25

def from
  @from == -Float::INFINITY ? nil : @from
end

#hashObject



47
48
49
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 47

def hash
  @from.hash ^ @to.hash
end

#intersect?(o) ⇒ Boolean

Checks if this numeric range intersects with another

Parameters:

Returns:

  • (Boolean)

    ‘true` if this range intersects with the other range



19
20
21
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 19

def intersect?(o)
  instance_of?(o.class) && !(@to < o.numeric_from || o.numeric_to < @from)
end

#merge(o) ⇒ PAbstractTimeDataType?

Concatenates this range with another range provided that the ranges intersect or are adjacent. When that’s not the case, this method will return ‘nil`

Parameters:

Returns:



86
87
88
89
90
91
92
93
94
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 86

def merge(o)
  if intersect?(o) || adjacent?(o)
    new_min = numeric_from <= o.numeric_from ? numeric_from : o.numeric_from
    new_max = numeric_to >= o.numeric_to ? numeric_to : o.numeric_to
    self.class.new(new_min, new_max)
  else
    nil
  end
end

#numeric_fromFloat, Integer

Same as #from but will return ‘-Float::Infinity` instead of `nil` if no lower bound is set.

Returns:

  • (Float, Integer)


37
38
39
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 37

def numeric_from
  @from
end

#numeric_toFloat, Integer

Same as #to but will return ‘Float::Infinity` instead of `nil` if no lower bound is set.

Returns:

  • (Float, Integer)


43
44
45
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 43

def numeric_to
  @to
end

#toFloat, Integer

Returns the upper bound of the numeric range or ‘nil` if no upper bound is set.

Returns:

  • (Float, Integer)


31
32
33
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 31

def to
  @to == Float::INFINITY ? nil : @to
end

#unbounded?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 55

def unbounded?
  @from == -Float::INFINITY && @to == Float::INFINITY
end