Class: Peanuts::Converter::Convert_boolean

Inherits:
Convert_string show all
Defined in:
lib/peanuts/converters.rb

Overview

An XSD boolean.

Specifier

:boolean

Options:

:format => :true_false

Format variation.

:true_false

true/false

:yes_no

yes/no

:numeric

0/1

In addition supports all options of Convert_string.

See also Convert_yesno.

Direct Known Subclasses

Convert_yesno

Instance Method Summary collapse

Methods inherited from Peanuts::Converter

create, create!, lookup, lookup!

Constructor Details

#initialize(options) ⇒ Convert_boolean

Returns a new instance of Convert_boolean.

Raises:

  • (ArgumentError)


86
87
88
89
90
# File 'lib/peanuts/converters.rb', line 86

def initialize(options)
  super
  @format = options[:format] || :truefalse
  raise ArgumentError, "unrecognized format #{@format}" unless [:truefalse, :yesno, :numeric].include?(@format)
end

Instance Method Details

#from_xml(string) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/peanuts/converters.rb', line 102

def from_xml(string)
  case string = super(string)
  when nil then nil
  when '1', 'true', 'yes' then true
  when '0', 'false', 'no' then false
  else
    raise ArgumentError, "invalid value for boolean: #{string.inspect}"
  end
end

#to_xml(flag) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/peanuts/converters.rb', line 92

def to_xml(flag)
  return nil if flag.nil?
  string = case @format
  when :true_false, :truefalse then flag ? 'true' : 'false'
  when :yes_no, :yesno then flag ? 'yes' : 'no'
  when :numeric then flag ? '0' : '1'
  end
  super(string)
end