ggoodale-attr_private 0.1.0
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/CHANGES +1 -0
- data/README.rdoc +22 -0
- data/Rakefile +29 -0
- data/lib/attr_private.rb +60 -0
- data/spec/attr_private/attr_private_spec.rb +1 -0
- data/spec/environment_fixture_setup.rb +7 -0
- data/spec/spec.opts +14 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/spec_suite.rb +13 -0
- metadata +65 -0
data/CHANGES
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0 - Initial release
|
data/README.rdoc
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
= attr_private
|
2
|
+
|
3
|
+
attr_private is a way to hide fields on an ActiveRecord model such
|
4
|
+
that they are inaccessible to external callers. fields declared
|
5
|
+
private are protected from most access methods provided by AR.
|
6
|
+
Attempts to access a private field will raise a NoMethodError as
|
7
|
+
though the field did not exist.
|
8
|
+
|
9
|
+
== Using attr_private
|
10
|
+
=== app/models/my_model.rb
|
11
|
+
class MyModel < ActiveRecord::Base
|
12
|
+
attr_private :my_field, :some_other_field
|
13
|
+
end
|
14
|
+
|
15
|
+
== Limitations
|
16
|
+
|
17
|
+
Currently, attr_private doesn't prevent reading via the attributes hash.
|
18
|
+
|
19
|
+
|
20
|
+
== Special Thanks To
|
21
|
+
* reQall for letting me hack in Ruby
|
22
|
+
* Evan Light for pushing me to make this a gem (my first!)
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "rake"
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
|
7
|
+
desc "Runs the Rspec suite"
|
8
|
+
task(:default) do
|
9
|
+
run_suite
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Runs the Rspec suite"
|
13
|
+
task(:spec) do
|
14
|
+
run_suite
|
15
|
+
end
|
16
|
+
|
17
|
+
def run_suite
|
18
|
+
dir = File.dirname(__FILE__)
|
19
|
+
system("ruby #{dir}/spec/spec_suite.rb --options #{dir}/spec/spec.opts") || raise("Spec Suite failed")
|
20
|
+
end
|
21
|
+
|
22
|
+
spec = eval(File.read("#{File.dirname(__FILE__)}/attr_private.gemspec"))
|
23
|
+
PKG_NAME = spec.name
|
24
|
+
PKG_VERSION = spec.version
|
25
|
+
|
26
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
27
|
+
pkg.need_zip = true
|
28
|
+
pkg.need_tar = true
|
29
|
+
end
|
data/lib/attr_private.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
|
3
|
+
module AttrPrivate
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(SingletonMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
# This provides the ability to mark a field on a record as private, preventing access from
|
9
|
+
# outside an instance of the class.
|
10
|
+
#
|
11
|
+
module SingletonMethods
|
12
|
+
|
13
|
+
def attr_private(*fields)
|
14
|
+
|
15
|
+
write_inheritable_attribute :private_fields, fields
|
16
|
+
class_inheritable_reader :private_fields
|
17
|
+
|
18
|
+
extend ClassMethods
|
19
|
+
|
20
|
+
fields.each {|field|
|
21
|
+
column = connection.columns(table_name, "AttrPrivate").detect{|column| column.name.to_sym == field.to_sym}
|
22
|
+
raise ActiveRecord::ActiveRecordError, "Unknown field #{field}" if column.nil?
|
23
|
+
|
24
|
+
puts "protecting #{field}"
|
25
|
+
# prevent bulk assignment
|
26
|
+
attr_protected field
|
27
|
+
|
28
|
+
# prevent use of the standard accessors
|
29
|
+
module_eval <<-"end_eval", __FILE__, __LINE__
|
30
|
+
def #{field}
|
31
|
+
raise ::NoMethodError, "undefined method \`#{field}' for \#{self}"
|
32
|
+
end
|
33
|
+
def #{field}=(x)
|
34
|
+
raise ::NoMethodError, "undefined method \`#{field}=' for \#{self}"
|
35
|
+
end
|
36
|
+
def #{field}?
|
37
|
+
raise ::NoMethodError, "undefined method \`#{field}?' for \#{self}"
|
38
|
+
end
|
39
|
+
end_eval
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
def method_missing_with_parameter_exclusion(name, *args)
|
45
|
+
private_fields.each do |field|
|
46
|
+
raise ::NoMethodError, "undefined method `#{name}' for #{self}" if /_#{field}_|_#{field}$/.match(name.to_s)
|
47
|
+
end
|
48
|
+
method_missing_without_parameter_exclusion(name, args)
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :method_missing_without_parameter_exclusion, :method_missing
|
52
|
+
alias_method :method_missing, :method_missing_with_parameter_exclusion
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class ActiveRecord::Base
|
59
|
+
include AttrPrivate
|
60
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
data/spec/spec_suite.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ggoodale-attr_private
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Grant Goodale
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: attr_private provides a way of making fields on an ActiveRecord model inaccessible to users of that model.
|
17
|
+
email: grant@reqall.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- CHANGES
|
25
|
+
files:
|
26
|
+
- CHANGES
|
27
|
+
- Rakefile
|
28
|
+
- README.rdoc
|
29
|
+
- lib/attr_private.rb
|
30
|
+
- spec/environment_fixture_setup.rb
|
31
|
+
- spec/spec.opts
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
- spec/spec_suite.rb
|
34
|
+
- spec/attr_private/attr_private_spec.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://reqall.com
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --main
|
40
|
+
- README.rdoc
|
41
|
+
- --inline-source
|
42
|
+
- --line-numbers
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: attr_private provides a way of making fields on an ActiveRecord model inaccessible to users of that model.
|
64
|
+
test_files:
|
65
|
+
- spec/attr_private/attr_private_spec.rb
|