JackDanger-immutable_attributes 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt ADDED
@@ -0,0 +1,10 @@
1
+ Manifest.txt
2
+ README
3
+ Rakefile
4
+ immutable_attributes.gemspec
5
+ init.rb
6
+ install.rb
7
+ lib/immutable_attributes.rb
8
+ rails/init.rb
9
+ test/immutable_attributes_test.rb
10
+ uninstall.rb
data/README ADDED
@@ -0,0 +1,26 @@
1
+
2
+
3
+ ImmutableAttributes
4
+ ===================
5
+
6
+ When you want to prevent certain attributes from being changed once set you can declare them as immutable:
7
+
8
+ class MyModel < ActiveRecord::Base
9
+ attr_immutable :permalink, :param_identifier
10
+ end
11
+
12
+ When MyModel.find(:first).permalink = 'anything' is called it will raise an ImmutableAttributeError
13
+ MyModel.new.permalink = 'works!' will properly set the value because the record is unsaved.
14
+
15
+ If you'd only like this to happen for certain conditions, and want to handle other attribute-writers, too (like update_attribute), you can use the validation.
16
+
17
+ validates_immutable :permalink
18
+
19
+ Configuration options for the validation:
20
+ * :message - A customer error message (default is: "can't be changed")
21
+ * :if - Specifies a method, proc, or string to call to determine if the validation should occure (e.g., :if => :allow_validation or :if => Proc.new{|user| user.signup_step > 2}. The method, proc or string should return or evaluate to a true or false value.
22
+ * :unless - Specifies a method, proc, or string to call to determine if the validation should not occure (e.g., :unless => :skip_validation or :unless => Proc.new{|user| user.signup_step <= 2}. The method, proc or string should return or evaluate to a true or false value.
23
+
24
+ Created by Jack Danger Canty @ http://6brand.com
25
+ Released under the same licence as Rails (MIT)
26
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ require './lib/immutable_attributes.rb'
4
+
5
+ Hoe.new('immutable_attributes', ImmutableAttributes::VERSION) do |p|
6
+ p.developer('Jack Danger Canty', 'rubygems@6brand.com')
7
+ p.summary = "Immutable Attributes is an ActiveRecord extension that prevents existing data from changing"
8
+ end
9
+
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{immutable_attributes}
3
+ s.version = "1.0.3"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Jack Danger Canty"]
7
+ s.date = %q{2009-03-19}
8
+ s.email = ["rubygems@6brand.com"]
9
+ s.extra_rdoc_files = ["Manifest.txt"]
10
+ s.files = ["Manifest.txt", "README", "Rakefile", "immutable_attributes.gemspec", "init.rb", "install.rb", "lib/immutable_attributes.rb", "rails/init.rb", "test/immutable_attributes_test.rb", "uninstall.rb"]
11
+ s.has_rdoc = true
12
+ s.rdoc_options = ["--main", "README.txt"]
13
+ s.require_paths = ["lib"]
14
+ s.rubyforge_project = %q{immutable_attributes}
15
+ s.rubygems_version = %q{1.3.1}
16
+ s.summary = %q{Immutable Attributes is an ActiveRecord extension that prevents existing data from changing}
17
+
18
+ if s.respond_to? :specification_version then
19
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
20
+ s.specification_version = 2
21
+
22
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
23
+ s.add_development_dependency(%q<hoe>, [">= 1.11.0"])
24
+ else
25
+ s.add_dependency(%q<hoe>, [">= 1.11.0"])
26
+ end
27
+ else
28
+ s.add_dependency(%q<hoe>, [">= 1.11.0"])
29
+ end
30
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'immutable_attributes'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'README'))
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'activerecord'
3
+
4
+ module ImmutableErrors
5
+ class ImmutableAttributeError < ActiveRecord::ActiveRecordError
6
+ end
7
+ end
8
+
9
+ module ImmutableAttributes
10
+ VERSION = "1.0.3"
11
+ def attr_immutable(*args)
12
+ args.each do |meth|
13
+ class_eval do
14
+ define_method("#{meth}=") do |value|
15
+ new_record? || read_attribute(meth).nil? ?
16
+ write_attribute(meth, value) :
17
+ raise(ActiveRecord::ImmutableAttributeError, "#{meth} is immutable!")
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ def validates_immutable(*attr_names)
24
+ config = { :on => :update, :if => lambda {|x| true}, :message => "can't be changed" }
25
+ config.update(attr_names.extract_options!)
26
+
27
+ @immutables = attr_names
28
+
29
+ attr_names.each do |meth|
30
+ class_eval do
31
+ define_method("original_#{meth}") do
32
+ instance_variable_get("@original_#{meth}")
33
+ end
34
+ end
35
+ end
36
+
37
+ class_eval do
38
+ def self.immutables
39
+ @immutables
40
+ end
41
+
42
+ def after_initialize; end;
43
+
44
+ def setup_originals
45
+ self.class.immutables.each do |attr_name|
46
+ instance_variable_set("@original_#{attr_name}", send(attr_name.to_s))
47
+ end
48
+ end
49
+
50
+ after_initialize :setup_originals
51
+ end
52
+
53
+ validates_each(attr_names, config) do |record, attr_name, value|
54
+ next if record.send("original_#{attr_name.to_s}").nil?
55
+ record.errors.add(attr_name, config[:message]) if record.send("original_#{attr_name.to_s}") != record.send(attr_name.to_s)
56
+ end
57
+ end
58
+ end
59
+
60
+ ActiveRecord.send :include, ImmutableErrors
61
+ ActiveRecord::Base.extend ImmutableAttributes
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'immutable_attributes'
@@ -0,0 +1,31 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'activerecord'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'immutable_attributes')
5
+
6
+ ActiveRecord::Base.establish_connection(
7
+ :adapter => "sqlite3",
8
+ :dbfile => ":memory:"
9
+ )
10
+ ActiveRecord::Schema.define do
11
+ create_table :records do |table|
12
+ table.column :name, :string
13
+ table.column :body, :string
14
+ end
15
+ end
16
+
17
+ class Record < ActiveRecord::Base
18
+ attr_immutable :name
19
+ end
20
+
21
+ class ImmutableAttributesTest < Test::Unit::TestCase
22
+
23
+ def test_immutable_attribute_can_be_set
24
+ assert Record.new(:name => 'record name')
25
+ end
26
+
27
+ def test_immutable_attribute_cannot_be_changed
28
+ record = Record.create!(:name => 'record name')
29
+ assert_raises(ActiveRecord::ImmutableAttributeError) { record.update_attributes(:name => 'new name') }
30
+ end
31
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JackDanger-immutable_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Jack Danger Canty
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.11.0
24
+ version:
25
+ description:
26
+ email:
27
+ - rubygems@6brand.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - Manifest.txt
34
+ files:
35
+ - Manifest.txt
36
+ - README
37
+ - Rakefile
38
+ - immutable_attributes.gemspec
39
+ - init.rb
40
+ - install.rb
41
+ - lib/immutable_attributes.rb
42
+ - rails/init.rb
43
+ - test/immutable_attributes_test.rb
44
+ - uninstall.rb
45
+ has_rdoc: true
46
+ homepage:
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --main
50
+ - README.txt
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: immutable_attributes
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Immutable Attributes is an ActiveRecord extension that prevents existing data from changing
72
+ test_files: []
73
+