json_on_rails 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 6d5cd9da9b9e15cf64d420716d697b9fdc34763d1dc4b2c5fa350bf3afdf3d6e
4
- data.tar.gz: 7d9e7647696a997b94f8ed3a916cd8817e070f4d4b13dc033a23757964db1b3c
2
+ SHA1:
3
+ metadata.gz: 2e69b316260dade8869a75fe7fe53189c13eb12f
4
+ data.tar.gz: 8ba6a935d5f718a6583fca4be7478838a4ef2254
5
5
  SHA512:
6
- metadata.gz: f6fdb0645ce6d729560a9ebd71f7b87c48d4e732b723effb291ce59a3cf3714d32c21334086ca847d54187938249769d87ad0321fe698e477816d8c063cc47da
7
- data.tar.gz: 93ca5c57db905354fcb8bfd6ea4b7f3dcdc85653193f28a80144c0b1edaca936426dd23b61c350bb610d7c4b031b03850a03f9de2fb2149afee73971892b844f
6
+ metadata.gz: 39465c73dc5ab6a33995910336add320e76b900cc5e1ba0e09c4ec2c20bb34a184d23fbeb738dc442088e15a7e4f21257d3fa4fffd42e585965f0bcf6845639c
7
+ data.tar.gz: 9dd50f5ecb082b355cc44c90e5c5691aac093e895c6b143f217513cc762f2edc1dca3178f04b56937606cb464a262ff545b6d42150037636e504d31b9275c6bd
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- [![Build Status][BS img]](https://travis-ci.org/saveriomiroddi/json_on_rails)
2
- [![Coverage Status][CS img]](https://coveralls.io/r/saveriomiroddi/json_on_rails)
1
+ [![Build Status](https://travis-ci.org/saveriomiroddi/json_on_rails)](https://travis-ci.org/saveriomiroddi/json_on_rails.svg?branch=master)
2
+ [![Coverage Status](https://coveralls.io/repos/github/saveriomiroddi/json_on_rails/badge.svg?branch=master)](https://coveralls.io/github/saveriomiroddi/json_on_rails?branch=master)
3
3
 
4
4
  # JSON on Rails
5
5
 
@@ -18,7 +18,7 @@ The inner working is simple, and uses the standard Rails (internal) APIs; this i
18
18
  Add the gem to the Gemfile of your rails project:
19
19
 
20
20
  ```ruby
21
- gem "json_on_rails", "~> 0.1.2"
21
+ gem "json_on_rails", "~> 0.2.0"
22
22
  ```
23
23
 
24
24
  and update the environment:
@@ -31,6 +31,13 @@ that's all!
31
31
 
32
32
  ## Usage/example
33
33
 
34
+ Change the `mysql` connection adapter to `mysql2_json`, in `config/database.yml`:
35
+
36
+ ```yaml
37
+ default: &default
38
+ adapter: mysql2_json
39
+ ```
40
+
34
41
  Create a table:
35
42
 
36
43
  ```
@@ -54,13 +61,10 @@ class CreateUsers < ActiveRecord::Migration
54
61
  end
55
62
  ```
56
63
 
57
- define the model:
64
+ define the model (rails will automatically pick up the data type):
58
65
 
59
66
  ```
60
- class User < ActiveRecord::Base
61
- attribute :extras, ActiveRecord::Type::Json.new
62
- end
63
-
67
+ class User < ActiveRecord::Base; end
64
68
  ```
65
69
 
66
70
  then (ab)use the new attribute!:
@@ -71,9 +75,22 @@ User.create!(login: "saverio", extras: {"uses" => ["mysql", "json"]})
71
75
  User.last.extras.fetch("uses") # => ["mysql", "json"]
72
76
  ```
73
77
 
78
+ The schema can be dumped as usual; json columns will be transparently included:
79
+
80
+ ```sh
81
+ $ rake db:schema:dump
82
+ $ cat db/schema.rb
83
+ ActiveRecord::Schema.define(version: 0) do
84
+
85
+ create_table "users", force: :cascade do |t|
86
+ t.json "extras"
87
+ end
88
+
89
+ end
90
+ ```
91
+
92
+ ## Caveat/further documentation
93
+
74
94
  Don't forget that JSON doesn't support symbols, therefore, they can be set, but are accessed/loaded as strings.
75
95
 
76
96
  Users are encouraged to have a look at the test suite ([here](spec/json_on_rails/json_attributes_spec.rb) and [here](spec/json_on_rails/arel_methods_spec.rb)) for an exhaustive view of the functionality.
77
-
78
- [BS img]: https://travis-ci.org/saveriomiroddi/json_on_rails.svg?branch=master
79
- [CS img]: https://coveralls.io/repos/saveriomiroddi/json_on_rails/badge.png?branch=master
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/connection_adapters/abstract/schema_definitions"
4
+
1
5
  module ActiveRecord
2
6
  # Unfortunately, the type shorthands are hardcoded, and the code itself is not encapsulated
3
7
  # in a method, so there's not much design freedom.
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/connection_adapters/mysql2_adapter"
4
+ require "active_record/type/json"
5
+
6
+ # ActiveRecord resources:
7
+ #
8
+ # lib/active_record/connection_adapters/mysql2_adapter.rb
9
+ # lib/active_record/connection_adapters/abstract_mysql_adapter.rb
10
+ # lib/active_record/schema_dumper.rb
11
+ #
12
+ # and a few other files.
13
+ #
14
+ module ActiveRecord
15
+ module ConnectionHandling
16
+ def mysql2_json_connection(config)
17
+ config = config.symbolize_keys
18
+
19
+ config[:username] = "root" if config[:username].nil?
20
+
21
+ if Mysql2::Client.const_defined? :FOUND_ROWS
22
+ config[:flags] = Mysql2::Client::FOUND_ROWS
23
+ end
24
+
25
+ client = Mysql2::Client.new(config)
26
+ options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
27
+ ConnectionAdapters::Mysql2JsonAdapter.new(client, logger, options, config)
28
+ rescue Mysql2::Error => error
29
+ if error.message.include?("Unknown database")
30
+ raise ActiveRecord::NoDatabaseError.new(error.message, error)
31
+ else
32
+ raise
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ module ActiveRecord
39
+ module ConnectionAdapters
40
+ class Mysql2JsonAdapter < Mysql2Adapter
41
+ ADAPTER_NAME = "Mysql2Json".freeze
42
+
43
+ def native_database_types
44
+ super.merge(json: {name: "json"})
45
+ end
46
+
47
+ protected
48
+
49
+ def initialize_type_map(m)
50
+ super
51
+
52
+ m.register_type %r{json}i, Type::Json.new
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Resources:
3
+ # ActiveRecord resources:
4
4
  #
5
- # gems/activerecord-4.<version>/lib/active_record/attributes.rb
6
- # gems/activerecord-4.<version>/lib/active_record/type/value.rb
7
- # gems/activerecord-4.<version>/lib/active_record/type/mutable.rb
5
+ # lib/active_record/attributes.rb
6
+ # lib/active_record/type/value.rb
7
+ # lib/active_record/type/mutable.rb
8
8
  #
9
9
  module ActiveRecord
10
10
  module Type
@@ -1,6 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "active_record/type/json"
4
- require_relative "active_record/connection_adapters/schema_definitions"
5
-
6
- module JsonOnRails; end
3
+ require_relative "active_record/connection_adapters/abstract/json_schema_definitions"
4
+ require_relative "active_record/connection_adapters/mysql2_json_adapter"
@@ -1,3 +1,3 @@
1
1
  module JsonOnRails
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Saverio Miroddi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-11 00:00:00.000000000 Z
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.3'
41
- - !ruby/object:Gem::Dependency
42
- name: coveralls
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 0.8.21
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 0.8.21
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -102,7 +88,8 @@ extra_rdoc_files: []
102
88
  files:
103
89
  - LICENSE
104
90
  - README.md
105
- - lib/active_record/connection_adapters/schema_definitions.rb
91
+ - lib/active_record/connection_adapters/abstract/json_schema_definitions.rb
92
+ - lib/active_record/connection_adapters/mysql2_json_adapter.rb
106
93
  - lib/active_record/type/json.rb
107
94
  - lib/json_on_rails.rb
108
95
  - lib/json_on_rails/version.rb
@@ -126,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
113
  version: '0'
127
114
  requirements: []
128
115
  rubyforge_project:
129
- rubygems_version: 2.7.4
116
+ rubygems_version: 2.6.11
130
117
  signing_key:
131
118
  specification_version: 4
132
119
  summary: MySQL JSON support for Rails 4