Method: Puppet::Pops::Types::PBooleanType.new_function

Defined in:
lib/puppet/pops/types/types.rb

.new_function(type) ⇒ Object



1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
# File 'lib/puppet/pops/types/types.rb', line 1868

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_boolean, type.loader) do
    dispatch :from_args do
      param "Variant[Integer, Float, Boolean, Enum['false','true','yes','no','y','n',true]]",  :from
    end

    argument_mismatch :on_error do
      param  'Any', :from
    end

    def from_args(from)
      from = from.downcase if from.is_a?(String)
      case from
      when Float
        from != 0.0
      when Integer
        from != 0
      when false, 'false', 'no', 'n'
        false
      else
        true
      end
    end

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