Module: JsonApiResource::Conversions
- Included in:
- Resource
- Defined in:
- lib/json_api_resource/conversions.rb
Instance Method Summary collapse
- #ApiErrors(*args) ⇒ Object
- #ApiResource(klass, *args) ⇒ Object
- #Boolean(*args) ⇒ Object
- #Date(*args) ⇒ Object
- #DateTime(*args) ⇒ Object
Instance Method Details
#ApiErrors(*args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/json_api_resource/conversions.rb', line 3 def ApiErrors(*args) case args.first when ActiveModel::Errors then args.first when Hash then args.first when Array then { base: args } when String then { base: [args] } when NilClass then { base: [[]]} else raise TypeError, "Cannot convert #{ args.inspect} to Error" end end |
#ApiResource(klass, *args) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/json_api_resource/conversions.rb', line 47 def ApiResource(klass, *args) case args.first when klass then args.first when Hash then klass.new(args.first) when klass.client_class then klass.new(args.first.attributes) when Array then args.first.map { |attr| ApiResource(klass, attr) } else raise TypeError, "Cannot convert #{ args.inspect} to #{klass}" end end |
#Boolean(*args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/json_api_resource/conversions.rb', line 30 def Boolean(*args) case args.first when FalseClass then args.first when TrueClass then args.first when String if %w(true 1).include?(args.first.downcase) true elsif %w(false 0).include?(args.first.downcase) false else raise TypeError, "Cannot convert #{ args.inspect} to Boolean" end when Integer then args.first != 0 else raise TypeError, "Cannot convert #{ args.inspect} to Boolean" end end |
#Date(*args) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/json_api_resource/conversions.rb', line 14 def Date(*args) case args.first when String then Date.parse(args.first) when Date then args.first else raise TypeError, "Cannot convert #{ args.inspect} to Date" end end |
#DateTime(*args) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/json_api_resource/conversions.rb', line 22 def DateTime(*args) case args.first when String then DateTime.parse(args.first) when DateTime then args.first else raise TypeError, "Cannot convert #{ args.inspect} to DateTime" end end |