active_console 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2ea503f52056d41b16922192a50529d30c9e6f0
4
+ data.tar.gz: 4d255afc9e07ffd0a7b3949eb4be7f2c97eeebd8
5
+ SHA512:
6
+ metadata.gz: 7c761dfb302b353e905154333ffd5a0ea7ee648db6f38fba29bc09ae367b91f125707c3f11431d3b1ae806431d5277ad6cb407722e57b7aca29336b65d8d27fa
7
+ data.tar.gz: f7d0b924c3920785d9f1b085686e60f9734b74461d4b6bd6c72a8508f83ce78c2cf1dd525235fbd1615bba3ce65c9ae114409a1af4960e582f6cd367d6a83dc2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fubar.gemspec
4
+ gemspec
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activeconsole (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (3.2.16)
10
+ activesupport (= 3.2.16)
11
+ builder (~> 3.0.0)
12
+ activerecord (3.2.16)
13
+ activemodel (= 3.2.16)
14
+ activesupport (= 3.2.16)
15
+ arel (~> 3.0.2)
16
+ tzinfo (~> 0.3.29)
17
+ activesupport (3.2.16)
18
+ i18n (~> 0.6, >= 0.6.4)
19
+ multi_json (~> 1.0)
20
+ arel (3.0.3)
21
+ awesome_print (1.2.0)
22
+ builder (3.0.4)
23
+ coderay (1.1.0)
24
+ diff-lcs (1.2.5)
25
+ i18n (0.6.9)
26
+ method_source (0.8.2)
27
+ multi_json (1.8.2)
28
+ pry (0.9.12.4)
29
+ coderay (~> 1.0)
30
+ method_source (~> 0.8)
31
+ slop (~> 3.4)
32
+ rake (10.1.0)
33
+ rspec (2.14.1)
34
+ rspec-core (~> 2.14.0)
35
+ rspec-expectations (~> 2.14.0)
36
+ rspec-mocks (~> 2.14.0)
37
+ rspec-core (2.14.7)
38
+ rspec-expectations (2.14.4)
39
+ diff-lcs (>= 1.1.3, < 2.0)
40
+ rspec-mocks (2.14.4)
41
+ slop (3.4.7)
42
+ sqlite3 (1.3.8)
43
+ tzinfo (0.3.38)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ activeconsole!
50
+ activerecord (~> 3.0)
51
+ awesome_print
52
+ bundler (~> 1.3)
53
+ pry
54
+ rake
55
+ rspec (~> 2.3)
56
+ sqlite3
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 sameera207
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.
@@ -0,0 +1,92 @@
1
+ activeconsole
2
+ =============
3
+
4
+ Improvements and shortcuts for Rails console
5
+
6
+ Usage
7
+ =======
8
+ Agrep
9
+ -----
10
+
11
+ ```ruby
12
+ User.last.agrep "oh"
13
+ # {"name"=>"John"}
14
+
15
+ User.last.agrep "mail"
16
+ # {"email"=>"test@mailinator.com"}
17
+ ```
18
+
19
+ Aliases
20
+ -------
21
+
22
+ ```ruby
23
+ User.w(name: "test").o("name").li(1).f
24
+ # User.where(name: "test").order("name").limit(1).first
25
+ ```
26
+
27
+ Similar
28
+ -------
29
+
30
+ ```ruby
31
+ user = User.last
32
+ #<User id: 2, name: "John", email: "test@mailinator.com">
33
+ user.similar(:name, :email)
34
+ # User.where(name: "John", email: "test@mailinator.com")
35
+ ```
36
+
37
+ Association
38
+ ------------
39
+
40
+ This helper will show the Activerecord relations for a given AR model
41
+
42
+ ```
43
+ <Class>.rels
44
+ ```
45
+
46
+ will return a relation hash array
47
+
48
+ ```ruby
49
+ class User < ActiveRecord::Base
50
+ has_many :roles
51
+ end
52
+
53
+ class Role < ActiveRecord::Base
54
+ belongs_to :user
55
+ end
56
+
57
+ User.rels #=> [{:has_many=>:roles}]
58
+ ```
59
+
60
+
61
+ Display columns
62
+ ------------------
63
+
64
+ This helper will display all the columns of an AR object
65
+
66
+ ```
67
+ <Class>.cols
68
+ ```
69
+
70
+ with options
71
+
72
+ comma seperated list of column names
73
+
74
+ ```
75
+ <Class>.cols "name" #=> name is the column we are searching for
76
+ ```
77
+
78
+ ```
79
+ <Class>.cols name, emai #=> columns , which has the name 'name' and starting with ema
80
+ ```
81
+
82
+ ```ruby
83
+ # columns -> name
84
+ # -> age
85
+ class User < ActiveRecord::Base
86
+
87
+ end
88
+
89
+ User.cols #=> [{"name"=>"varchar(255)"}, {"age" => "INTEGER"}]
90
+ User.cols name #=> [{"name"=>"varchar(255)"}
91
+ User.cols name,ag #=> [{"name"=>"varchar(255)"}, {"age" => "INTEGER"}]
92
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_console/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_console"
8
+ spec.version = ActiveConsole::VERSION
9
+ spec.authors = ["morhekil","sameera207", "noma4i", "lis2", "halhenke", "gzzengwei"]
10
+ spec.email = [""]
11
+ spec.description = %q{Improvements and shortcuts for Rails console}
12
+ spec.summary = %q{Improvements and shortcuts for Rails console}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.3"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "activerecord", "~> 3.0"
26
+ spec.add_development_dependency "awesome_print"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,16 @@
1
+ require "awesome_print"
2
+ require "active_record"
3
+ require "active_console/version"
4
+ require "active_console/cols"
5
+ require "active_console/agrep"
6
+ require "active_console/rels"
7
+
8
+ Dir[File.dirname(__FILE__) + "/active_console/*.rb"].each { |file| require file }
9
+
10
+ module ActiveConsole
11
+ include Cols
12
+ include Agrep
13
+ include Alias
14
+ include Similar
15
+ include Rels
16
+ end
@@ -0,0 +1,22 @@
1
+ module Agrep
2
+ def agrep(pattern)
3
+ regex = Regexp.new pattern
4
+ agrep = self.dup.select { |k, v| k.to_s =~ regex || v.to_s =~ regex }
5
+ agrep.present? ? agrep : self
6
+ end
7
+ end
8
+
9
+ class Hash
10
+ include Agrep
11
+ end
12
+
13
+ module AgrepActiveRecord
14
+ extend ActiveSupport::Concern
15
+
16
+ def agrep(pattern)
17
+ self.attributes.agrep(pattern)
18
+ end
19
+
20
+ ActiveRecord::Base.send(:include, AgrepActiveRecord)
21
+ end
22
+
@@ -0,0 +1,13 @@
1
+ module Alias
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def w(args) ; where(args) ; end
6
+ def li(args) ; limit(args) ; end
7
+ def o(args) ; order(args) ; end
8
+ def l ; last ; end
9
+ def f ; first ; end
10
+ end
11
+
12
+ ActiveRecord::Base.send(:include, Alias)
13
+ end
@@ -0,0 +1,24 @@
1
+ module Cols
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def cols(list = "")
6
+
7
+ matching_cols = []
8
+ searching_cols = list.split(",")
9
+
10
+ columns.each do |col|
11
+ if searching_cols.count > 0
12
+ searching_cols.select do |c|
13
+ matching_cols << Hash[col.name, col.sql_type] if col.name.match(/^#{c.strip}/)
14
+ end
15
+ else
16
+ matching_cols << Hash[col.name, col.sql_type]
17
+ end
18
+ end
19
+ matching_cols
20
+ end
21
+ end
22
+
23
+ ActiveRecord::Base.send(:include, Cols)
24
+ end
@@ -0,0 +1,21 @@
1
+ module Rels
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+
6
+ def rels(*args)
7
+ rels = self.reflect_on_all_associations
8
+ display(rels)
9
+ end
10
+
11
+
12
+ private
13
+ def display(rels)
14
+ rels.map { |rel| Hash[rel.macro, rel.name].merge(rel.options) }
15
+ end
16
+
17
+ end
18
+
19
+ ActiveRecord::Base.send(:include, Rels)
20
+
21
+ end
@@ -0,0 +1,12 @@
1
+ module Similar
2
+ extend ActiveSupport::Concern
3
+
4
+ def similar(*columns)
5
+ attributes = {}
6
+ columns.each { |c| attributes[c] = self.send(c) }
7
+ self.class.where(attributes)
8
+ end
9
+
10
+ ActiveRecord::Base.send(:include, Similar)
11
+ end
12
+
@@ -0,0 +1,3 @@
1
+ module ActiveConsole
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe "agrep" do
4
+ context "hash" do
5
+ let(:hash) { { a: "b", b: "a", c: "c" } }
6
+
7
+ it "filter hash when value exists" do
8
+ expect(hash.agrep("a")).to eq({ a: "b", b: "a"})
9
+ end
10
+
11
+ it "all hash when value not exists" do
12
+ expect(hash.agrep("d")).to eq(hash)
13
+ end
14
+ end
15
+
16
+ context "active record" do
17
+ let(:user) { FactoryGirl.build(:user) }
18
+
19
+ it "filter user attributes" do
20
+ expect(user.agrep("oh")).to eq( {"name"=>"John"} )
21
+ end
22
+
23
+ it "filter user columns" do
24
+ expect(user.agrep("mail")).to eq( {"email" => "test@mailinator.com"} )
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Alias" do
4
+ subject { User }
5
+ it { subject.respond_to?(:w).should be_true }
6
+ it { subject.respond_to?(:l).should be_true }
7
+ it { subject.respond_to?(:f).should be_true }
8
+ it { subject.respond_to?(:o).should be_true }
9
+ it { subject.respond_to?(:li).should be_true }
10
+ end
@@ -0,0 +1,33 @@
1
+
2
+ require 'spec_helper'
3
+
4
+ describe "model columns" do
5
+
6
+ context "when passing no arguments" do
7
+
8
+ it "should give all the column names" do
9
+ expect(User.cols.count).to eq(3)
10
+ end
11
+
12
+ end
13
+
14
+ context "when passing arguments" do
15
+
16
+ it "should give one column" do
17
+ expect(User.cols("name").count).to eq(1)
18
+ end
19
+
20
+ it "should give one column" do
21
+ expect(User.cols("nam").count).to eq(1)
22
+ end
23
+
24
+ it "should give two columns" do
25
+ expect(User.cols("nam, em").count).to eq(2)
26
+ end
27
+
28
+ it "should give two column" do
29
+ expect(User.cols("name, id").count).to eq(2)
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ name "John"
4
+ email "test@mailinator.com"
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ load File.dirname(__FILE__) + "/support/ar_relations.rb"
4
+
5
+ describe "Rels" do
6
+
7
+ shared_examples "show relations" do |klass, message|
8
+ subject { klass }
9
+
10
+ it "should show the relationship" do
11
+ expect(subject.rels).to eq(message)
12
+ end
13
+ end
14
+
15
+
16
+ context "when there is no relation" do
17
+ it_behaves_like "show relations", NoRelation, []
18
+ end
19
+
20
+ context "when :has_many" do
21
+ it_behaves_like "show relations", User, [{:has_many => :roles}]
22
+ end
23
+
24
+ context "when :belongs_to" do
25
+ it_behaves_like "show relations", Role, [{:belongs_to => :user}]
26
+ end
27
+
28
+ context "when :has_one" do
29
+ it_behaves_like "show relations", Company, [{:has_one => :user}]
30
+ end
31
+
32
+ context "when has_and_belongs_to" do
33
+ it_behaves_like "show relations", Physician, [{:has_many=>:appointments}, {:has_many=>:patients, :through=>:appointments}]
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe "similar" do
4
+ let(:user) { FactoryGirl.create(:user) }
5
+ let!(:user2) { FactoryGirl.create(:user) }
6
+
7
+ it "return similar objects" do
8
+ puts user.inspect
9
+ expect(user.similar(:email, :name).size).to eq(2)
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_console'
2
+ require 'factory_girl'
3
+
4
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3",
5
+ database: File.dirname(__FILE__) + "/activeconsole/sqlite3")
6
+ load File.dirname(__FILE__) + "/support/schema.rb"
7
+ load File.dirname(__FILE__) + "/support/models.rb"
8
+
9
+ FactoryGirl.find_definitions
@@ -0,0 +1,32 @@
1
+ #this file has all the AR relation classes
2
+
3
+ class NoRelation < ActiveRecord::Base
4
+
5
+ end
6
+
7
+ class User < ActiveRecord::Base
8
+ has_many :roles
9
+ end
10
+
11
+ class Role < ActiveRecord::Base
12
+ belongs_to :user
13
+ end
14
+
15
+ class Company < ActiveRecord::Base
16
+ has_one :user
17
+ end
18
+
19
+ class Physician < ActiveRecord::Base
20
+ has_many :appointments
21
+ has_many :patients, through: :appointments
22
+ end
23
+
24
+ class Appointment < ActiveRecord::Base
25
+ belongs_to :physician
26
+ belongs_to :patient
27
+ end
28
+
29
+ class Patient < ActiveRecord::Base
30
+ has_many :appointments
31
+ has_many :physicians, through: :appointments
32
+ end
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ end
3
+
4
+
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, force: true do |t|
5
+ t.string :name
6
+ t.string :email
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_console
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - morhekil
8
+ - sameera207
9
+ - noma4i
10
+ - lis2
11
+ - halhenke
12
+ - gzzengwei
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+ date: 2014-01-20 00:00:00.000000000 Z
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: bundler
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: '1.3'
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '1.3'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: '2.3'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '2.3'
60
+ - !ruby/object:Gem::Dependency
61
+ name: sqlite3
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ - !ruby/object:Gem::Dependency
75
+ name: activerecord
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: '3.0'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ - !ruby/object:Gem::Dependency
89
+ name: awesome_print
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ type: :development
96
+ prerelease: false
97
+ version_requirements: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ - !ruby/object:Gem::Dependency
103
+ name: pry
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ description: Improvements and shortcuts for Rails console
117
+ email:
118
+ - ''
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - active_console.gemspec
129
+ - lib/active_console.rb
130
+ - lib/active_console/agrep.rb
131
+ - lib/active_console/alias.rb
132
+ - lib/active_console/cols.rb
133
+ - lib/active_console/rels.rb
134
+ - lib/active_console/similar.rb
135
+ - lib/active_console/version.rb
136
+ - spec/activeconsole/sqlite3
137
+ - spec/agrep_spec.rb
138
+ - spec/alias_spec.rb
139
+ - spec/cols_spec.rb
140
+ - spec/factories/users.rb
141
+ - spec/rels_spec.rb
142
+ - spec/similar_spec.rb
143
+ - spec/spec_helper.rb
144
+ - spec/support/ar_relations.rb
145
+ - spec/support/data.rb
146
+ - spec/support/models.rb
147
+ - spec/support/schema.rb
148
+ homepage: ''
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.0.6
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Improvements and shortcuts for Rails console
172
+ test_files:
173
+ - spec/activeconsole/sqlite3
174
+ - spec/agrep_spec.rb
175
+ - spec/alias_spec.rb
176
+ - spec/cols_spec.rb
177
+ - spec/factories/users.rb
178
+ - spec/rels_spec.rb
179
+ - spec/similar_spec.rb
180
+ - spec/spec_helper.rb
181
+ - spec/support/ar_relations.rb
182
+ - spec/support/data.rb
183
+ - spec/support/models.rb
184
+ - spec/support/schema.rb