Class: Puppet::Pops::Types::PBooleanType

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

Constant Summary collapse

DEFAULT =
PBooleanType.new
TRUE =
PBooleanType.new(true)
FALSE =
PBooleanType.new(false)

Instance Attribute Summary collapse

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, #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(value = nil) ⇒ PBooleanType

Returns a new instance of PBooleanType.



1854
1855
1856
# File 'lib/puppet/pops/types/types.rb', line 1854

def initialize(value = nil)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)



1852
1853
1854
# File 'lib/puppet/pops/types/types.rb', line 1852

def value
  @value
end

Class Method Details

.new_function(type) ⇒ Object



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
1902
1903
1904
1905
# File 'lib/puppet/pops/types/types.rb', line 1874

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, Integer
        !from.zero?
      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

.register_ptype(loader, ir) ⇒ Object



1848
1849
1850
# File 'lib/puppet/pops/types/types.rb', line 1848

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarDataType')
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1858
1859
1860
# File 'lib/puppet/pops/types/types.rb', line 1858

def eql?(o)
  o.is_a?(PBooleanType) && @value == o.value
end

#generalizeObject



1862
1863
1864
# File 'lib/puppet/pops/types/types.rb', line 1862

def generalize
  PBooleanType::DEFAULT
end

#hashObject



1866
1867
1868
# File 'lib/puppet/pops/types/types.rb', line 1866

def hash
  31 ^ @value.hash
end

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

Returns:

  • (Boolean)


1870
1871
1872
# File 'lib/puppet/pops/types/types.rb', line 1870

def instance?(o, guard = nil)
  (o == true || o == false) && (@value.nil? || value == o)
end