Class: KeyValueName::IntegerMarshaler

Inherits:
MarshalerBase show all
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

Instance Method Summary collapse

Methods inherited from MarshalerBase

#to_comparable

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_stringObject (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

#baseObject



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

#matcherObject



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