Module: JsonApiResource::Conversions
- Included in:
- Queryable::ClassMethods, Resource
- Defined in:
- lib/json_api_resource/conversions.rb
Instance Method Summary collapse
- #Address(*args) ⇒ Object
- #ApiErrors(*args) ⇒ Object
- #ApiResource(klass, *args) ⇒ Object
- #Boolean(*args) ⇒ Object
- #Date(*args) ⇒ Object
- #DateTime(*args) ⇒ Object
- #Symbolize(*args) ⇒ Object
Instance Method Details
#Address(*args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/json_api_resource/conversions.rb', line 3 def Address(*args) case args.first when Address then args.first when Hash then Address.new(args.first) when Address.client then Address.new(args.first.attributes) when Array then args.first.map { |attr| Address(attr) } when Integer then Address.new(* args) when String then Address.new(* args.first.split(':'). map(&:to_i)) else raise TypeError, "Cannot convert #{ args.inspect} to Address" end end |
#ApiErrors(*args) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/json_api_resource/conversions.rb', line 15 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] } else raise TypeError, "Cannot convert #{ args.inspect} to Error" end end |
#ApiResource(klass, *args) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/json_api_resource/conversions.rb', line 65 def ApiResource(klass, *args) case args.first when klass then args.first when Hash then klass.new(args.first) when klass.client then klass.new(args.first.attributes) when Array then args.first.map { |attr| JsonApiResource(attr, klass) } else raise TypeError, "Cannot convert #{ args.inspect} to #{klass}" end end |
#Boolean(*args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/json_api_resource/conversions.rb', line 41 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
25 26 27 28 29 30 31 |
# File 'lib/json_api_resource/conversions.rb', line 25 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
33 34 35 36 37 38 39 |
# File 'lib/json_api_resource/conversions.rb', line 33 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 |
#Symbolize(*args) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/json_api_resource/conversions.rb', line 58 def Symbolize(*args) case args.first when String then args.first.underscore.parameterize('_').to_sym else args.first end end |