immutable_attributes 1.1.4 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -4
- data/VERSION +1 -1
- data/immutable_attributes.gemspec +3 -6
- data/lib/immutable_attributes.rb +14 -9
- data/test/immutable_attributes_test.rb +6 -1
- metadata +9 -7
data/Rakefile
CHANGED
@@ -9,16 +9,17 @@ begin
|
|
9
9
|
gem.description = %Q{Allows specified attributes to be freely overwritten _until_ the record is saved for the first time}
|
10
10
|
gem.email = "gemcutter@jackcanty.com"
|
11
11
|
gem.homepage = "http://github.com/JackDanger/immutable_attributes"
|
12
|
-
gem.authors = ["Jack Danger Canty"]
|
12
|
+
gem.authors = ["Jack Danger Canty", "Terry Heath", "Nicholas Mertaugh"]
|
13
13
|
end
|
14
14
|
rescue LoadError
|
15
15
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
16
|
end
|
17
17
|
|
18
18
|
require 'rake/testtask'
|
19
|
-
|
20
|
-
|
19
|
+
Rake::TestTask.new(:test) do |test|
|
20
|
+
test.libs << '.'
|
21
|
+
test.pattern = 'test/*_test.rb'
|
22
|
+
test.verbose = true
|
21
23
|
end
|
22
|
-
|
23
24
|
task :default => :test
|
24
25
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{immutable_attributes}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Jack Danger Canty"]
|
12
|
-
s.date = %q{2011-
|
11
|
+
s.authors = ["Jack Danger Canty", "Terry Heath", "Nicholas Mertaugh"]
|
12
|
+
s.date = %q{2011-07-18}
|
13
13
|
s.description = %q{Allows specified attributes to be freely overwritten _until_ the record is saved for the first time}
|
14
14
|
s.email = %q{gemcutter@jackcanty.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -33,9 +33,6 @@ Gem::Specification.new do |s|
|
|
33
33
|
s.require_paths = ["lib"]
|
34
34
|
s.rubygems_version = %q{1.4.2}
|
35
35
|
s.summary = %q{Selected attributes are permanent once a record is created}
|
36
|
-
s.test_files = [
|
37
|
-
"test/immutable_attributes_test.rb"
|
38
|
-
]
|
39
36
|
|
40
37
|
if s.respond_to? :specification_version then
|
41
38
|
s.specification_version = 3
|
data/lib/immutable_attributes.rb
CHANGED
@@ -12,14 +12,19 @@ end
|
|
12
12
|
module ImmutableAttributes
|
13
13
|
VERSION = "1.0.3"
|
14
14
|
def attr_immutable(*args)
|
15
|
-
|
16
|
-
|
17
|
-
define_method("#{
|
18
|
-
new_record? || read_attribute(
|
19
|
-
write_attribute(
|
20
|
-
raise(ActiveRecord::ImmutableAttributeError, "#{
|
15
|
+
class_eval do
|
16
|
+
args.each do |attr|
|
17
|
+
define_method("#{attr}=") do |value|
|
18
|
+
new_record? || read_attribute(attr).nil? ?
|
19
|
+
write_attribute(attr, value) :
|
20
|
+
raise(ActiveRecord::ImmutableAttributeError, "#{attr} is immutable!")
|
21
21
|
end
|
22
22
|
end
|
23
|
+
# handle ActiveRecord::Base#[]=
|
24
|
+
define_method :[]= do |attr, value|
|
25
|
+
return write_attribute(attr, value) unless args.include?(attr.to_sym)
|
26
|
+
send "#{attr}=", value
|
27
|
+
end
|
23
28
|
end
|
24
29
|
end
|
25
30
|
|
@@ -29,10 +34,10 @@ module ImmutableAttributes
|
|
29
34
|
|
30
35
|
@immutables = attr_names
|
31
36
|
|
32
|
-
attr_names.each do |
|
37
|
+
attr_names.each do |attr|
|
33
38
|
class_eval do
|
34
|
-
define_method("original_#{
|
35
|
-
instance_variable_get("@original_#{
|
39
|
+
define_method("original_#{attr}") do
|
40
|
+
instance_variable_get("@original_#{attr}")
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -24,8 +24,13 @@ class ImmutableAttributesTest < Test::Unit::TestCase
|
|
24
24
|
assert Record.new(:name => 'record name')
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def test_immutable_attribute_cannot_be_changed_via_mass_setter
|
28
28
|
record = Record.create!(:name => 'record name')
|
29
29
|
assert_raises(ActiveRecord::ImmutableAttributeError) { record.update_attributes(:name => 'new name') }
|
30
30
|
end
|
31
|
+
|
32
|
+
def test_immutable_attribute_cannot_be_changed_via_bracket_setter
|
33
|
+
record = Record.create!(:name => 'record name')
|
34
|
+
assert_raises(ActiveRecord::ImmutableAttributeError) { record[:name] = 'new name' }
|
35
|
+
end
|
31
36
|
end
|
metadata
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immutable_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jack Danger Canty
|
14
|
+
- Terry Heath
|
15
|
+
- Nicholas Mertaugh
|
14
16
|
autorequire:
|
15
17
|
bindir: bin
|
16
18
|
cert_chain: []
|
17
19
|
|
18
|
-
date: 2011-
|
20
|
+
date: 2011-07-18 00:00:00 -07:00
|
19
21
|
default_executable:
|
20
22
|
dependencies: []
|
21
23
|
|
@@ -74,5 +76,5 @@ rubygems_version: 1.4.2
|
|
74
76
|
signing_key:
|
75
77
|
specification_version: 3
|
76
78
|
summary: Selected attributes are permanent once a record is created
|
77
|
-
test_files:
|
78
|
-
|
79
|
+
test_files: []
|
80
|
+
|