has_json_attributes_on 0.0.3 → 0.0.4
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/MIT-LICENSE +1 -1
- data/lib/has_json_attributes_on/version.rb +1 -1
- data/lib/has_json_attributes_on.rb +31 -10
- 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: 4f3e46066738b802560e2c7aa03e2528d94c41e2
|
4
|
+
data.tar.gz: f6cac4d1a5f0520c2135c4d35b467cfcd4d073fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d08cc5459cf92c64e304eedacb47fd24be44ce75d5de1087f0299776600ea386f85d583dbc41ed2711268a1428e23fd3e49a73d0286d3382aaecfc4fa4b5783
|
7
|
+
data.tar.gz: 9f76ec22f60c8fd9b8d9819cf37bbed259e1a4abce8a7d8ab1b6356f6423825d793aaab08353c7b98359e5d4b39f51b29753b42dd73c9c821f2ce90aa2900bdd
|
data/MIT-LICENSE
CHANGED
@@ -25,7 +25,22 @@ module HasJsonAttributesOn
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
def table_and_data_column_exist?(data_column, _table_name = self.table_name)
|
29
|
+
# check if we have database connection and the table columns
|
30
|
+
begin
|
31
|
+
raise "database table: #{self.table_name} does not exist" unless ActiveRecord::Base.connection.table_exists? _table_name.to_s
|
32
|
+
# try and access columns
|
33
|
+
raise "column: #{data_column} of database table #{self.table_name} does not exist" unless columns.map(&:name).include?(data_column.to_s)
|
34
|
+
return true
|
35
|
+
rescue Exception => e
|
36
|
+
puts "HasJsonAttributesOn => Model: #{name}#has_json_attributes_on, #{e.message}, skipping..."
|
37
|
+
puts "\n\n\nPLEASE RUN ALL MIGRATIONS FIRST BEFORE RUNNING ANYTHING ELSE!!!\n\n\n"
|
38
|
+
return false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
28
42
|
def has_json_attributes_on(data_column = :data, accessors = {})
|
43
|
+
return unless table_and_data_column_exist?(data_column, self.table_name)
|
29
44
|
validated_data_column, data_column_sql_type = validate_json_attributes_data_column!(data_column)
|
30
45
|
validated_accessors = validate_dynamic_accessors!(accessors)
|
31
46
|
build_json_attributes(validated_data_column, data_column_sql_type, validated_accessors)
|
@@ -35,36 +50,42 @@ module HasJsonAttributesOn
|
|
35
50
|
|
36
51
|
def validate_json_attributes_data_column!(data_column)
|
37
52
|
data_column = data_column.to_sym
|
38
|
-
|
39
|
-
|
40
|
-
|
53
|
+
begin
|
54
|
+
unless columns.map(&:name).include?(data_column.to_s)
|
55
|
+
raise ArgumentError, "HasJsonAttributesOn => Model: #{name}#has_json_attributes_on, data_column: #{data_column} does not exist as a column in database"
|
56
|
+
end
|
41
57
|
|
42
|
-
|
43
|
-
|
44
|
-
|
58
|
+
data_column_sql_type = columns.detect{|x| x.name.to_s == data_column.to_s}.sql_type.to_s
|
59
|
+
unless data_column_sql_type.in?(VALUE_TYPE_CLASSES.keys)
|
60
|
+
raise ArgumentError, "HasJsonAttributesOn => Model: #{name}#has_json_attributes_on, data_column: #{data_column} is of sql type: #{data_column_sql_type}, supported types are:#{VALUE_TYPE_CLASSES.keys}"
|
61
|
+
end
|
62
|
+
rescue Exception => e
|
63
|
+
puts e
|
64
|
+
return [data_column, 'json']
|
45
65
|
end
|
46
66
|
return [data_column, data_column_sql_type]
|
47
67
|
end
|
48
68
|
|
49
69
|
|
50
70
|
def validate_dynamic_accessors!(accessors)
|
51
|
-
raise ArgumentError, "Model: #{name}#has_json_attributes_on, accessors must be a hash" unless accessors.is_a?(Hash)
|
71
|
+
raise ArgumentError, "HasJsonAttributesOn => Model: #{name}#has_json_attributes_on, accessors must be a hash" unless accessors.is_a?(Hash)
|
52
72
|
accessors = accessors.symbolize_keys
|
53
73
|
accessors.each do |k,v|
|
54
74
|
if columns.map(&:name).include?(k.to_s)
|
55
|
-
raise ArgumentError, "Model: #{name}#has_json_attributes_on, accessor key:#{k} has already been defined as the model column"
|
75
|
+
raise ArgumentError, "HasJsonAttributesOn => Model: #{name}#has_json_attributes_on, accessor key:#{k} has already been defined as the model column"
|
56
76
|
end
|
57
77
|
|
58
78
|
begin
|
59
79
|
v.assert_valid_keys(:type, :validates, :default)
|
60
80
|
rescue Exception => e
|
61
|
-
raise ArgumentError, "Model: #{name}#has_json_attributes_on, accessor key:#{k}, #{e.message}"
|
81
|
+
raise ArgumentError, "HasJsonAttributesOn => Model: #{name}#has_json_attributes_on, accessor key:#{k}, #{e.message}"
|
62
82
|
end
|
63
83
|
end
|
64
84
|
return accessors
|
65
85
|
end
|
66
86
|
|
67
87
|
def build_json_attributes(data_column, data_column_sql_type, accessors)
|
88
|
+
|
68
89
|
cattr_accessor :_json_attributes_on
|
69
90
|
self._json_attributes_on ||= {}.symbolize_keys
|
70
91
|
self._json_attributes_on[data_column] ||= {
|
@@ -74,7 +95,7 @@ module HasJsonAttributesOn
|
|
74
95
|
validations: {},
|
75
96
|
delegators: [],
|
76
97
|
data_column_sql_type: data_column_sql_type,
|
77
|
-
value_type_class: VALUE_TYPE_CLASSES[data_column_sql_type.to_s]
|
98
|
+
value_type_class: VALUE_TYPE_CLASSES[data_column_sql_type.to_s],
|
78
99
|
value_type_instance: nil
|
79
100
|
}.symbolize_keys
|
80
101
|
|
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.4
|
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-
|
11
|
+
date: 2016-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|