data_container 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c96761daa6ca3faffb4a7908d22c1a935dc83a2c
4
- data.tar.gz: c868705d807fcf9a148f094f4d8826cc633b0194
3
+ metadata.gz: 9ae20427afd37c61dfaaa19ae045faf56cd2c657
4
+ data.tar.gz: 51f16f7ebb3e488af7ed93fa0c4ce2d9349fe4c5
5
5
  SHA512:
6
- metadata.gz: acc092fc00359dfa5f9f8d79b10bd71aacf87b6b0e82a1f40d6ecb5979c472d086408f1393b117c7c7f5b206307ff9549e6b8adfdb3ea799fdd568e48ceaff62
7
- data.tar.gz: b3c15c86316663c4d673fadf88ebcb1272d95a21d6655a91413a8a10fe747b083b28e7453a7f8c97b47b3173e9a6dbcf4f538d60a22637fc220b07ad609bf771
6
+ metadata.gz: 5ccbae560fb380f34dbdb45a530b29d46d0cf5d900ec5b485b3dde864631a4194ec5e90d01f115c962720d8877e7f4e629676aec126ae9fa2bf393c250647ea9
7
+ data.tar.gz: 225a6011fc44894fceb049320072e9a3c0621be7086a262fe3c9eaba81c905133c76d67944518245fd936ccd73c0013e19711bce580b8a58d71e4f2783a1a1d8
@@ -0,0 +1,55 @@
1
+ require_relative 'data_container'
2
+
3
+ class AccessControlledDataContainer
4
+ def initialize(*args)
5
+ @data = DataContainer.new(*args)
6
+ @locked = Hash.new { |hash,attr| hash[attr] = false }
7
+ end
8
+
9
+ def name
10
+ 'AccessControlledDataContainer'
11
+ end
12
+
13
+ def to_s
14
+ @data.to_s.gsub!('DataContainer', name)
15
+ end
16
+
17
+ def lock(*attrs)
18
+ attrs.each { |attr| @locked[attr] = true }
19
+ end
20
+
21
+ def unlock(*attrs)
22
+ attrs.each { |attr| @locked[attr] = false }
23
+ end
24
+
25
+ def method_missing(sym, *args, &block)
26
+ raise_error_if_setting_locked_attribute(sym, *args)
27
+ @data.send(sym, *args, &block)
28
+ end
29
+
30
+ private
31
+
32
+ def raise_error_if_setting_locked_attribute(method, *args)
33
+ if method =~ /=$/ # attribute setting method, or []=
34
+ check_accessibility(method.to_s.chop)
35
+ elsif method == :set
36
+ check_accessibility(args.first)
37
+ end
38
+ end
39
+
40
+ def editing_method?(sym)
41
+ sym == :set || sym =~ /=$/ # attribute setting method, or []=
42
+ end
43
+
44
+ def locked?(attr)
45
+ @locked[attr.to_sym] == true # since default is false, nil is also ok
46
+ end
47
+
48
+ def raise_locked_attribute_error(attr)
49
+ raise DataContainer::AccessError, "cannot modify locked attribute '#{attr}'"
50
+ end
51
+
52
+ def check_accessibility(attr)
53
+ raise_locked_attribute_error(attr) if locked?(attr)
54
+ end
55
+ end
@@ -20,11 +20,7 @@ class DataContainer < Struct
20
20
  end
21
21
 
22
22
  def set(ivar, value)
23
- if include?(ivar)
24
- send("#{ivar}=", value)
25
- else
26
- raise AttributeError, attribute_error_message(ivar)
27
- end
23
+ do_if_included(ivar) { send("#{ivar}=", value) }
28
24
  end
29
25
 
30
26
  def to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_container
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelli Searfos
@@ -32,6 +32,7 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - lib/access_controlled_data_container.rb
35
36
  - lib/data_container.rb
36
37
  homepage: https://github.com/ksearfos/data_container
37
38
  licenses: []