activerecord-raw-data 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +36 -0
- data/Rakefile +21 -0
- data/activerecord-raw-data.gemspec +24 -0
- data/lib/activerecord-raw-data/version.rb +5 -0
- data/lib/activerecord-raw-data.rb +27 -0
- data/spec/activerecord-raw-data_spec.rb +55 -0
- data/spec/database.rb +21 -0
- data/spec/spec_helper.rb +20 -0
- metadata +131 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Yuichi Tateno
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# ActiveRecord::RawData
|
2
|
+
|
3
|
+
Get ActiveRecord raw data. Hash, Array..
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
ActiveRecord Version ">= 3.0.0"
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'activerecord-raw-data'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install activerecord-raw-data
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
User.raw_data #=> [{"id" => 1, "name" => "alice"}, {"id" => 2, "name" => "bob"}, ...]
|
26
|
+
User.raw_rows #=> [[1, "alice"], [2, "bob"], ...]
|
27
|
+
# raw_values like ActiveRecord::Base.pluck. if your AR support, should use pluck.
|
28
|
+
User.select(:name).raw_values #=> ["alice", "bob, ...]
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require "bundler/gem_tasks"
|
6
|
+
require 'bundler'
|
7
|
+
|
8
|
+
begin
|
9
|
+
Bundler.setup(:default, :development)
|
10
|
+
rescue Bundler::BundlerError => e
|
11
|
+
$stderr.puts e.message
|
12
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
13
|
+
exit e.status_code
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'rspec/core'
|
17
|
+
require 'rspec/core/rake_task'
|
18
|
+
|
19
|
+
RSpec::Core::RakeTask.new(:spec)
|
20
|
+
|
21
|
+
task :default => :spec
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/activerecord-raw-data/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Yuichi Tateno"]
|
6
|
+
gem.email = ["hotchpotch@gmail.com"]
|
7
|
+
gem.description = %q{Get raw data from ActiveRecord}
|
8
|
+
gem.summary = %q{Get raw data from ActiveRecord}
|
9
|
+
gem.homepage = "https://github.com/hotchpotch/activerecord-raw-data"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "activerecord-raw-data"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveRecord::RawData::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
19
|
+
gem.add_dependency(%q<rake>, [">= 0.8.0"])
|
20
|
+
gem.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
21
|
+
gem.add_development_dependency(%q<pry>, [">= 0"])
|
22
|
+
gem.add_development_dependency(%q<sqlite3>, [">= 0"])
|
23
|
+
gem.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
24
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
require 'activerecord-raw-data/version'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module RawData
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def raw_data
|
12
|
+
connection.select_all scoped.to_sql
|
13
|
+
end
|
14
|
+
|
15
|
+
def raw_rows
|
16
|
+
connection.select_rows scoped.to_sql
|
17
|
+
end
|
18
|
+
|
19
|
+
def raw_values
|
20
|
+
connection.select_values scoped.to_sql
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveRecord::Base.send(:include, ActiveRecord::RawData)
|
27
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'activerecord-raw-data'
|
3
|
+
|
4
|
+
describe ActiveRecord::RawData do
|
5
|
+
let(:alice) { User.create(:name => 'alice', :mail => 'alice@example.com') }
|
6
|
+
let(:bob) { User.create(:name => 'bob', :mail => 'bob@example.com') }
|
7
|
+
let(:carol) { User.create(:name => 'carol', :mail => 'carol@example.com') }
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
alice
|
11
|
+
bob
|
12
|
+
carol
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should result raw data" do
|
16
|
+
User.raw_data.should eq [
|
17
|
+
{"id"=>1, "name"=>"alice", "mail"=>"alice@example.com"},
|
18
|
+
{"id"=>2, "name"=>"bob", "mail"=>"bob@example.com"},
|
19
|
+
{"id"=>3, "name"=>"carol", "mail"=>"carol@example.com"}
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should result raw data with select" do
|
24
|
+
User.select([:name, :mail]).raw_data.should eq [
|
25
|
+
{"name"=>"alice", "mail"=>"alice@example.com"},
|
26
|
+
{"name"=>"bob", "mail"=>"bob@example.com"},
|
27
|
+
{"name"=>"carol", "mail"=>"carol@example.com"}
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should result raw rows" do
|
32
|
+
User.raw_rows.should eq [
|
33
|
+
[1, "alice", "alice@example.com"],
|
34
|
+
[2, "bob", "bob@example.com"],
|
35
|
+
[3, "carol", "carol@example.com"]
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should result raw rows with select" do
|
40
|
+
User.select([:name, :mail]).raw_rows.should eq [
|
41
|
+
["alice", "alice@example.com"],
|
42
|
+
["bob", "bob@example.com"],
|
43
|
+
["carol", "carol@example.com"]
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
it "should result raw values" do
|
49
|
+
User.raw_values.should eq [1, 2, 3]
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should result raw values with select" do
|
53
|
+
User.select(:name).raw_values.should eq ['alice', 'bob', 'carol']
|
54
|
+
end
|
55
|
+
end
|
data/spec/database.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'database_cleaner'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
tmp_dir = Pathname.new(__FILE__).parent.parent.join('tmp')
|
6
|
+
tmp_dir.mkpath
|
7
|
+
::ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => tmp_dir.join("test.db").to_s}}
|
8
|
+
::ActiveRecord::Base.establish_connection('test')
|
9
|
+
|
10
|
+
class User < ::ActiveRecord::Base
|
11
|
+
class Migration < ::ActiveRecord::Migration
|
12
|
+
def self.up
|
13
|
+
unless User.table_exists?
|
14
|
+
create_table(:users) {|t|
|
15
|
+
t.string :name
|
16
|
+
t.string :mail
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.setup(:default, :development)
|
3
|
+
|
4
|
+
require 'database'
|
5
|
+
require 'activerecord-raw-data'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.mock_with :rspec
|
9
|
+
|
10
|
+
config.before(:suite) do
|
11
|
+
User::Migration.up
|
12
|
+
DatabaseCleaner.clean_with :deletion
|
13
|
+
DatabaseCleaner.strategy = :deletion
|
14
|
+
end
|
15
|
+
|
16
|
+
config.before(:each) do
|
17
|
+
DatabaseCleaner.clean
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-raw-data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yuichi Tateno
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &2191743620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2191743620
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &2191743100 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.8.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2191743100
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &2191742620 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.8.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2191742620
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: pry
|
49
|
+
requirement: &2191742140 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2191742140
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: sqlite3
|
60
|
+
requirement: &2191741660 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2191741660
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: &2191741180 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2191741180
|
80
|
+
description: Get raw data from ActiveRecord
|
81
|
+
email:
|
82
|
+
- hotchpotch@gmail.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- activerecord-raw-data.gemspec
|
93
|
+
- lib/activerecord-raw-data.rb
|
94
|
+
- lib/activerecord-raw-data/version.rb
|
95
|
+
- spec/activerecord-raw-data_spec.rb
|
96
|
+
- spec/database.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: https://github.com/hotchpotch/activerecord-raw-data
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: -504192017406598156
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
hash: -504192017406598156
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.17
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Get raw data from ActiveRecord
|
128
|
+
test_files:
|
129
|
+
- spec/activerecord-raw-data_spec.rb
|
130
|
+
- spec/database.rb
|
131
|
+
- spec/spec_helper.rb
|