attr_immutable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in attr_immutable.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "attr_immutable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "attr_immutable"
7
+ s.version = AttrImmutable::VERSION
8
+ s.authors = ["Mark Menard"]
9
+ s.email = ["mark@mjm.net"]
10
+ s.homepage = ""
11
+ s.summary = %q{Add attr_immutable to create immutable protected attributes on your Ruby classes.}
12
+ s.description = %q{AttrImmutable add the attr_immutable class method to create immutable properties on Ruby classes. The attribute are only accessible using the accessor/mutator pair by hiding the attribute value using a closure. (ie. the immutable attribute is not stored in an instance variable and therefore can't accessed in any other way.)}
13
+
14
+ s.rubyforge_project = "attr_immutable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,3 @@
1
+ module AttrImmutable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ require "attr_immutable/version"
2
+
3
+ module AttrImmutable
4
+ def self.included (base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def attr_immutable (*args)
11
+ args.each do |arg|
12
+ setter = "#{arg}=".to_sym
13
+
14
+ define_method(arg) do
15
+ self.attr_immutable(arg)
16
+ self.send(arg)
17
+ end
18
+
19
+ define_method(setter) do |value|
20
+ self.attr_immutable(arg)
21
+ self.send(setter, value)
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def attr_immutable (arg)
28
+ attribute_set = false
29
+ attribute = nil
30
+
31
+ singleton_class = class << self
32
+ self
33
+ end
34
+
35
+ singleton_class.send(:define_method, arg) do
36
+ attribute
37
+ end
38
+
39
+ singleton_class.send(:define_method, "#{arg.to_s}=") do |value|
40
+ raise "ERROR: Attempt to modify an immutable attribute" if attribute_set
41
+ attribute_set = true
42
+ attribute = value
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_immutable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Mark Menard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-04 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: AttrImmutable add the attr_immutable class method to create immutable properties on Ruby classes. The attribute are only accessible using the accessor/mutator pair by hiding the attribute value using a closure. (ie. the immutable attribute is not stored in an instance variable and therefore can't accessed in any other way.)
17
+ email:
18
+ - mark@mjm.net
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - Gemfile
28
+ - Rakefile
29
+ - attr_immutable.gemspec
30
+ - lib/attr_immutable.rb
31
+ - lib/attr_immutable/version.rb
32
+ homepage: ""
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ requirements: []
53
+
54
+ rubyforge_project: attr_immutable
55
+ rubygems_version: 1.8.7
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Add attr_immutable to create immutable protected attributes on your Ruby classes.
59
+ test_files: []
60
+