Class: ParamsProcessor::TypeConvert
- Inherits:
-
Object
- Object
- ParamsProcessor::TypeConvert
- Defined in:
- lib/params_processor/type_convert.rb
Class Method Summary collapse
-
.all_of ⇒ Object
combined TODO.
- .any_of ⇒ Object
- .array ⇒ Object
- .boolean ⇒ Object
- .call(input, based_on:) ⇒ Object
-
.convert ⇒ Object
TODO: 循环和递归转换.
-
.integer ⇒ Object
int32 / int64.
- .not ⇒ Object
-
.number ⇒ Object
float / double.
- .object ⇒ Object
- .one_of ⇒ Object
-
.parse_time(cls) ⇒ Object
helpers.
-
.string ⇒ Object
date / date-time / base64.
Class Method Details
.all_of ⇒ Object
combined TODO
51 52 53 54 |
# File 'lib/params_processor/type_convert.rb', line 51 def all_of doc = ParamDoc.new name: @doc.name, schema: @doc.all_of.reduce({}, :merge) TypeConvert.(@input, based_on: doc) end |
.any_of ⇒ Object
61 62 63 64 |
# File 'lib/params_processor/type_convert.rb', line 61 def any_of doc = ParamDoc.new name: @doc.name, schema: @doc.all_of.reduce({}, :merge) TypeConvert.(@input, based_on: doc) end |
.array ⇒ Object
41 42 43 |
# File 'lib/params_processor/type_convert.rb', line 41 def array @input end |
.boolean ⇒ Object
27 28 29 |
# File 'lib/params_processor/type_convert.rb', line 27 def boolean @input.to_s.in?(%w[ true 1 ]) ? true : false end |
.call(input, based_on:) ⇒ Object
4 5 6 7 8 |
# File 'lib/params_processor/type_convert.rb', line 4 def call(input, based_on:) @input = input @doc = based_on convert end |
.convert ⇒ Object
TODO: 循环和递归转换
11 12 13 14 15 |
# File 'lib/params_processor/type_convert.rb', line 11 def convert send(@doc.type || @doc.combined_modes.first) # TODO rescue NoMethodError @input end |
.integer ⇒ Object
int32 / int64
18 19 20 |
# File 'lib/params_processor/type_convert.rb', line 18 def integer @input.to_i end |
.not ⇒ Object
66 67 68 |
# File 'lib/params_processor/type_convert.rb', line 66 def not @input end |
.number ⇒ Object
float / double
23 24 25 |
# File 'lib/params_processor/type_convert.rb', line 23 def number @input.to_f end |
.object ⇒ Object
45 46 47 |
# File 'lib/params_processor/type_convert.rb', line 45 def object @input end |
.one_of ⇒ Object
56 57 58 59 |
# File 'lib/params_processor/type_convert.rb', line 56 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
72 73 74 75 76 77 78 |
# File 'lib/params_processor/type_convert.rb', line 72 def parse_time(cls) if @doc.pattern cls.send(:strptime, @input, @doc.pattern) else cls.send(:parse, @input) end end |
.string ⇒ Object
date / date-time / base64
32 33 34 35 36 37 38 39 |
# File 'lib/params_processor/type_convert.rb', line 32 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) else @input.to_s end end |