blazy 0.1.1 → 0.1.2

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.
@@ -1,41 +1,72 @@
1
1
  == Description
2
2
 
3
- Blazy read as be_lazy is a handy extension to active record models meant to be used in script/console
3
+ Blazy(be lazy) is a fluent extension to active record models to reduce number of key strokes in script/console.
4
4
 
5
- == Install
5
+ Currently works with rails 2.2.x and 2.3.x
6
6
 
7
- Install the gem with:
7
+ == Install
8
8
 
9
- sudo gem install blazy #Note: If you are using rvm to manage ruby versions don't use sudo
9
+ gem install blazy
10
10
 
11
11
  == Usage
12
12
 
13
13
  Go to script/console in your rails project and type
14
14
 
15
- require 'blazy'
15
+ require 'blazy'
16
16
 
17
17
  you are ready to go!!
18
18
 
19
+ == How does it help me?
20
+
21
+ Most of the operations we do in script/console while debugging a problem or looking for model values in database take a lot of key strokes. Blazy provides methods, aliases and a fluent interface to reduce the number of keystrokes to bare minimal.
22
+
19
23
  == Examples
24
+ Lets say you have a active record model called Project.
25
+
26
+ This is what being lazy means
27
+
28
+ Project.find(11) --> Project-11 or Project[11] # [id] is preferred
29
+ Project.all --> projects
30
+ Project.first --> project
31
+
32
+ When you want to see some subset of data
20
33
 
21
- If you have a model Project.
34
+ Project.all(:limit => 10) --> 10.projects
35
+ Project.first --> 1.project
22
36
 
23
- 1.project or 2.projects to list projects
37
+ Scopes generated for each column
24
38
 
25
- Project-11 to get project with id 11
39
+ Project.all(:conditions => {:name => 'blazy' }) --> Project.with_name('blazy') # you get autocomplete for this
26
40
 
27
- == Work in progress
41
+ And its fluency using named scope (more like rails 3 way)
28
42
 
29
- Searching a project as
43
+ Project.all(:conditions =>....) --> Project.where(..) or Project.with()
44
+ Project.all(:limit => 10) --> Project.limit(10)
45
+ Project.all(:order => 'name DESC') --> Project.order('name DESC')
46
+ Project.all(:select => 'name') --> Project.select('name')
47
+ Project.all(:conditions => ["name like '%lazy'"]) --> Project.where("name like '%lazy'") #can also use 'with' instead of 'where'
30
48
 
31
- Project.with_name('blazy') or 5.projects.with_name_like('%zy')
49
+ And the power comes by chaining them
32
50
 
33
- Chaining
51
+ 10.projects.with('priority < 2').order(:priority)
34
52
 
35
- 5.projects.with_priority(3..5).not_completed
53
+ Project.with_priority(1).limit 10
54
+
55
+ Lastly and actually the least some aliases. These are useful at the end of scope to inspect a model
56
+
57
+ Project.with_priority(1).f.name
58
+
59
+ Listing them --> f => first, l => last, a => all
60
+
61
+ == To do
62
+
63
+ models - to list all models in the project
64
+ relations - to show quick summary of a model's associations
36
65
 
37
66
  == Other useful gems in script/console
38
67
 
