data_steroid 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57b5bbd206036f4e3f7435df8b3108c04ba47b88
4
- data.tar.gz: 793926a6ea2ef60cc1087eaf547cb5bf0efe1067
3
+ metadata.gz: a1ea709eb2675cbdd294ab7d4e55977e918488d0
4
+ data.tar.gz: 3eec8d55cdd716cb92de5110b8fe85f8f2d6c59b
5
5
  SHA512:
6
- metadata.gz: 3a840565696832cb8e22f863355deff1932da017d8bbf05137cb743138aa9e4887b8841e2f48c720f015438e5816d4c9bc932364c9aa9db28e9173aa5d261e6d
7
- data.tar.gz: 86ca125f2075cd2645a28c0d4be627092b12fa98b13a2767d8089122e0e6f29091e96d1ac66e57e8a5e193af0b7893946f16f7c23ed59191106c937031e35792
6
+ metadata.gz: 175d002dac45ff1aa456ee6fe96de540e2dc95f28845287dee63ec9c15ea517a9195e03cb7b0afa821a92280a496601165c29c06d80172f6ab8405e9be474007
7
+ data.tar.gz: 9cf46386c6b0c24f1bf703d5866a49f2adde8556028d4d0a86c560f4b151382e000b6f2194d1045e0e22be7cca1ebb2d117af1a5e96c7c7eb404caffc800f7a7
@@ -45,14 +45,66 @@ module DataSteroid
45
45
  def coercer
46
46
  @coercer ||= Coercible::Coercer.new
47
47
  end
48
+
49
+ def coerce(value, type)
50
+ if value.nil? || !type
51
+ value
52
+ else
53
+ case
54
+ when Array == type || Array === type then coerce_array(value, type)
55
+ when Hash == type || Hash === type then coerce_hash(value, type)
56
+ when type == Time then coerce_time(value)
57
+ else coerce_other(value, type)
58
+ end
59
+ end
60
+ end
61
+
62
+ def coerce_array(value, type)
63
+ # type: generic Array
64
+ if type == Array
65
+ coerce(element, type)
66
+ # type: Array[Something]
67
+ elsif value.respond_to?(:map)
68
+ value.map do |element|
69
+ coerce(element, type[0])
70
+ end
71
+ else
72
+ raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
73
+ end
74
+ end
75
+
76
+ def coerce_hash(value, type)
77
+ # type: generic Hash
78
+ if type == Hash
79
+ coerce(element, type)
80
+ # type: Hash[Something => Other thing]
81
+ elsif value.is_a?(Hash)
82
+ k_type, v_type = type.to_a[0]
83
+ value.map{ |k, v| [ coerce(k, k_type), coerce(v, v_type) ] }.to_h
84
+ else
85
+ raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
86
+ end
87
+ end
88
+
89
+ def coerce_time(value)
90
+ case value
91
+ when Integer then Time.at(value)
92
+ when String then Time.parse(value)
93
+ else value
94
+ end
95
+ end
96
+
97
+ def coerce_other(value, type)
98
+ coercer[value.class].send("to_#{type.to_s.downcase}", value)
99
+ end
48
100
  end
49
101
 
50
102
  class_methods do
51
103
  protected
52
104
 
53
105
  def add_property(name, *args)
54
- options = args[-1].is_a?(Hash) ? args.pop : {}
55
- options[:type] = args[0] if args[0].is_a?(Class)
106
+ options = args[-1].is_a?(Hash) ? args[-1].slice(:default) : {}
107
+ options[:type] = args[0] if args[0]
56
108
  properties[name.to_s] = options
57
109
  create_accessors(name, options)
58
110
  self
@@ -65,13 +117,7 @@ module DataSteroid
65
117
  end
66
118
 
67
119
  define_method("#{name}=") do |value| # Define set method
68
- coerced_value =
69
- if options[:type] && !value.nil?
70
- coercer[value.class].send("to_#{options[:type].to_s.downcase}", value)
71
- else
72
- value
73
- end
74
- instance_variable_set("@#{name}", coerced_value)
120
+ instance_variable_set("@#{name}", coerce(value, options[:type]))
75
121
  end
76
122
  end
77
123
  end
@@ -1,5 +1,5 @@
1
1
  module DataSteroid
2
2
  # version string
3
3
  # @api public
4
- VERSION = '0.5.2'
4
+ VERSION = '0.5.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_steroid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Tomio