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

Constructor Details

#initialize(options) ⇒ Convert_boolean

Returns a new instance of Convert_boolean.

Raises:

  • (ArgumentError)


77
78
79
80
81
# File 'lib/peanuts/converters.rb', line 77

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



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

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



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

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