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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f3e46066738b802560e2c7aa03e2528d94c41e2
4
- data.tar.gz: f6cac4d1a5f0520c2135c4d35b467cfcd4d073fd
3
+ metadata.gz: ba81d9925b53112ad1428e426467520d2c6ae6d1
4
+ data.tar.gz: 7755762a4ed521c56fe6f30d7c204652aea9a788
5
5
  SHA512:
6
- metadata.gz: 1d08cc5459cf92c64e304eedacb47fd24be44ce75d5de1087f0299776600ea386f85d583dbc41ed2711268a1428e23fd3e49a73d0286d3382aaecfc4fa4b5783
7
- data.tar.gz: 9f76ec22f60c8fd9b8d9819cf37bbed259e1a4abce8a7d8ab1b6356f6423825d793aaab08353c7b98359e5d4b39f51b29753b42dd73c9c821f2ce90aa2900bdd
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
- def initialize(model, data_column, attrs = {})
65
- @virtus_model = self.class.build_virtus_model(model, data_column, attrs)
66
- end
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
- def type_cast_from_user(value)
69
- @virtus_model.new(value)
70
- end
88
+ klazz = Class.new() do
89
+ cattr_accessor :klazz_name, :virtus_model, :model, :data_column, :attrs
71
90
 
72
- def type_cast_from_database(value)
73
- @virtus_model.new(super(value))
74
- end
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
- def type_cast_for_database(value)
77
- if value.is_a?(@virtus_model)
78
- ::ActiveSupport::JSON.encode(value)
79
- else
80
- super
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
@@ -1,3 +1,3 @@
1
1
  module HasJsonAttributesOn
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  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
- value_type_instance: nil
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
- # set attribute for this class
112
- self._json_attributes_on[data_column][:value_type_instance] = self._json_attributes_on[data_column][:value_type_class].new(self, data_column, self._json_attributes_on[data_column][:types])
113
- attribute data_column, self._json_attributes_on[data_column][:value_type_instance]
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
- # set the delegators
116
- def_delegators data_column, *self._json_attributes_on[data_column][:delegators]
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
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-01-28 00:00:00.000000000 Z
11
+ date: 2016-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails