Class: Puppet::Pops::Types::PAbstractTimeDataType
- Inherits:
-
PScalarType
- Object
- TypedModelObject
- PAnyType
- PScalarType
- Puppet::Pops::Types::PAbstractTimeDataType
- Defined in:
- lib/puppet/pops/types/p_timespan_type.rb
Direct Known Subclasses
Constant Summary
Constants inherited from PScalarType
Puppet::Pops::Types::PScalarType::DEFAULT
Constants inherited from PAnyType
Puppet::Pops::Types::PAnyType::DEFAULT
Instance Method Summary collapse
- #_assignable?(o, guard) ⇒ Boolean
- #convert_arg(arg, min) ⇒ Object
- #eql?(o) ⇒ Boolean
-
#from ⇒ Float, Integer
Returns the lower bound of the numeric range or
nilif no lower bound is set. - #hash ⇒ Object
-
#initialize(from, to = nil) ⇒ PAbstractTimeDataType
constructor
A new instance of PAbstractTimeDataType.
-
#intersect?(o) ⇒ Boolean
Checks if this numeric range intersects with another.
-
#merge(o) ⇒ PAbstractTimeDataType?
Concatenates this range with another range provided that the ranges intersect or are adjacent.
-
#numeric_from ⇒ Float, Integer
Same as #from but will return
-Float::Infinityinstead ofnilif no lower bound is set. -
#numeric_to ⇒ Float, Integer
Same as #to but will return
Float::Infinityinstead ofnilif no lower bound is set. -
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or
nilif no upper bound is set. - #unbounded? ⇒ Boolean
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
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.
7 8 9 10 11 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 7 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
94 95 96 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 94 def _assignable?(o, guard) self.class == o.class && numeric_from <= o.numeric_from && numeric_to >= o.numeric_to end |
#convert_arg(arg, min) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 58 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
50 51 52 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 50 def eql?(o) self.class == o.class && @from == o.numeric_from && @to == o.numeric_to end |
#from ⇒ Float, Integer
Returns the lower bound of the numeric range or nil if no lower bound is set.
24 25 26 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 24 def from @from == -Float::INFINITY ? nil : @from end |
#hash ⇒ Object
46 47 48 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 46 def hash @from.hash ^ @to.hash end |
#intersect?(o) ⇒ Boolean
Checks if this numeric range intersects with another
18 19 20 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 18 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
84 85 86 87 88 89 90 91 92 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 84 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_from ⇒ Float, Integer
Same as #from but will return -Float::Infinity instead of nil if no lower bound is set.
36 37 38 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 36 def numeric_from @from end |
#numeric_to ⇒ Float, Integer
Same as #to but will return Float::Infinity instead of nil if no lower bound is set.
42 43 44 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 42 def numeric_to @to end |
#to ⇒ Float, Integer
Returns the upper bound of the numeric range or nil if no upper bound is set.
30 31 32 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 30 def to @to == Float::INFINITY ? nil : @to end |
#unbounded? ⇒ Boolean
54 55 56 |
# File 'lib/puppet/pops/types/p_timespan_type.rb', line 54 def unbounded? @from == -Float::INFINITY && @to == Float::INFINITY end |