has_json_attributes_on 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/has_json_attributes_on/type/json_type.rb +101 -15
- data/lib/has_json_attributes_on/version.rb +1 -1
- data/lib/has_json_attributes_on.rb +49 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba81d9925b53112ad1428e426467520d2c6ae6d1
|
4
|
+
data.tar.gz: 7755762a4ed521c56fe6f30d7c204652aea9a788
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b536608bf77cbedbdeb1080af3ffa17851d6e8af93a2fdee7b95cb649110c69809cb844fd329d43fec687a0e5d3990ce095fdf0ddf730cc2e66f33e8d1f2ede8
|
7
|
+
data.tar.gz: 438acfb2649fb64aaa71445c264c90d0f6c9ad183fd1b3fce971f002592ce0a368fc59f834ed9e0354de587747e25a65f8f8096c1ef7b813e66911d841621a5d
|
@@ -43,11 +43,28 @@ module HasJsonAttributesOn
|
|
43
43
|
|
44
44
|
klazz = Class.new do
|
45
45
|
include Virtus.model
|
46
|
+
cattr_accessor :type_name, :klazz_name
|
46
47
|
|
47
48
|
attrs.each do |attr_name, attr_type_key|
|
48
49
|
attribute attr_name.to_sym, AXIOMS[attr_type_key]
|
49
50
|
end
|
50
51
|
|
52
|
+
def self.type_name
|
53
|
+
@type_name
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.type_name=(v)
|
57
|
+
@type_name = v
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.type_name
|
61
|
+
@klazz_name
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.type_name=(v)
|
65
|
+
@klazz_name = v
|
66
|
+
end
|
67
|
+
|
51
68
|
def self.inspect
|
52
69
|
_attrs = attribute_set.instance_variable_get("@attributes").map{|x| [x.name, x.type.inspect].join(":")}.join(", ")
|
53
70
|
"<#{name} type => #{type_name} attribute_set => [#{_attrs}]>"
|
@@ -57,27 +74,96 @@ module HasJsonAttributesOn
|
|
57
74
|
self.inspect
|
58
75
|
end
|
59
76
|
end
|
77
|
+
klazz.klazz_name = klazz_name
|
78
|
+
klazz.type_name = type_name
|
60
79
|
return model.send(:const_set, klazz_name, klazz)
|
61
80
|
end
|
62
|
-
end
|
63
81
|
|
64
|
-
|
65
|
-
|
66
|
-
|
82
|
+
def build_serializer(model, data_column, attrs = {})
|
83
|
+
type_name = self.name
|
84
|
+
validate_virtus_model_attr_options!(model, attrs)
|
85
|
+
klazz_name = data_column.to_s.camelize + "DynamicTypeSerializer"
|
86
|
+
virtus_model = build_virtus_model(model, data_column, attrs)
|
67
87
|
|
68
|
-
|
69
|
-
|
70
|
-
end
|
88
|
+
klazz = Class.new() do
|
89
|
+
cattr_accessor :klazz_name, :virtus_model, :model, :data_column, :attrs
|
71
90
|
|
72
|
-
|
73
|
-
|
74
|
-
|
91
|
+
def self.is_my_type?(v)
|
92
|
+
v.is_a?(virtus_model)
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.attrs
|
96
|
+
@attrs
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.attrs=(v)
|
100
|
+
@attrs = v
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.data_column
|
104
|
+
@data_column
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.data_column=(v)
|
108
|
+
@data_column = v
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.model
|
112
|
+
@model
|
113
|
+
end
|
75
114
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
115
|
+
def self.model=(v)
|
116
|
+
@model = v
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.klazz_name
|
120
|
+
@klazz_name
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.klazz_name=(v)
|
124
|
+
@klazz_name = v
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.virtus_model=(v)
|
128
|
+
@virtus_model = v
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.virtus_model
|
132
|
+
@virtus_model
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.load(value)
|
136
|
+
begin
|
137
|
+
if (value.is_a?(String))
|
138
|
+
value = JSON.load(value)
|
139
|
+
end
|
140
|
+
return @virtus_model.new(value)
|
141
|
+
rescue Exception => e
|
142
|
+
Rails.logger.warn("#{name} for model #{model.name} threw an exception within self.load, #{e.class} => #{e.message}")
|
143
|
+
return @virtus_model.new
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.dump(value)
|
148
|
+
ActiveSupport::JSON.encode(value)
|
149
|
+
end
|
150
|
+
|
151
|
+
def self.inspect
|
152
|
+
"<#{name} \n virtus_model => #{self.virtus_model.inspect} \n model => #{self.model.inspect} \n data_column => #{self.data_column} \n attrs => #{self.attrs.inspect} >"
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.to_s
|
156
|
+
self.inspect
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
klazz.virtus_model = virtus_model
|
161
|
+
klazz.klazz_name = klazz_name
|
162
|
+
klazz.model = model
|
163
|
+
klazz.data_column = data_column
|
164
|
+
klazz.attrs = attrs
|
165
|
+
|
166
|
+
return model.send(:const_set, klazz_name, klazz)
|
81
167
|
end
|
82
168
|
end
|
83
169
|
end
|
@@ -11,7 +11,7 @@ module HasJsonAttributesOn
|
|
11
11
|
}
|
12
12
|
|
13
13
|
included do
|
14
|
-
extend Forwardable
|
14
|
+
# extend Forwardable
|
15
15
|
end
|
16
16
|
|
17
17
|
class_methods do
|
@@ -86,6 +86,7 @@ module HasJsonAttributesOn
|
|
86
86
|
|
87
87
|
def build_json_attributes(data_column, data_column_sql_type, accessors)
|
88
88
|
|
89
|
+
|
89
90
|
cattr_accessor :_json_attributes_on
|
90
91
|
self._json_attributes_on ||= {}.symbolize_keys
|
91
92
|
self._json_attributes_on[data_column] ||= {
|
@@ -93,10 +94,9 @@ module HasJsonAttributesOn
|
|
93
94
|
types: {},
|
94
95
|
default_values: {},
|
95
96
|
validations: {},
|
96
|
-
delegators: [],
|
97
97
|
data_column_sql_type: data_column_sql_type,
|
98
98
|
value_type_class: VALUE_TYPE_CLASSES[data_column_sql_type.to_s],
|
99
|
-
|
99
|
+
serializer_klazz: nil
|
100
100
|
}.symbolize_keys
|
101
101
|
|
102
102
|
self._json_attributes_on[data_column][:accessors].merge!(accessors)
|
@@ -105,15 +105,44 @@ module HasJsonAttributesOn
|
|
105
105
|
self._json_attributes_on[data_column][:validations][_accessor_attribute] = options[:validates] if options[:validates]
|
106
106
|
self._json_attributes_on[data_column][:default_values][_accessor_attribute] = options[:default]
|
107
107
|
self._json_attributes_on[data_column][:types][_accessor_attribute] = options[:type] || 'String'
|
108
|
-
self._json_attributes_on[data_column][:delegators] += [_accessor_attribute, "#{_accessor_attribute.to_s}=".to_sym]
|
109
108
|
end
|
110
109
|
|
111
|
-
|
112
|
-
|
113
|
-
|
110
|
+
self._json_attributes_on[data_column][:serializer_klazz] = self._json_attributes_on[data_column][:value_type_class].build_serializer(self, data_column, self._json_attributes_on[data_column][:types])
|
111
|
+
|
112
|
+
store data_column,
|
113
|
+
accessors: accessors.keys,
|
114
|
+
coder: self._json_attributes_on[data_column][:serializer_klazz]
|
115
|
+
|
116
|
+
|
117
|
+
define_method data_column.to_sym do
|
118
|
+
column_value = self[data_column.to_sym]
|
119
|
+
unless self._json_attributes_on[data_column][:serializer_klazz].is_my_type?(column_value)
|
120
|
+
column_value = self._json_attributes_on[data_column][:serializer_klazz].virtus_model.new(column_value)
|
121
|
+
end
|
122
|
+
column_value
|
123
|
+
end
|
124
|
+
|
125
|
+
define_method "#{data_column.to_s}=" do |value|
|
126
|
+
unless self._json_attributes_on[data_column][:serializer_klazz].is_my_type?(value)
|
127
|
+
value = self._json_attributes_on[data_column][:serializer_klazz].virtus_model.new(value)
|
128
|
+
end
|
129
|
+
raw_write_attribute(data_column, value.to_hash)
|
130
|
+
send("#{data_column.to_s}_will_change!")
|
131
|
+
end
|
132
|
+
|
133
|
+
accessors.keys.each do |_accessor_attribute|
|
134
|
+
define_method _accessor_attribute.to_sym do
|
135
|
+
column_value = send(data_column)
|
136
|
+
column_value.send(_accessor_attribute)
|
137
|
+
end
|
114
138
|
|
115
|
-
|
116
|
-
|
139
|
+
define_method "#{_accessor_attribute.to_s}=" do |value|
|
140
|
+
column_value = send(data_column)
|
141
|
+
column_value.send("#{_accessor_attribute.to_s}=", value)
|
142
|
+
send("#{data_column.to_s}=", column_value)
|
143
|
+
value
|
144
|
+
end
|
145
|
+
end
|
117
146
|
|
118
147
|
# set default values
|
119
148
|
self._json_attributes_on[data_column][:default_values].each do |_accessor_attribute, _default_value|
|
@@ -129,8 +158,19 @@ module HasJsonAttributesOn
|
|
129
158
|
self._json_attributes_on[data_column][:validations].each do |_accessor_attribute, _attribute_validations|
|
130
159
|
validates _accessor_attribute, _attribute_validations
|
131
160
|
end
|
161
|
+
|
162
|
+
self.after_initialize :build_json_attributes
|
132
163
|
end
|
133
164
|
end
|
165
|
+
|
166
|
+
def build_json_attributes
|
167
|
+
# ensure that we can call the method
|
168
|
+
self._json_attributes_on.each_pair do |data_column, options|
|
169
|
+
send(data_column);
|
170
|
+
end
|
171
|
+
return true
|
172
|
+
end
|
173
|
+
|
134
174
|
end
|
135
175
|
|
136
176
|
ActiveRecord::Base.send(:include, HasJsonAttributesOn)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_json_attributes_on
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wiseallie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|