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
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_of ⇒ Object
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 |
.array ⇒ Object
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 |
.boolean ⇒ Object
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 |
.convert ⇒ Object
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 |
.integer ⇒ Object
int32 / int64
20 21 22 |
# File 'lib/params_processor/type_convert.rb', line 20 def integer @input.to_i end |
.not ⇒ Object
71 72 73 |
# File 'lib/params_processor/type_convert.rb', line 71 def not @input end |
.number ⇒ Object
float / double
25 26 27 |
# File 'lib/params_processor/type_convert.rb', line 25 def number @input.to_f end |
.object ⇒ Object
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_of ⇒ Object
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 |
.string ⇒ Object
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 |