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.



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

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

Instance Attribute Details

#valueObject (readonly)



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

def value
  @value
end

Class Method Details

.new_function(type) ⇒ Object



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

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



1840
1841
1842
# File 'lib/puppet/pops/types/types.rb', line 1840

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

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#generalizeObject



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

def generalize
  PBooleanType::DEFAULT
end

#hashObject



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

def hash
  31 ^ @value.hash
end

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

Returns:

  • (Boolean)


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

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