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


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

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

Instance Attribute Details

#valueObject (readonly)


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

def value
  @value
end

Class Method Details

.new_function(type) ⇒ Object


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

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

.register_ptype(loader, ir) ⇒ Object


1843
1844
1845
# File 'lib/puppet/pops/types/types.rb', line 1843

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

Instance Method Details

#eql?(o) ⇒ Boolean


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

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

#generalizeObject


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

def generalize
  PBooleanType::DEFAULT
end

#hashObject


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

def hash
  31 ^ @value.hash
end

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


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

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