make-private 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.
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +20 -0
- data/README.md +88 -0
- data/Rakefile +24 -0
- data/db.yml +5 -0
- data/lib/make_private.rb +48 -0
- data/lib/make_private/active_record_persistence.rb +21 -0
- data/lib/make_private/version.rb +3 -0
- data/skratch.rb +15 -0
- data/spec/lib/make_private_spec.rb +77 -0
- data/spec/migrations/001_test_schema.rb +31 -0
- data/spec/models/bar.rb +2 -0
- data/spec/models/corge.rb +3 -0
- data/spec/models/foo.rb +16 -0
- data/spec/models/quux.rb +2 -0
- data/spec/models/qux.rb +2 -0
- data/spec/spec_helper.rb +31 -0
- metadata +196 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
guard 'bundler' do
|
2
|
+
watch('Gemfile')
|
3
|
+
watch(/^.+\.gemspec/)
|
4
|
+
end
|
5
|
+
|
6
|
+
guard 'spork' do
|
7
|
+
watch('Gemfile')
|
8
|
+
watch('Gemfile.lock')
|
9
|
+
watch(/^.+\.gemspec/)
|
10
|
+
watch('spec/spec_helper.rb')
|
11
|
+
end
|
12
|
+
|
13
|
+
guard 'rspec', :version => 2 do
|
14
|
+
watch(%r{^spec/models/.+\.rb}) {"spec"}
|
15
|
+
watch(%r{^spec/.+_spec\.rb$})
|
16
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
17
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
18
|
+
watch('spec/spec_helper.rb') { "spec" }
|
19
|
+
end
|
20
|
+
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# make-private
|
2
|
+
|
3
|
+
|
4
|
+
make-private is an ActiveRecord extension that will make all your active record properties private and give you control over what is private and what should be public.
|
5
|
+
|
6
|
+
# Why make-private
|
7
|
+
|
8
|
+
By default active record makes all properties and associations public which does make it faster to develop with but breaks encapsulation by default. make-private has been developed to bring back encapsulation to active record model.
|
9
|
+
|
10
|
+
|
11
|
+
## Status
|
12
|
+
_make-private_ is currently in development
|
13
|
+
|
14
|
+
## Example
|
15
|
+
|
16
|
+
To use make-private private you simply have to include the module in your model, simple.
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'make-private'
|
20
|
+
|
21
|
+
class BusinessModel < ActiveRecord::Base
|
22
|
+
#a_property: string
|
23
|
+
include MakePrivate
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
business_model = BusinessModel.new
|
28
|
+
business_model.a_property # This will throw a NoMethodError exception
|
29
|
+
```
|
30
|
+
|
31
|
+
Properties can be set as normal from within the model
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'make-private'
|
35
|
+
|
36
|
+
class BusinessModel < ActiveRecord::Base
|
37
|
+
#a_property: string
|
38
|
+
include MakePrivate
|
39
|
+
|
40
|
+
def do_something_useful parameter
|
41
|
+
##some logic
|
42
|
+
self.a_property = parameter
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
If you require to make a property publicly readable set the method to public
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'make-private'
|
53
|
+
|
54
|
+
class BusinessModel < ActiveRecord::Base
|
55
|
+
#a_property: string
|
56
|
+
include MakePrivate
|
57
|
+
|
58
|
+
public :a_property
|
59
|
+
end
|
60
|
+
|
61
|
+
business_model = BusinessModel.new
|
62
|
+
business_model.a_property # This will no longer throw a NoMethodError exception
|
63
|
+
business_model.a_property = "something interesting" # This will still throw a NoMethodError exception
|
64
|
+
```
|
65
|
+
|
66
|
+
Similarly if you really really really need to make the property setter public.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'make-private'
|
70
|
+
|
71
|
+
class BusinessModel < ActiveRecord::Base
|
72
|
+
#a_property: string
|
73
|
+
include MakePrivate
|
74
|
+
|
75
|
+
public :a_property, :a_property=
|
76
|
+
end
|
77
|
+
|
78
|
+
business_model = BusinessModel.new
|
79
|
+
business_model.a_property # This will no longer throw a NoMethodError exception
|
80
|
+
business_model.a_property = "something interesting" # This will not throw a NoMethodError exception
|
81
|
+
```
|
82
|
+
|
83
|
+
##Notes
|
84
|
+
|
85
|
+
Please send any questions, comments or feedback to pythonandchips{at}gmail.com.
|
86
|
+
|
87
|
+
Cheers
|
88
|
+
Colin
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
require 'active_record'
|
5
|
+
require 'yaml'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
ROOT = "#{File.dirname(__FILE__)}" unless defined?(ROOT)
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
desc "Migrate the database"
|
12
|
+
task(:migrate) do
|
13
|
+
db_config = YAML::load(File.open('db.yml'))['test']
|
14
|
+
ActiveRecord::Base.establish_connection(db_config)
|
15
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
16
|
+
migration_folder = ROOT << "/spec/migrations/"
|
17
|
+
puts migration_folder
|
18
|
+
migrations_directory = (migration_folder)
|
19
|
+
ActiveRecord::Migrator.migrate(migrations_directory)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec::Core::RakeTask.new('spec')
|
24
|
+
task :default => :spec
|
data/db.yml
ADDED
data/lib/make_private.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "make_private/version"
|
2
|
+
require "make_private/active_record_persistence"
|
3
|
+
|
4
|
+
module MakePrivate
|
5
|
+
def self.included(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
self.columns.each do |column|
|
8
|
+
self.instance_eval do
|
9
|
+
define_method(:"#{column.name}=") do |val|
|
10
|
+
write_attribute column.name.to_sym, val
|
11
|
+
end
|
12
|
+
|
13
|
+
define_method(:"#{column.name}") do
|
14
|
+
self[column.name.to_sym]
|
15
|
+
end
|
16
|
+
private :"#{column.name}=", column.name.to_sym
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
alias :__belongs_to_public :belongs_to
|
22
|
+
alias :__has_one_public :has_one
|
23
|
+
alias :__has_many_public :has_many
|
24
|
+
alias :__has_and_belongs_to_many_public :has_and_belongs_to_many
|
25
|
+
|
26
|
+
def belongs_to *args, &block
|
27
|
+
__belongs_to_public *args, &block
|
28
|
+
private args[0], :"#{args[0]}="
|
29
|
+
end
|
30
|
+
|
31
|
+
def has_one *args, &block
|
32
|
+
__has_one_public *args, &block
|
33
|
+
private args[0], :"#{args[0]}="
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_many *args, &block
|
37
|
+
__has_many_public *args, &block
|
38
|
+
private args[0], :"#{args[0]}="
|
39
|
+
end
|
40
|
+
|
41
|
+
def has_and_belongs_to_many *args, &block
|
42
|
+
__has_and_belongs_to_many_public *args, &block
|
43
|
+
private args[0], :"#{args[0]}="
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "active_record/persistence"
|
2
|
+
module ActiveRecord
|
3
|
+
module Persistence
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def create
|
8
|
+
attributes_values = arel_attributes_values(!id.nil?)
|
9
|
+
|
10
|
+
new_id = self.class.unscoped.insert attributes_values
|
11
|
+
if self.class.primary_key
|
12
|
+
id ||= new_id
|
13
|
+
self.send(:id=, id)
|
14
|
+
end
|
15
|
+
|
16
|
+
IdentityMap.add(self) if IdentityMap.enabled?
|
17
|
+
@new_record = false
|
18
|
+
id
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/skratch.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe MakePrivate do
|
4
|
+
|
5
|
+
context "a class that has a belongs to association" do
|
6
|
+
Given(:foo){Foo.new}
|
7
|
+
Then do
|
8
|
+
lambda {
|
9
|
+
foo.bar
|
10
|
+
}.should raise_error(NoMethodError)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
context "a class that has a has one association" do
|
14
|
+
Given(:foo){Foo.new}
|
15
|
+
Then do
|
16
|
+
lambda {
|
17
|
+
foo.qux
|
18
|
+
}.should raise_error(NoMethodError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
context "a class that has a has many association" do
|
22
|
+
Given(:foo){Foo.new}
|
23
|
+
Then do
|
24
|
+
lambda {
|
25
|
+
foo.quux
|
26
|
+
}.should raise_error(NoMethodError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
context "a class that has a has and belongs to may association" do
|
30
|
+
Given(:foo){Foo.new}
|
31
|
+
Then do
|
32
|
+
lambda {
|
33
|
+
foo.corge
|
34
|
+
}.should raise_error(NoMethodError)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "with properties" do
|
39
|
+
Then do
|
40
|
+
lambda {
|
41
|
+
foo = Foo.new
|
42
|
+
foo.a_prop = "blah"
|
43
|
+
}.should raise_error(NoMethodError)
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when setting property from public method" do
|
47
|
+
Given(:value){"hello"}
|
48
|
+
When(:foo) do
|
49
|
+
foo = Foo.new
|
50
|
+
foo.set_property(value)
|
51
|
+
foo
|
52
|
+
end
|
53
|
+
Then{foo.get_property.should eql value}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "should not affect other classes properties in project" do
|
58
|
+
Given(:qux){Qux.new}
|
59
|
+
Then do
|
60
|
+
lambda {
|
61
|
+
qux.a_prop = "hello"
|
62
|
+
}.should_not raise_error(NoMethodError)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "should allow record to be save" do
|
67
|
+
Given(:value){"hello"}
|
68
|
+
When(:foo) do
|
69
|
+
foo = Foo.new
|
70
|
+
foo.set_property(value)
|
71
|
+
foo.save
|
72
|
+
Foo.find(foo.send(:id))
|
73
|
+
end
|
74
|
+
Then{foo.get_property.should eql value}
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class TestSchema < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :foos do |t|
|
4
|
+
t.integer :bar_id
|
5
|
+
t.string :a_prop
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table :bars do |t|
|
9
|
+
t.string :a_prop
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :quxs do |t|
|
13
|
+
t.string :a_prop
|
14
|
+
t.integer :foo_id
|
15
|
+
end
|
16
|
+
|
17
|
+
create_table :quuxs do |t|
|
18
|
+
t.integer :foo_id
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table :corges do |t|
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :foos_corges do |t|
|
25
|
+
t.integer :foo_id
|
26
|
+
t.integer :corge_id
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/spec/models/bar.rb
ADDED
data/spec/models/foo.rb
ADDED
data/spec/models/quux.rb
ADDED
data/spec/models/qux.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'spork'
|
4
|
+
|
5
|
+
Spork.prefork do
|
6
|
+
require "active_record"
|
7
|
+
require "rspec"
|
8
|
+
require "rspec/given"
|
9
|
+
Dir.glob(File.expand_path(File.dirname(__FILE__)) + "/support/**/*.rb").each { |file| require file}
|
10
|
+
db_config = YAML::load(File.open('db.yml'))['test']
|
11
|
+
db_config["database"] = File.expand_path(File.dirname(__FILE__)) + '/../'+ db_config["database"]
|
12
|
+
ActiveRecord::Base.establish_connection(db_config)
|
13
|
+
|
14
|
+
|
15
|
+
RSpec.configuration do |config|
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
Spork.each_run do
|
21
|
+
require "make_private"
|
22
|
+
Dir.glob(File.expand_path(File.dirname(__FILE__)) + "/models/**/*.rb").each { |file| require file}
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: make-private
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Colin Gemmell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &73973650 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *73973650
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3-ruby
|
27
|
+
requirement: &73973440 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *73973440
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: guard
|
38
|
+
requirement: &73973230 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *73973230
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: guard-rspec
|
49
|
+
requirement: &73973020 !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: *73973020
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rb-fsevent
|
60
|
+
requirement: &73972810 !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: *73972810
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: spork
|
71
|
+
requirement: &73972560 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.9.0.rc
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *73972560
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: guard-spork
|
82
|
+
requirement: &73972350 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *73972350
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: guard-bundler
|
93
|
+
requirement: &73972120 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *73972120
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: rspec-given
|
104
|
+
requirement: &73971910 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *73971910
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rb-inotify
|
115
|
+
requirement: &73971700 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
type: :development
|
122
|
+
prerelease: false
|
123
|
+
version_requirements: *73971700
|
124
|
+
- !ruby/object:Gem::Dependency
|
125
|
+
name: libnotify
|
126
|
+
requirement: &73971490 !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: *73971490
|
135
|
+
- !ruby/object:Gem::Dependency
|
136
|
+
name: activerecord
|
137
|
+
requirement: &73971280 !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
type: :runtime
|
144
|
+
prerelease: false
|
145
|
+
version_requirements: *73971280
|
146
|
+
description: Make all active record properties private
|
147
|
+
email:
|
148
|
+
- pythonandchips@gmail.com
|
149
|
+
executables: []
|
150
|
+
extensions: []
|
151
|
+
extra_rdoc_files: []
|
152
|
+
files:
|
153
|
+
- .gitignore
|
154
|
+
- .rspec
|
155
|
+
- Gemfile
|
156
|
+
- Guardfile
|
157
|
+
- README.md
|
158
|
+
- Rakefile
|
159
|
+
- db.yml
|
160
|
+
- lib/make_private.rb
|
161
|
+
- lib/make_private/active_record_persistence.rb
|
162
|
+
- lib/make_private/version.rb
|
163
|
+
- skratch.rb
|
164
|
+
- spec/lib/make_private_spec.rb
|
165
|
+
- spec/migrations/001_test_schema.rb
|
166
|
+
- spec/models/bar.rb
|
167
|
+
- spec/models/corge.rb
|
168
|
+
- spec/models/foo.rb
|
169
|
+
- spec/models/quux.rb
|
170
|
+
- spec/models/qux.rb
|
171
|
+
- spec/spec_helper.rb
|
172
|
+
homepage: ''
|
173
|
+
licenses: []
|
174
|
+
post_install_message:
|
175
|
+
rdoc_options: []
|
176
|
+
require_paths:
|
177
|
+
- lib
|
178
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
179
|
+
none: false
|
180
|
+
requirements:
|
181
|
+
- - ! '>='
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
185
|
+
none: false
|
186
|
+
requirements:
|
187
|
+
- - ! '>='
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project: make_private
|
192
|
+
rubygems_version: 1.8.6
|
193
|
+
signing_key:
|
194
|
+
specification_version: 3
|
195
|
+
summary: Make all active record properties private
|
196
|
+
test_files: []
|