Class: Puppet::Pops::Types::PNumericType

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

Direct Known Subclasses

PFloatType, PIntegerType

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #generalize, #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

#initialize(from, to = Float::INFINITY) ⇒ PNumericType

Returns a new instance of PNumericType.

Raises:

  • (ArgumentError)


940
941
942
943
944
945
946
947
# File 'lib/puppet/pops/types/types.rb', line 940

def initialize(from, to = Float::INFINITY)
  from = -Float::INFINITY if from.nil? || from == :default
  to = Float::INFINITY if to.nil? || to == :default
  raise ArgumentError, "'from' must be less or equal to 'to'. Got (#{from}, #{to}" if from > to

  @from = from
  @to = to
end

Class Method Details

.new_function(type) ⇒ Object



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/puppet/pops/types/types.rb', line 868

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_numeric, type.loader) do
    local_types do
      type "Convertible = Variant[Integer, Float, Boolean, Pattern[/#{FLOAT_PATTERN}/], Timespan, Timestamp]"
      type 'NamedArgs   = Struct[{from => Convertible, Optional[abs] => Boolean}]'
    end

    dispatch :from_args do
      param          'Convertible',  :from
      optional_param 'Boolean',      :abs
    end

    dispatch :from_hash do
      param          'NamedArgs', :hash_args
    end

    argument_mismatch :on_error do
      param          'Any',     :from
      optional_param 'Boolean', :abs
    end

    def from_args(from, abs = false)
      result = from_convertible(from)
      abs ? result.abs : result
    end

    def from_hash(args_hash)
      from_args(args_hash['from'], args_hash['abs'] || false)
    end

    def from_convertible(from)
      case from
      when Float
        from
      when Integer
        from
      when Time::TimeData
        from.to_f
      when TrueClass
        1
      when FalseClass
        0
      else
        begin
          if from[0] == '0'
            second_char = (from[1] || '').downcase
            if second_char == 'b' || second_char == 'x'
              # use built in conversion
              return Integer(from)
            end
          end

          Puppet::Pops::Utils.to_n(from)
        rescue TypeError => e
          raise TypeConversionError, e.message
        rescue ArgumentError => e
          raise TypeConversionError, e.message
        end
      end
    end

    def on_error(from, abs = false)
      if from.is_a?(String)
        _("The string '%{str}' cannot be converted to Numeric") % { str: from }
      else
        t = TypeCalculator.singleton.infer(from).generalize
        _("Value of type %{type} cannot be converted to Numeric") % { type: t }
      end
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



862
863
864
865
866
# File 'lib/puppet/pops/types/types.rb', line 862

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

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


986
987
988
# File 'lib/puppet/pops/types/types.rb', line 986

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)


960
961
962
# File 'lib/puppet/pops/types/types.rb', line 960

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

#hashObject



982
983
984
# File 'lib/puppet/pops/types/types.rb', line 982

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

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

Returns:

  • (Boolean)


990
991
992
# File 'lib/puppet/pops/types/types.rb', line 990

def instance?(o, guard = nil)
  (o.is_a?(Float) || o.is_a?(Integer)) && o >= @from && o <= @to
end

#intersect?(o) ⇒ Boolean

Checks if this numeric range intersects with another

Parameters:

Returns:

  • (Boolean)

    ‘true` if this range intersects with the other range



954
955
956
# File 'lib/puppet/pops/types/types.rb', line 954

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

#numeric_fromFloat, Integer

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

Returns:

  • (Float, Integer)


972
973
974
# File 'lib/puppet/pops/types/types.rb', line 972

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)


978
979
980
# File 'lib/puppet/pops/types/types.rb', line 978

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)


966
967
968
# File 'lib/puppet/pops/types/types.rb', line 966

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

#unbounded?Boolean

Returns:

  • (Boolean)


994
995
996
# File 'lib/puppet/pops/types/types.rb', line 994

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