39
- Wirble
68
+ Wirble[http://github.com/blackwinter/wirble]
69
+
70
+ Hirb[http://github.com/cldwalker/hirb]
40
71
 
41
- Hirb
72
+ and add this[http://gist.github.com/609341] to your ~/.irbc
data/Rakefile CHANGED
@@ -13,9 +13,9 @@ task :default => ["spec"]
13
13
 
14
14
  spec = Gem::Specification.new do |s|
15
15
  s.name = "blazy"
16
- s.version = "0.1.1"
17
- s.summary = "Provides fluent extension to active record models"
18
- s.description = "Blazy read as be_lazy is a fluent extension to active record models and meant only to be used in script/console"
16
+ s.version = "0.1.2"
17
+ s.summary = "Provides fluent extensions and aliases to active record models"
18
+ s.description = "Blazy(be lazy) is a fluent extension to active record models to reduce number of key strokes in script/console"
19
19
  s.author = "Deepak N"
20
20
  s.email = "endeep123@gmail.com"
21
21
  s.homepage = "http://github.com/endeepak/blazy"
@@ -24,10 +24,10 @@ spec = Gem::Specification.new do |s|
24
24
  s.rdoc_options = %w(--main README.rdoc)
25
25
  s.files = %w(Rakefile README.rdoc) + Dir.glob("{spec,lib/**/*}")
26
26
  s.require_paths = ["lib"]
27
- s.add_development_dependency('rspec')
28
- s.add_development_dependency('activerecord')
29
- s.add_development_dependency('active_support')
30
- s.add_development_dependency('factory_girl')
27
+ s.add_development_dependency('rspec', '= 1.3.0')
28
+ s.add_development_dependency('activerecord', '= 2.3.5')
29
+ s.add_development_dependency('active_support', '= 2.3.5')
30
+ s.add_development_dependency('factory_girl', '= 1.2.4')
31
31
  end
32
32
 
33
33
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -62,3 +62,14 @@ desc 'Clear out RDoc and generated packages'
62
62
  task :clean => [:clobber_rdoc, :clobber_package] do
63
63
  rm "#{spec.name}.gemspec"
64
64
  end
65
+
66
+ namespace :db do
67
+ namespace :mysql do
68
+ desc 'Recreates test database in mysql server'
69
+ task :recreate do
70
+ `mysql -uroot -e "drop database if exists blazy_test"`
71
+ `mysql -uroot -e "create database if not exists blazy_test"`
72
+ `mysql -uroot -D blazy_test -e "show tables"`
73
+ end
74
+ end
75
+ end
@@ -2,7 +2,8 @@ require 'rubygems'
2
2
  require 'active_record'
3
3
  require 'active_support/inflector'
4
4
 
5
- require 'extensions/active_model'
5
+ require 'extensions/object'
6
6
  require 'extensions/active_record'
7
7
 
8
+ #HACKTAG:: Load all model from rails app model instead of lazy load - Testing this?
8
9
  Dir.glob(File.join(RAILS_ROOT,'app','models','**','*.rb')).each { |file| require_dependency file } if defined?(RAILS_ROOT)
@@ -1,3 +1,18 @@
1
- require File.dirname(__FILE__) + '/active_record/fixnum'
1
+ class ActiveRecord::Base
2
+ named_scope :with, lambda { |conditions| {:conditions => conditions } }
3
+ named_scope :limit, lambda { |limit| {:limit => limit} }
4
+ named_scope :order, lambda { |order| {:order => order} }
5
+ named_scope :select, lambda { |*columns| {:select => columns} }
2
6
 
7
+ class << self
8
+ {:where => :with, :- => :find, :[] => :find, :f => :first, :a => :all, :l => :last}.each do |new_name, method|
9
+ eval "alias #{new_name} #{method}"
10
+ end
11
+ end
12
+ end
13
+
14
+ Dir.glob(File.join(File.dirname(__FILE__),'active_record','*.rb')).each { |file| require file }
15
+ ActiveRecord::Base.send(:include, Blazy::Extensions::ActiveRecord::ScopeMerge)
16
+ ActiveRecord::Base.send(:include, Blazy::Extensions::ActiveRecord::Object)
3
17
  ActiveRecord::Base.send(:include, Blazy::Extensions::ActiveRecord::Fixnum)
18
+ ActiveRecord::Base.send(:include, Blazy::Extensions::ActiveRecord::ColumnScope)
@@ -0,0 +1,27 @@
1
+ module Blazy
2
+ module Extensions
3
+ module ActiveRecord
4
+ module ColumnScope
5
+ def self.included(klass)
6
+ klass.extend ClassMethods
7
+ klass.send(:subclasses).each {|subclass| subclass.add_column_scope unless subclass.abstract_class?}
8
+ end
9
+
10
+ module ClassMethods
11
+ def inherited(klass)
12
+ super
13
+ klass.add_column_scope
14
+ end
15
+
16
+ def add_column_scope
17
+ self.columns.each do |column|
18
+ self.named_scope "with_#{column.name}", lambda { |*params| {:conditions => { column.name => params } } }
19
+ end
20
+ rescue ::ActiveRecord::ActiveRecordError => e
21
+ puts "Failed to add column scope for #{self.name} : #{e.message}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -14,22 +14,18 @@ module Blazy
14
14
  end
15
15
 
16
16
  def add_extension_to_fixnum
17
- underscored_model_name = self.name.demodulize.underscore
18
- pluralized_underscored_model_name = underscored_model_name.pluralize
19
- definition = <<-METHOD_DEFINITION
20
- class ::Fixnum
21
- def #{underscored_model_name}
22
- record_to_find = self==1 ? :first : :all
23
- #{self}.find(record_to_find, :limit => self)
24
- end
25
- alias #{pluralized_underscored_model_name} #{underscored_model_name}
26
- end
27
- METHOD_DEFINITION
28
- eval(definition)
17
+ eval <<-METHOD
18
+ class ::Fixnum
19
+ def #{self.singular_name}
20
+ #{self}.limit(self)
21
+ end
22
+ alias #{self.plural_name} #{self.singular_name}
23
+ end
24
+ METHOD
29
25
  end
30
26
  end
31
27
  end
32
-
28
+
33
29
  end
34
30
  end
35
31
  end
@@ -0,0 +1,36 @@
1
+ module Blazy
2
+ module Extensions
3
+ module ActiveRecord
4
+ module Object
5
+ def self.included(klass)
6
+ klass.extend ClassMethods
7
+ klass.send(:subclasses).each {|subclass| subclass.add_extension_to_object}
8
+ end
9
+
10
+ module ClassMethods
11
+ def inherited(klass)
12
+ super
13
+ klass.add_extension_to_object
14
+ end
15
+
16
+ def add_extension_to_object
17
+ eval <<-METHOD
18
+ class ::Object
19
+ def #{self.singular_name}
20
+ #{self}.first
21
+ end
22
+ end
23
+ METHOD
24
+ eval <<-METHOD
25
+ class ::Object
26
+ def #{self.plural_name}
27
+ #{self}.all
28
+ end
29
+ end
30
+ METHOD
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ module Blazy
2
+ module Extensions
3
+ module ActiveRecord
4
+ module ScopeMerge
5
+ def self.included(klass)
6
+ klass.send(:subclasses).each {|subclass| subclass.scopes.reverse_merge!(klass.scopes) }
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ Dir.glob(File.join(File.dirname(__FILE__),'object','*.rb')).each { |file| require file }
2
+
3
+ Object.send(:include, Blazy::Extensions::Object::Name)
@@ -0,0 +1,15 @@
1
+ module Blazy
2
+ module Extensions
3
+ module Object
4
+ module Name
5
+ def singular_name
6
+ self.name.demodulize.underscore
7
+ end
8
+
9
+ def plural_name
10
+ self.singular_name.pluralize
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blazy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Deepak N
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-04 00:00:00 +05:30
18
+ date: 2010-10-11 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -24,12 +24,14 @@ dependencies:
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ">="
27
+ - - "="
28
28
  - !ruby/object:Gem::Version
29
- hash: 3
29
+ hash: 27
30
30
  segments:
31
+ - 1
32
+ - 3
31
33
  - 0
32
- version: "0"
34
+ version: 1.3.0
33
35
  type: :development
34
36
  version_requirements: *id001
35
37
  - !ruby/object:Gem::Dependency
@@ -38,12 +40,14 @@ dependencies:
38
40
  requirement: &id002 !ruby/object:Gem::Requirement
39
41
  none: false
40
42
  requirements:
41
- - - ">="
43
+ - - "="
42
44
  - !ruby/object:Gem::Version
43
- hash: 3
45
+ hash: 9
44
46
  segments:
45
- - 0
46
- version: "0"
47
+ - 2
48
+ - 3
49
+ - 5
50
+ version: 2.3.5
47
51
  type: :development
48
52
  version_requirements: *id002
49
53
  - !ruby/object:Gem::Dependency
@@ -52,12 +56,14 @@ dependencies:
52
56
  requirement: &id003 !ruby/object:Gem::Requirement
53
57
  none: false
54
58
  requirements:
55
- - - ">="
59
+ - - "="
56
60
  - !ruby/object:Gem::Version
57
- hash: 3
61
+ hash: 9
58
62
  segments:
59
- - 0
60
- version: "0"
63
+ - 2
64
+ - 3
65
+ - 5
66
+ version: 2.3.5
61
67
  type: :development
62
68
  version_requirements: *id003
63
69
  - !ruby/object:Gem::Dependency
@@ -66,15 +72,17 @@ dependencies:
66
72
  requirement: &id004 !ruby/object:Gem::Requirement
67
73
  none: false
68
74
  requirements:
69
- - - ">="
75
+ - - "="
70
76
  - !ruby/object:Gem::Version
71
- hash: 3
77
+ hash: 23
72
78
  segments:
73
- - 0
74
- version: "0"
79
+ - 1
80
+ - 2
81
+ - 4
82
+ version: 1.2.4
75
83
  type: :development
76
84
  version_requirements: *id004
77
- description: Blazy read as be_lazy is a fluent extension to active record models and meant only to be used in script/console
85
+ description: Blazy(be lazy) is a fluent extension to active record models to reduce number of key strokes in script/console
78
86
  email: endeep123@gmail.com
79
87
  executables: []
80
88
 
@@ -86,9 +94,13 @@ files:
86
94
  - Rakefile
87
95
  - README.rdoc
88
96
  - lib/blazy.rb
89
- - lib/extensions/active_model.rb
97
+ - lib/extensions/active_record/column_scope.rb
90
98
  - lib/extensions/active_record/fixnum.rb
99
+ - lib/extensions/active_record/object.rb
100
+ - lib/extensions/active_record/scope_merge.rb
91
101
  - lib/extensions/active_record.rb
102
+ - lib/extensions/object/name.rb
103
+ - lib/extensions/object.rb
92
104
  has_rdoc: true
93
105
  homepage: http://github.com/endeepak/blazy
94
106
  licenses: []
@@ -123,6 +135,6 @@ rubyforge_project:
123
135
  rubygems_version: 1.3.7
124
136
  signing_key:
125
137
  specification_version: 3
126
- summary: Provides fluent extension to active record models
138
+ summary: Provides fluent extensions and aliases to active record models
127
139
  test_files: []
128
140
 
@@ -1,15 +0,0 @@
1
- module Blazy
2
- module ActiveModel
3
- def self.included(klass)
4
- klass.extend ClassMethods
5
- end
6
-
7
- module ClassMethods
8
- def -(id)
9
- self.find(id)
10
- end
11
- end
12
- end
13
- end
14
-
15
- ActiveRecord::Base.send(:include, Blazy::ActiveModel)