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

Constructor Details

#initialize(from, to) ⇒ PAbstractTimeDataType

Returns a new instance of PAbstractTimeDataType.

Parameters:

  • from (AbstractTime)

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

  • to (AbstractTime)

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

Raises:

  • (ArgumentError)


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

def initialize(from, to)
  @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)


93
94
95
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 93

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

#convert_arg(arg, min) ⇒ Object



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

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
    arg == impl_class.new(arg * Time::NSECS_PER_SEC)
  when Float
    arg == (min ? -Float::INFINITY : Float::INFINITY) ? arg : 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)


49
50
51
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 49

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)


23
24
25
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 23

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

#hashObject



45
46
47
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 45

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



17
18
19
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 17

def intersect?(o)
  self.class == 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:



83
84
85
86
87
88
89
90
91
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 83

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)


35
36
37
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 35

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)


41
42
43
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 41

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)


29
30
31
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 29

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

#unbounded?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 53

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