Class: ParamsProcessor::TypeConvert

Inherits:
Object
  • Object
show all
Defined in:
lib/params_processor/type_convert.rb

Class Method Summary collapse

Class Method Details

.all_ofObject

combined TODO



56
57
58
59
# File 'lib/params_processor/type_convert.rb', line 56

def all_of
  doc = ParamDoc.new name: @doc.name, schema: @doc.all_of.reduce({}, :merge)
  TypeConvert.(@input, based_on: doc)
end

.any_ofObject



66
67
68
69
# File 'lib/params_processor/type_convert.rb', line 66

def any_of
  doc = ParamDoc.new name: @doc.name, schema: @doc.all_of.reduce({}, :merge)
  TypeConvert.(@input, based_on: doc)
end

.arrayObject



44
45
46
47
# File 'lib/params_processor/type_convert.rb', line 44

def array
  return @input unless @input.is_a?(String)
  @input = MultiJson.load(@input)
end

.booleanObject



29
30
31
# File 'lib/params_processor/type_convert.rb', line 29

def boolean
  @input.to_s.in?(%w[ true 1 ]) ? true : false
end

.call(input, based_on:) ⇒ Object



6
7
8
9
10
# File 'lib/params_processor/type_convert.rb', line 6

def call(input, based_on:)
  @input = input
  @doc = based_on
  convert
end

.convertObject

TODO: 循环和递归转换



13
14
15
16
17
# File 'lib/params_processor/type_convert.rb', line 13

def convert
  send(@doc.type || @doc.combined_modes.first) # TODO
rescue NoMethodError
  @input
end

.integerObject

int32 / int64



20
21
22
# File 'lib/params_processor/type_convert.rb', line 20

def integer
  @input.to_i
end

.notObject



71
72
73
# File 'lib/params_processor/type_convert.rb', line 71

def not
  @input
end

.numberObject

float / double



25
26
27
# File 'lib/params_processor/type_convert.rb', line 25

def number
  @input.to_f
end

.objectObject



49
50
51
52
# File 'lib/params_processor/type_convert.rb', line 49

def object
  return @input unless @input.is_a?(String)
  @input = MultiJson.load(@input)
end

.one_ofObject



61
62
63
64
# File 'lib/params_processor/type_convert.rb', line 61

def one_of
  doc = ParamDoc.new name: @doc.name, schema: @doc.all_of.reduce({}, :merge)
  TypeConvert.(@input, based_on: doc)
end

.parse_time(cls) ⇒ Object

helpers



77
78
79
80
81
82
83
# File 'lib/params_processor/type_convert.rb', line 77

def parse_time(cls)
  if @doc.pattern
    cls.send(:strptime, @input, @doc.pattern)
  else
    cls.send(:parse, @input)
  end
end

.stringObject

date / date-time / base64



34
35
36
37
38
39
40
41
42
# File 'lib/params_processor/type_convert.rb', line 34

def string
  case @doc.format
  when 'date'      then parse_time(Date)
  when 'date-time' then parse_time(DateTime)
  when 'base64'    then @input # Base64.strict_decode64(@input)
  when 'binary'    then @input
  else @input.to_s
  end
end