activerecord_converter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 484336f3f3bd3acb42ce5a9ea88edf487ffa6fa978f8a239224605ce2f9171fb
4
+ data.tar.gz: 28dc05c79f075cfbdd040c6e9eccd136596fdbe9b48f75563859675e383cb896
5
+ SHA512:
6
+ metadata.gz: ba5990bde2d5e4cc74e26d54f1dc26aed00828d41c9550680d7d7a8dd0604a7e7ae950fcf67d1aed6fe779b4a86094b3811eb1aa21cfe3b882d4cff0c0161a62
7
+ data.tar.gz: e938963b4225967f470fa5d15aed9a362840a5df7f0ea2e0b9b335c945fc9e57f24bcb17a34d64f68ec69952944e9a405202ca1cb0b1b2118edb4c58dc88b8d6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # ActiverecordConverter
2
+
3
+ convert activerecord to just ary hash
4
+
5
+ ## Usage
6
+
7
+ ActiverecordConverter.to_ary_h(activerecord)
8
+
9
+ ```
10
+ record =
11
+ Animal.joins(:animal_type)
12
+ .joins(:countory)
13
+ .select("animals.id",
14
+ "animals.name",
15
+ "animals.weight",
16
+ "animals.height",
17
+ "animals.body",
18
+ "animals.hair",
19
+ "animals.created_at",
20
+ "animals.updated_at",
21
+ "animal_types.id as animal_type_id",
22
+ "countories.id as countory_id")
23
+ .order("animals.id")
24
+
25
+ ary_h = ActiverecordConverter.to_ary_h(record)
26
+
27
+ puts ary_h
28
+ =>
29
+ {"id"=>1, "name"=>"動物太郎_0", "weight"=>23, "height"=>44, "body"=>"small", "hair"=>"none", "created_at"=>Mon, 01 Apr 2019 00:00:00.000000000 UTC +00:00, "updated_at"=>Mon, 01 Apr 2019 05:10:00.000000000 UTC +00:00, "animal_type_id"=>1, "countory_id"=>1}
30
+ {"id"=>2, "name"=>"動物太郎_1", "weight"=>21, "height"=>33, "body"=>"middle", "hair"=>"short", "created_at"=>Tue, 02 Apr 2019 00:00:00.000000000 UTC +00:00, "updated_at"=>Tue, 02 Apr 2019 05:10:00.000000000 UTC +00:00, "animal_type_id"=>2, "countory_id"=>2}
31
+
32
+ ```
33
+
34
+ ## Installation
35
+ Add this line to your application's Gemfile:
36
+
37
+ ```ruby
38
+ gem 'activerecord_converter'
39
+ ```
40
+
41
+ And then execute:
42
+ ```bash
43
+ $ bundle
44
+ ```
45
+
46
+ Or install it yourself as:
47
+ ```bash
48
+ $ gem install activerecord_converter
49
+ ```
50
+
51
+ ## Contributing
52
+ Contribution directions go here.
53
+
54
+ ## License
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,27 @@
1
+ require "activerecord_converter/version"
2
+ require "activerecord_converter/railtie"
3
+
4
+ module ActiverecordConverter
5
+ def self.to_ary_h active_records
6
+ query_text = active_records.to_sql()
7
+
8
+ end_select_query_idx = 7
9
+ start_from_query_idx = ( /FROM `/ =~ query_text ) - 2
10
+
11
+ select_column_names =
12
+ query_text[end_select_query_idx..(start_from_query_idx)].split(",")
13
+ .map{|m| m.strip }
14
+
15
+ key_column_names =
16
+ select_column_names.map do |m|
17
+ if m.include?(" as ")
18
+ end_as_query_idx = ( / as / =~ m ) + 4
19
+ m[(end_as_query_idx)..]
20
+ else
21
+ m.split(".")[1].gsub("`", "")
22
+ end
23
+ end
24
+
25
+ active_records.pluck(*select_column_names).map { |m| key_column_names.zip(m).to_h }
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module ActiverecordConverter
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ActiverecordConverter
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activerecord_converter do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kashiwara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ description: convert activerecord to just ary hash
28
+ email:
29
+ - tamatebako0205@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/activerecord_converter.rb
38
+ - lib/activerecord_converter/railtie.rb
39
+ - lib/activerecord_converter/version.rb
40
+ - lib/tasks/activerecord_converter_tasks.rake
41
+ homepage: https://github.com/Kashiwara0205/activerecord_converter
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.3.1
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: convert activerecord to just ary hash
64
+ test_files: []