has_json_attributes_on 0.0.1 → 0.0.3

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: 2d72de8b5fe0ad71e4e6ccc7ba63a7a708916516
4
- data.tar.gz: e4ffdff6fc8e9886df9a8e80b7b9c00bc43994a7
3
+ metadata.gz: b12c1abafaaf601c42dc878d0a7b941b60a26f1f
4
+ data.tar.gz: 02e1613de3fb1cf3b03c9a3ebabe49aae5aed317
5
5
  SHA512:
6
- metadata.gz: f9052ae90b3e913af9aafa1689c28fe7dc1af741228e48310c6b87f6f35f73784011d0e61c36aa7dd1bd6e8d3f4788030a993c596926f1c30cffcbf7902279bf
7
- data.tar.gz: 3e148c926b5c3f564229e5bf6f6990271ac62cda03648617789056a89376f74a34393deff32395cad9d28efe1f64e81b9ac2b014dd24921869e836e23e1e5e32
6
+ metadata.gz: b8b6a277b613a0e935abe8478c27260a2d57476a8864ef6662e45308e996f3ab6ff061b6b9ba6dd28b6d87386fbf4023c95940c8890d7922b7f2c318b32eb17d
7
+ data.tar.gz: 93aad3d412cdc226064e95a201a405485914a074dbb5b077c1a0d5dc9c5fc56d835fe0ed78bb41c9322c09d33e616a7650a48b72563178c310540d17a85b0e9c
@@ -0,0 +1,9 @@
1
+ require 'active_record/connection_adapters/postgresql/oid/json'
2
+ require_relative 'json_type'
3
+ module HasJsonAttributesOn
4
+ module Type
5
+ class Json < ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json
6
+ include JsonType
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,85 @@
1
+ require 'virtus'
2
+ require 'default_value_for'
3
+ module HasJsonAttributesOn
4
+ extend ActiveSupport::Concern
5
+
6
+ AXIOMS = {
7
+ 'Boolean' => Axiom::Types::Boolean,
8
+ 'String' => Axiom::Types::String,
9
+ 'Decimal' => Axiom::Types::Decimal,
10
+ 'Date' => Axiom::Types::Date,
11
+ 'DateTime'=> Axiom::Types::DateTime,
12
+ 'Time' => Axiom::Types::Time,
13
+ 'Float' => Axiom::Types::Float,
14
+ 'Integer' => Axiom::Types::Integer,
15
+ 'Object' => Axiom::Types::Object,
16
+ 'Array' => Axiom::Types::Array,
17
+ 'Set' => Axiom::Types::Set,
18
+ 'Hash' => Axiom::Types::Hash
19
+ }
20
+
21
+ module Type
22
+ module JsonType
23
+ extend ActiveSupport::Concern
24
+
25
+ included do
26
+ attr_accessor :virtus_model
27
+ end
28
+
29
+ class_methods do
30
+
31
+ def validate_virtus_model_attr_options!(model, attrs)
32
+ raise "Model: #{model}, attributes must be a Hash of key:attribute_name and value:attribute_type" unless attrs.is_a?(Hash)
33
+ attrs.each do |k,v|
34
+ raise "Model: #{model}, key:#{k} should be a valid hash key" if k.to_s.include?(" ");
35
+ raise "Model: #{model}, value for key:#{k} should be one of: #{AXIOMS.keys}" unless v.in?(AXIOMS.keys)
36
+ end
37
+ end
38
+
39
+ def build_virtus_model(model, data_column, attrs = {})
40
+ type_name = self.name
41
+ validate_virtus_model_attr_options!(model, attrs)
42
+ klazz_name = data_column.to_s.camelize + "DynamicType"
43
+
44
+ klazz = Class.new do
45
+ include Virtus.model
46
+
47
+ attrs.each do |attr_name, attr_type_key|
48
+ attribute attr_name.to_sym, AXIOMS[attr_type_key]
49
+ end
50
+
51
+ def self.inspect
52
+ _attrs = attribute_set.instance_variable_get("@attributes").map{|x| [x.name, x.type.inspect].join(":")}.join(", ")
53
+ "<#{name} type => #{type_name} attribute_set => [#{_attrs}]>"
54
+ end
55
+
56
+ def self.to_s
57
+ self.inspect
58
+ end
59
+ end
60
+ return model.send(:const_set, klazz_name, klazz)
61
+ end
62
+ end
63
+
64
+ def initialize(model, data_column, attrs = {})
65
+ @virtus_model = self.class.build_virtus_model(model, data_column, attrs)
66
+ end
67
+
68
+ def type_cast_from_user(value)
69
+ @virtus_model.new(value)
70
+ end
71
+
72
+ def type_cast_from_database(value)
73
+ @virtus_model.new(super(value))
74
+ end
75
+
76
+ def type_cast_for_database(value)
77
+ if value.is_a?(@virtus_model)
78
+ ::ActiveSupport::JSON.encode(value)
79
+ else
80
+ super
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record/connection_adapters/postgresql/oid/jsonb'
2
+ require_relative 'json_type'
3
+ module HasJsonAttributesOn
4
+ module Type
5
+ class Jsonb < ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb
6
+ include JsonType
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module HasJsonAttributesOn
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,104 +1,15 @@
1
1
  require 'rails'
