setonce 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +41 -0
  3. data/lib/setonce.rb +32 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f595482684cdea69750a95cc080a3b488e53e0a13f211ff9567d195409bb50c3
4
+ data.tar.gz: 6973b484f241a2fef78a4692d66e065dca5776544a334cdc0553845d381a1b07
5
+ SHA512:
6
+ metadata.gz: 0a894e4e4adfaafab468dcfb3d03ff44b8935c91fb30b6daf6ab11b02d9ea145d003e99827a594893ae443ecaf081e7c7053d50df2a7d976a4a3eb672a286e81
7
+ data.tar.gz: 7b189a3e2244e1b1334ac49823e0246881cf3580159d671ba2193048c2a6714338395ed7ddd75b7d693fce089afad12e2ef2910baffbf7d2ec4a4857ccb6288c
@@ -0,0 +1,41 @@
1
+ # attr_set_once
2
+
3
+ This gem adds a `attr_set_once` to the Class class. Use `attr_set_once` like
4
+ `attr_accessor`.
5
+
6
+ ```ruby
7
+ class MyClass
8
+ attr_set_once :hsa
9
+ end
10
+ ```
11
+
12
+ The attribute can be set as usual:
13
+
14
+ ```ruby
15
+ myob = MyClass.new
16
+ myob.hsa = 'x'
17
+ myob.hsa #=> x
18
+ ```
19
+
20
+ However, once the attribute has been set, it cannot be set again.
21
+
22
+ ```ruby
23
+ myob.hsa = 'y' # raises 'set-once-instance-variable-already-set: hsa'
24
+ ```
25
+
26
+ ## Install
27
+
28
+ ```ruby
29
+ gem install setonce
30
+ ```
31
+
32
+ ## Author
33
+
34
+ Mike O'Sullivan
35
+ mike@idocs.com
36
+
37
+ ## History
38
+
39
+ | version | date | notes |
40
+ |---------|-------------|-------------------------------------------------------|
41
+ | 1.0 | Mar 2, 2020 | Initial upload. |
@@ -0,0 +1,32 @@
1
+ #===============================================================================
2
+ # Class
3
+ #
4
+ class Class
5
+ # attr_set_once
6
+ def attr_set_once(*atts)
7
+ atts.each do |att|
8
+ att = att.to_s
9
+
10
+ # set
11
+ define_method("#{att}=") do |val|
12
+ if instance_variable_defined?("@#{att}")
13
+ raise 'set-once-instance-variable-already-set: ' + att
14
+ else
15
+ instance_variable_set "@#{att}", val
16
+ end
17
+ end
18
+
19
+ # get
20
+ define_method("#{att}") do
21
+ if instance_variable_defined?("@#{att}")
22
+ return instance_variable_get("@#{att}")
23
+ else
24
+ return nil
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ #
31
+ # Class
32
+ #===============================================================================
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: setonce
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Mike O'Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Like attr_accessor, but attribute can only be set once
14
+ email: mike@idocs.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/setonce.rb
21
+ homepage: https://rubygems.org/gems/setonce
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.7.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Object attribute that can only be set once
45
+ test_files: []