toar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa14a5e02e62c059352c817ae8975d06dfa2fec2
4
+ data.tar.gz: 58eedef19bdc3bc9d536d62695fe99083974350c
5
+ SHA512:
6
+ metadata.gz: 28822b112a902708ccb61cc1ca7563f28cc1fc0afce4e9e8e99175563c44733f773ddad8e4590a5129e2eba0d6ba1e5544720941fabad22ce9e14aec063f28c5
7
+ data.tar.gz: 5b3db2ae67ba9e894a02cac162dc952b8a66eb1f53ed38b27dd7b3770ab59b46f5a63f507fef5cc3f83a4864cf4f023e56d64d4d8b0d715cdcda6f4a822f0e33
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in toar.gemspec
4
+ gemspec
@@ -0,0 +1,74 @@
1
+ # Toar
2
+
3
+ # Deserialize ActiveRecord Model JSON (to_json -> to_ar)
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'toar'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install toar
19
+
20
+ ## Usage
21
+
22
+ ```
23
+ > user = User.create(name: 'A')
24
+ => #<User id: 1, name: "A", account_id: nil>
25
+
26
+ > user.posts.create(body: 'Post1')
27
+ => #<Post id: 1, user_id: 1, body: "Post1">
28
+
29
+ > user.posts.create(body: 'Post2')
30
+ => #<Post id: 2, user_id: 1, body: "Post2">
31
+
32
+ > user.create_account(name: 'AC')
33
+ => #<Account id: 1, name: "AC">
34
+
35
+ > json = user.to_json(include: [:posts, :account])
36
+ => "{\"id\":1,\"name\":\"A\",\"account_id\":1,\"posts\":[{\"id\":1,\"user_id\":1,\"body\":\"Post1\"},{\"id\":2,\"user_id\":1,\"body\":\"Post2\"}],\"account\":{\"id\":1,\"name\":\"AC\"}}"
37
+
38
+ > [Post, User, Account].each(&:destroy_all)
39
+ => [Post(id: integer, user_id: integer, body: text), User(id: integer, name: string, account_id: integer), Account(id: integer, name: string)]
40
+
41
+ > user_r = Toar.to_ar(User, json)
42
+ => #<User id: 1, name: "A", account_id: nil>
43
+
44
+ > user_r.posts
45
+ => #<ActiveRecord::Associations::CollectionProxy [#<Post id: 1, user_id: 1, body: "Post1">, #<Post id: 2, user_id: 1, body: "Post2">]>
46
+
47
+ > user_r.account
48
+ => #<Account id: 1, name: "AC">
49
+ ```
50
+
51
+ extend ActiveRecord Model
52
+
53
+ ```
54
+ class User
55
+ extend Toar::Ar
56
+ toar includes: [:posts, :account]
57
+ end
58
+
59
+ u = User.new
60
+ u.build_account
61
+ 3.times { u.posts.build }
62
+ u.save!
63
+ u.reload
64
+ json = u.toar_to_json
65
+ u_ = User.to_ar(json)
66
+ p u_.posts
67
+ p u_.account
68
+ ```
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "toar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,95 @@
1
+ require 'toar/version'
2
+ require 'json'
3
+
4
+ # Deserialize JSON to ActiveRecord Model with associations
5
+ module Toar
6
+
7
+ def self.to_ar(klass, val, base_object = nil)
8
+ d = val.class == Hash ? val.dup : JSON.parse(val)
9
+ obj = base_object || klass.new
10
+
11
+ hmt = klass.reflect_on_all_associations(:has_many).reduce({}) do |r, i|
12
+ r[i.options[:through]] = i if i.options[:through]
13
+ r
14
+ end
15
+
16
+ d.except(*obj.attributes.keys).each do |k, v|
17
+ as = klass.reflect_on_association(k)
18
+ next unless as
19
+
20
+ case as.macro
21
+ when :belongs_to
22
+ d.delete("#{k}_id")
23
+ to_ar(as.klass, v, obj.send("build_#{k}"))
24
+ obj.class_eval do
25
+ define_method("#{k}_id") { obj.send(k).id }
26
+ end
27
+ when :has_one
28
+ to_ar(as.klass, v, obj.send("build_#{k}"))
29
+ when :has_many
30
+ obj.send(k).proxy_association.target =
31
+ v.map { |i| to_ar(as.klass, i) }
32
+
33
+ as_th = hmt[k.to_sym]
34
+ if as_th
35
+ obj.send(as_th.name).proxy_association.target =
36
+ v.map { |i| to_ar(as_th.klass, i[as_th.source_reflection_name.to_s]) }
37
+ end
38
+ end
39
+ end
40
+ obj.assign_attributes(d.slice(*obj.attributes.keys))
41
+
42
+ obj.instance_eval do
43
+ # prevent save
44
+ def valid?(_context = nil)
45
+ false
46
+ end
47
+ end
48
+ obj
49
+ end
50
+
51
+ def self.convert_includes_option(*opt)
52
+ r = []
53
+ opt.flatten.each do |i|
54
+ case i
55
+ when Symbol
56
+ r << i
57
+ when Hash
58
+ i.each do |k, v|
59
+ r << { k => convert_includes_option(v) }
60
+ end
61
+ end
62
+ end
63
+ { include: r }
64
+ end
65
+
66
+ module Ar
67
+ def toar(opt = {})
68
+ @toar_opt ||= {}
69
+ @toar_opt[(opt[:name] || '').to_sym] = opt
70
+ include Instance
71
+ end
72
+
73
+ def to_ar(json)
74
+ Toar.to_ar(self, json)
75
+ end
76
+
77
+ def toar_opt(name = '')
78
+ @toar_opt[name.to_sym]
79
+ end
80
+
81
+ module Instance
82
+ def toar_as_json(name = '')
83
+ inc_opt = self.class.toar_opt(name)[:includes]
84
+ self.class
85
+ .includes(inc_opt)
86
+ .find_by(id: self.id)
87
+ .as_json(Toar.convert_includes_option(inc_opt))
88
+ end
89
+
90
+ def toar_to_json(name = '')
91
+ toar_as_json(name).to_json
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module Toar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'toar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "toar"
8
+ spec.version = Toar::VERSION
9
+ spec.authors = ["swdyh"]
10
+ spec.email = ["youhei@gmail.com"]
11
+
12
+ spec.summary = %q{Deserialize JSON to ActiveRecord Model with associations}
13
+ spec.description = %q{Deserialize JSON to ActiveRecord Model with associations}
14
+ spec.homepage = "https://github.com/swdyh/toar"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "activerecord", '>= 4.0.0'
33
+ spec.add_development_dependency "sqlite3", "~> 1.3"
34
+ spec.add_development_dependency "rubocop", "~> 0.43"
35
+ spec.add_development_dependency "bundler", "~> 1.13"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - swdyh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.43'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.43'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Deserialize JSON to ActiveRecord Model with associations
98
+ email:
99
+ - youhei@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - README.md
107
+ - Rakefile
108
+ - bin/console
109
+ - bin/setup
110
+ - lib/toar.rb
111
+ - lib/toar/version.rb
112
+ - toar.gemspec
113
+ homepage: https://github.com/swdyh/toar
114
+ licenses: []
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.5.1
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Deserialize JSON to ActiveRecord Model with associations
136
+ test_files: []