2
- require 'active_record/connection_adapters/postgresql/oid/json'
3
- require 'active_record/connection_adapters/postgresql/oid/jsonb'
4
- require 'virtus'
5
- require 'default_value_for'
2
+ require_relative 'has_json_attributes_on/type/json'
3
+ require_relative 'has_json_attributes_on/type/jsonb'
6
4
 
7
5
  module HasJsonAttributesOn
8
6
  extend ActiveSupport::Concern
9
7
 
10
- AXIOMS = {
11
- 'Boolean' => Axiom::Types::Boolean,
12
- 'String' => Axiom::Types::String,
13
- 'Decimal' => Axiom::Types::Decimal,
14
- 'Date' => Axiom::Types::Date,
15
- 'DateTime'=> Axiom::Types::DateTime,
16
- 'Time' => Axiom::Types::Time,
17
- 'Float' => Axiom::Types::Float,
18
- 'Integer' => Axiom::Types::Integer,
19
- 'Object' => Axiom::Types::Object,
20
- 'Array' => Axiom::Types::Array,
21
- 'Set' => Axiom::Types::Set,
22
- 'Hash' => Axiom::Types::Hash
23
- }
24
-
25
- SUPPORTED_DB_SQL_TYPES = %w(json jsonb)
26
8
  VALUE_TYPE_CLASSES = {
27
- 'json' => 'HasJsonAttributesOn::Type::Json',
28
- 'jsonb' => 'HasJsonAttributesOn::Type::Jsonb'
9
+ 'json' => HasJsonAttributesOn::Type::Json,
10
+ 'jsonb' => HasJsonAttributesOn::Type::Jsonb
29
11
  }
30
12
 
