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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b12c1abafaaf601c42dc878d0a7b941b60a26f1f
4
- data.tar.gz: 02e1613de3fb1cf3b03c9a3ebabe49aae5aed317
3
+ metadata.gz: 4f3e46066738b802560e2c7aa03e2528d94c41e2
4
+ data.tar.gz: f6cac4d1a5f0520c2135c4d35b467cfcd4d073fd
5
5
  SHA512:
6
- metadata.gz: b8b6a277b613a0e935abe8478c27260a2d57476a8864ef6662e45308e996f3ab6ff061b6b9ba6dd28b6d87386fbf4023c95940c8890d7922b7f2c318b32eb17d
7
- data.tar.gz: 93aad3d412cdc226064e95a201a405485914a074dbb5b077c1a0d5dc9c5fc56d835fe0ed78bb41c9322c09d33e616a7650a48b72563178c310540d17a85b0e9c
6
+ metadata.gz: 1d08cc5459cf92c64e304eedacb47fd24be44ce75d5de1087f0299776600ea386f85d583dbc41ed2711268a1428e23fd3e49a73d0286d3382aaecfc4fa4b5783
7
+ data.tar.gz: 9f76ec22f60c8fd9b8d9819cf37bbed259e1a4abce8a7d8ab1b6356f6423825d793aaab08353c7b98359e5d4b39f51b29753b42dd73c9c821f2ce90aa2900bdd
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2016 wiseallie
1
+ Copyright (c) 2016 wiseallie <wiseallie@gmail.com>
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -1,3 +1,3 @@
1
1
  module HasJsonAttributesOn
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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
- unless columns.map(&:name).include?(data_column.to_s)
39
- raise ArgumentError, "Model: #{name}#has_json_attributes_on, data_column: #{data_column} does not exist as a column in database"
40
- end
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
- data_column_sql_type = columns.detect{|x| x.name.to_s == data_column.to_s}.sql_type.to_s
43
- unless data_column_sql_type.in?(VALUE_TYPE_CLASSES.keys)
44
- raise ArgumentError, "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}"
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].constantize,
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.3
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-07 00:00:00.000000000 Z
11
+ date: 2016-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails