rails_metadata 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rails_metadata.rb +11 -11
- data/lib/rails_metadata/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 084ee18887c21e25caac6e07614d1fe9196748b4
|
4
|
+
data.tar.gz: db5578b6f8f54823e44b89d30eaf02c54eb8cfb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a72c957d8bce64a33c0db8edf02ce9d6b4e58485d46370beec5864d08bcc3f871bea3fd7435dffd273edda2c8a22ff5af01d7c242783b0090682ca5117e33b48
|
7
|
+
data.tar.gz: cef7076850762436e4664cb94931c3b600fcb93afd66d9426eac72a44a18a63fb6d1ad9448eadf1b63fcd5f1a05f696c8874a9493c32d92f596dce5dad623778
|
data/lib/rails_metadata.rb
CHANGED
@@ -11,19 +11,19 @@ module RailsMetadata
|
|
11
11
|
self.metadata = {} unless self.metadata.present?
|
12
12
|
self.metadata[name]
|
13
13
|
end
|
14
|
+
typecast = case type
|
15
|
+
when nil then val
|
16
|
+
when :boolean then lambda { |v| v.is_a?(String) ? v.empty? : !!v }
|
17
|
+
when :string then lambda { |v| v.to_s }
|
18
|
+
when :integer then lambda { |v| v.to_i }
|
19
|
+
when :float then lambda { |v| v.to_f }
|
20
|
+
when :array then lambda { |v| v.to_a }
|
21
|
+
when :hash then lambda { |v| v.to_hash }
|
22
|
+
else raise ArgumentError, "Invalid type(:boolean, :string, :integer, :float, :array, :hash)"
|
23
|
+
end
|
14
24
|
define_method(name.to_s + '=') do |val|
|
15
|
-
val = case type
|
16
|
-
when nil then val
|
17
|
-
when :boolean then !(val.nil? || val.empty? || val)
|
18
|
-
when :string then String(val)
|
19
|
-
when :integer then val.to_i
|
20
|
-
when :float then val.to_f
|
21
|
-
when :array then Array(val)
|
22
|
-
when :hash then Hash(val)
|
23
|
-
else raise ArgumentError, "Invalid type(:boolean, :string, :integer, :float, :array, :hash)"
|
24
|
-
end
|
25
25
|
self.metadata = {} unless self.metadata.present?
|
26
|
-
self.metadata.merge!(name => val)
|
26
|
+
self.metadata.merge!(name => typecast.call(val))
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|