31
- module Type
32
- module JsonType
33
- extend ActiveSupport::Concern
34
-
35
- included do
36
- attr_accessor :virtus_model
37
- end
38
-
39
- class_methods do
40
-
41
- def validate_virtus_model_attr_options!(model, attrs)
42
- raise "Model: #{model}, attributes must be a Hash of key:attribute_name and value:attribute_type" unless attrs.is_a?(Hash)
43
- attrs.each do |k,v|
44
- raise "Model: #{model}, key:#{k} should be a valid hash key" if k.to_s.include?(" ");
45
- raise "Model: #{model}, value for key:#{k} should be one of: #{AXIOMS.keys}" unless v.in?(AXIOMS.keys)
46
- end
47
- end
48
-
49
- def build_virtus_model(model, data_column, attrs = {})
50
- validate_virtus_model_attr_options!(model, attrs)
51
- klazz_name = data_column.to_s.camelize + "DynamicType"
52
-
53
- klazz = Class.new do
54
- include Virtus.model
55
-
56
- attrs.each do |attr_name, attr_type_key|
57
- attribute attr_name.to_sym, AXIOMS[attr_type_key]
58
- end
59
-
60
- def self.inspect
61
- _attrs = attribute_set.instance_variable_get("@attributes").map{|x| [x.name, x.type.inspect].join(":")}.join(", ")
62
- "<#{name} type =>HasJsonAttributesOn::Type::JsonType attribute_set => [#{_attrs}]>"
63
- end
64
-
65
- def self.to_s
66
- self.inspect
67
- end
68
- end
69
-
70
- return model.send(:const_set, klazz_name, klazz)
71
- end
72
- end
73
-
74
- def initialize(model, data_column, attrs = {})
75
- @virtus_model = self.class.build_virtus_model(model, data_column, attrs)
76
- end
77
-
78
- def type_cast_from_user(value)
79
- @virtus_model.new(value)
80
- end
81
-
82
- def type_cast_from_database(value)
83
- @virtus_model.new(super(value))
84
- end
85
-
86
- def type_cast_for_database(value)
87
- if value.is_a?(@virtus_model)
88
- ::ActiveSupport::JSON.encode(value)
89
- else
90
- super
91
- end
92
- end
93
- end
94
- class Jsonb < ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb
95
- include JsonType
96
- end
97
- class Json < ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json
98
- include JsonType
99
- end
100
- end
101
-
102
13
  included do
103
14
  extend Forwardable
104
15
  end
@@ -128,9 +39,9 @@ module HasJsonAttributesOn
128
39
  raise ArgumentError, "Model: #{name}#has_json_attributes_on, data_column: #{data_column} does not exist as a column in database"
129
40
  end
130
41
 
131
- data_column_sql_type = columns.detect{|x| x.name.to_s == data_column.to_s}.sql_type
132
- unless data_column_sql_type.in?(SUPPORTED_DB_SQL_TYPES)
133
- raise ArgumentError, "Model: #{name}#has_json_attributes_on, data_column: #{data_column} is of sql type: #{data_column_sql_type}, supported types are:#{SUPPORTED_DB_SQL_TYPES}"
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}"
134
45
  end
135
46
  return [data_column, data_column_sql_type]
136
47
  end
@@ -163,7 +74,7 @@ module HasJsonAttributesOn
163
74
  validations: {},
164
75
  delegators: [],
165
76
  data_column_sql_type: data_column_sql_type,
166
- value_type_class: VALUE_TYPE_CLASSES[data_column_sql_type].constantize,
77
+ value_type_class: VALUE_TYPE_CLASSES[data_column_sql_type.to_s].constantize,
167
78
  value_type_instance: nil
168
79
  }.symbolize_keys
169
80
 
@@ -186,7 +97,8 @@ module HasJsonAttributesOn
186
97
  # set default values
187
98
  self._json_attributes_on[data_column][:default_values].each do |_accessor_attribute, _default_value|
188
99
  if _default_value.is_a?(Proc)
189
- default_value_for(_accessor_attribute, &_default_value)
100
+ # pass the context to self in the proc
101
+ default_value_for(_accessor_attribute, &_default_value)
190
102
  else
191
103
  default_value_for _accessor_attribute, _default_value
192
104
  end
@@ -196,7 +108,6 @@ module HasJsonAttributesOn
196
108
  self._json_attributes_on[data_column][:validations].each do |_accessor_attribute, _attribute_validations|
197
109
  validates _accessor_attribute, _attribute_validations
198
110
  end
199
-
200
111
  end
201
112
  end
202
113
  end
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.1
4
+ version: 0.0.3
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-06 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -76,6 +76,9 @@ files:
76
76
  - MIT-LICENSE
77
77
  - Rakefile
78
78
  - lib/has_json_attributes_on.rb
79
+ - lib/has_json_attributes_on/type/json.rb
80
+ - lib/has_json_attributes_on/type/json_type.rb
81
+ - lib/has_json_attributes_on/type/jsonb.rb
79
82
  - lib/has_json_attributes_on/version.rb
80
83
  - lib/tasks/has_json_attributes_on_tasks.rake
81
84
  homepage: https://github.com/wiseallie/has_json_attributes_on