Method: Numerals::Format::Input#read

Defined in:
lib/numerals/format/input.rb

#read(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/numerals/format/input.rb', line 6

def read(*args)
  options = {}
  args.each do |arg|
    case arg
    when String
      options.merge! text: arg
    when Hash
      options.merge! arg
    else
      options.merge! type: arg
    end
  end

  text = options[:text]
  options[:type] ||= options[:as]

  # 1. Obtain destination type
  selector = options[:context] || options[:type]
  conversion = Conversions[selector, options[:type_options]]
  if conversion
    type = conversion.type
  else
    type = options[:type]
    if type != Numeral
      raise Format::InvalidNumericType, "Invalid type #{selector.inspect}"
    end
  end

  # if conversion.is_a?(ContextConversion)
  #   context = conversion.context
  # end

  # 2. dissassemble (parse notation): text notation => text parts
  text_parts = Format.disassemble(@notation, self, text)

  # 3. Convert text parts to values and generate a Numeral
  numeral = partition_in(text_parts)

  # 4. Convert to requested type:
  if type == Numeral
    return numeral
  else
    conversion_in(numeral, options)
  end
end