Class: KeyValueName::IntegerMarshaler
- Inherits:
-
MarshalerBase
- Object
- MarshalerBase
- KeyValueName::IntegerMarshaler
- Defined in:
- lib/key_value_name/marshalers/integer_marshaler.rb
Overview
Read and write integer types. Positive binary, octal and hexadecimal numbers without prefixes are also supported. Padding with zeros is OK, but padding with spaces will not work.
Instance Attribute Summary collapse
-
#format_string ⇒ Object
readonly
Returns the value of attribute format_string.
Instance Method Summary collapse
- #base ⇒ Object
- #generate(value) ⇒ Object
-
#initialize(format: '%d') ⇒ IntegerMarshaler
constructor
A new instance of IntegerMarshaler.
- #matcher ⇒ Object
- #parse(string) ⇒ Object
Methods inherited from MarshalerBase
Constructor Details
#initialize(format: '%d') ⇒ IntegerMarshaler
10 11 12 |
# File 'lib/key_value_name/marshalers/integer_marshaler.rb', line 10 def initialize(format: '%d') @format_string = format end |
Instance Attribute Details
#format_string ⇒ Object (readonly)
Returns the value of attribute format_string.
14 15 16 |
# File 'lib/key_value_name/marshalers/integer_marshaler.rb', line 14 def format_string @format_string end |
Instance Method Details
#base ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/key_value_name/marshalers/integer_marshaler.rb', line 20 def base case format_string when /b\z/i then 2 when /o\z/i then 8 when /x\z/i then 16 else 10 end end |
#generate(value) ⇒ Object
33 34 35 |
# File 'lib/key_value_name/marshalers/integer_marshaler.rb', line 33 def generate(value) format(format_string, value) end |
#matcher ⇒ Object
16 17 18 |
# File 'lib/key_value_name/marshalers/integer_marshaler.rb', line 16 def matcher /[-+]?[0-9a-f]+/i end |
#parse(string) ⇒ Object
29 30 31 |
# File 'lib/key_value_name/marshalers/integer_marshaler.rb', line 29 def parse(string) string.to_i(base) end |