in_place_relation 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/in_place_relation.gemspec +24 -0
- data/lib/in_place_relation/version.rb +3 -0
- data/lib/in_place_relation.rb +59 -0
- metadata +9 -5
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "in_place_relation/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "in_place_relation"
|
7
|
+
s.version = InPlaceRelation::VERSION
|
8
|
+
s.authors = ["Marek Janukowicz/Starware"]
|
9
|
+
s.email = ["marek@janukowicz.net"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A relation that can be changed in place}
|
12
|
+
s.description = %q{ActiveRecord::Relation wrapper that can be modified/combined in place}
|
13
|
+
|
14
|
+
#s.rubyforge_project = "in_place_relation"
|
15
|
+
|
16
|
+
s.files = `hg manifest`.split("\n")
|
17
|
+
s.test_files = `hg manifest | grep ^test`.split("\n")
|
18
|
+
s.executables = `hg manifest | grep ^bin`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "activerecord", ">= 3"
|
24
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "in_place_relation/version"
|
2
|
+
|
3
|
+
# This module provides an in-place wrapper for ActiveRecord::Relation. So
|
4
|
+
# instead of doing this:
|
5
|
+
# rel = SomeModel.where( ... )
|
6
|
+
# rel = rel.joins().where() if x
|
7
|
+
# rel = rel.select() unless y
|
8
|
+
# rel.all
|
9
|
+
#
|
10
|
+
# you can do this
|
11
|
+
# rel = SomeModel.where( ... ).in_place
|
12
|
+
# rel.joins!().where!() if x
|
13
|
+
# rel.select!() unless y
|
14
|
+
# rel.all
|
15
|
+
module InPlaceRelation
|
16
|
+
|
17
|
+
module Methods
|
18
|
+
|
19
|
+
def in_place
|
20
|
+
return Wrapper.new( self )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Wrapper
|
25
|
+
|
26
|
+
attr_reader :relation
|
27
|
+
|
28
|
+
METHODS_TO_WRAP = ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS + ActiveRecord::Relation::SINGLE_VALUE_METHODS
|
29
|
+
|
30
|
+
METHODS_TO_WRAP.each do |meth|
|
31
|
+
define_method( meth ) do |*args|
|
32
|
+
return @relation.send( meth, *args )
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method( "#{meth}!" ) do |*args|
|
36
|
+
@relation = @relation.send( meth, *args )
|
37
|
+
return self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize( relation )
|
42
|
+
@relation = relation
|
43
|
+
end
|
44
|
+
|
45
|
+
def inspect
|
46
|
+
return "#{self.class}:#{object_id}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_missing( name, *args, &block )
|
50
|
+
@relation.send( name, *args, &block )
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveRecord::Associations::CollectionProxy.send( :include, InPlaceRelation::Methods )
|
58
|
+
ActiveRecord::Relation.send( :include, InPlaceRelation::Methods )
|
59
|
+
ActiveRecord::Base.send( :extend, InPlaceRelation::Methods )
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: in_place_relation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marek Janukowicz/Starware
|
@@ -41,8 +41,12 @@ extensions: []
|
|
41
41
|
|
42
42
|
extra_rdoc_files: []
|
43
43
|
|
44
|
-
files:
|
45
|
-
|
44
|
+
files:
|
45
|
+
- Gemfile
|
46
|
+
- Rakefile
|
47
|
+
- in_place_relation.gemspec
|
48
|
+
- lib/in_place_relation.rb
|
49
|
+
- lib/in_place_relation/version.rb
|
46
50
|
has_rdoc: true
|
47
51
|
homepage: ""
|
48
52
|
licenses: []
|