crstruct 0.1.210920 → 1.0.221218
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -30
- data/lib/crstruct.rb +20 -3
- metadata +9 -11
- data/lib/crstruct/open.rb +0 -48
- data/lib/crstruct/registered.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a713b076e07ecbd1b25b13a7326cb761a311d77a96184a749feab77ed8d618e5
|
4
|
+
data.tar.gz: b104e26fd619dd30524785342c6a087ccaf39ad8a4cbedb5e83e74280bfa311b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ee0c530bda11e5559a28b21df33bc142af213a980f67a13ae0d8f7519a2f3a2b5d3a82ec57778d1542c4d67641bcb864270951d7687366365959ebac8f2176f
|
7
|
+
data.tar.gz: dfa6d07b64926709ac873aa5c6be86e80bc46f8a30a88e8d8bf9f7f19b7ccac27a24d5a9b24e7946e04f523ab29afbde22752db357c11d768151434966ac064c
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# CRStruct
|
2
2
|
|
3
|
-
* [VERSION 0.
|
3
|
+
* [VERSION 1.0.221218](https://www.github.com/carlosjhr64/crstruct/releases)
|
4
4
|
* [github](https://www.github.com/carlosjhr64/crstruct)
|
5
5
|
* [rubygems](https://rubygems.org/gems/crstruct)
|
6
6
|
|
@@ -10,8 +10,8 @@ Ruby gem for an extremely lite OpenStruct like class with
|
|
10
10
|
attributes that can only be set once, and then read thereafter.
|
11
11
|
|
12
12
|
The "CR" in `CRStruct` is for "Create and Read"...
|
13
|
-
|
14
|
-
|
13
|
+
No "Update"...
|
14
|
+
No "Delete".
|
15
15
|
|
16
16
|
## INSTALL:
|
17
17
|
```console
|
@@ -30,19 +30,16 @@ s.a #=> "A"
|
|
30
30
|
begin
|
31
31
|
# Not allowed to reset s.a...
|
32
32
|
s.a = "B" # raises error
|
33
|
-
|
34
|
-
rescue NoMethodError
|
33
|
+
rescue FrozenError
|
35
34
|
s.b = "B"
|
36
35
|
end
|
37
36
|
s.a #=> "A"
|
38
37
|
s.b #=> "B"
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
s.c = "C"
|
45
|
-
end
|
39
|
+
begin
|
40
|
+
s.c # There is no s.c so raises
|
41
|
+
rescue NoMethodError
|
42
|
+
s.c = "C"
|
46
43
|
end
|
47
44
|
s.a #=> "A"
|
48
45
|
s.b #=> "B"
|
@@ -56,32 +53,17 @@ s.b = "B"
|
|
56
53
|
s.c = "C"
|
57
54
|
begin
|
58
55
|
s.d = "D"
|
59
|
-
rescue
|
56
|
+
rescue KeyError
|
60
57
|
# Can't set :d as it was not registered with s.
|
61
58
|
end
|
62
59
|
s
|
63
|
-
#~> ^#<CRStruct::Registered:0x\h+ @
|
64
|
-
```
|
65
|
-
## MORE
|
66
|
-
|
67
|
-
The following can be done, but
|
68
|
-
you'd be circumventing the intended use:
|
69
|
-
```ruby
|
70
|
-
s = CRStruct::Open.new a: "A", b: "B"
|
71
|
-
s.get?(:c) #=> nil
|
72
|
-
s.set!(:c, "C")
|
73
|
-
s.get?(:c) #=> "C"
|
74
|
-
s.c #=> "C"
|
75
|
-
|
76
|
-
# The :free? method determines if a Symbol is available for setting.
|
77
|
-
s.free?(:to_s) #=> false
|
78
|
-
s.free?(:d) #=> true
|
60
|
+
#~> ^#<CRStruct::Registered:0x\h+ @keys=\[:a, :b, :c\]>$
|
79
61
|
```
|
80
62
|
## LICENSE:
|
81
63
|
|
82
64
|
(The MIT License)
|
83
65
|
|
84
|
-
Copyright (c)
|
66
|
+
Copyright (c) 2022 CarlosJHR64
|
85
67
|
|
86
68
|
Permission is hereby granted, free of charge, to any person obtaining
|
87
69
|
a copy of this software and associated documentation files (the
|
data/lib/crstruct.rb
CHANGED
@@ -1,7 +1,24 @@
|
|
1
1
|
module CRStruct
|
2
|
-
VERSION = '0.
|
3
|
-
|
4
|
-
|
2
|
+
VERSION = '1.0.221218'
|
3
|
+
class Open
|
4
|
+
def method_missing(key, *args, &blk)
|
5
|
+
super unless blk.nil? and key[-1]=='=' and args.length==1
|
6
|
+
k,v = key[0..-2].to_sym,args[0]
|
7
|
+
raise FrozenError, "can't modify CRStruct method: :#{k}" if respond_to? k
|
8
|
+
define_singleton_method(k){v}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Registered < Open
|
13
|
+
def initialize(*keys)
|
14
|
+
@keys = keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def define_singleton_method(k,&blk)
|
18
|
+
raise KeyError, "can't add unregistered key: :#{k}" unless @keys.include? k
|
19
|
+
super(k,&blk)
|
20
|
+
end
|
21
|
+
end
|
5
22
|
end
|
6
23
|
|
7
24
|
# Requires:
|
metadata
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crstruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.221218
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- CarlosJHR64
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
Ruby gem for an extremely lite OpenStruct like class with
|
15
15
|
attributes that can only be set once, and then read thereafter.
|
16
16
|
|
17
17
|
The "CR" in `CRStruct` is for "Create and Read"...
|
18
|
-
|
19
|
-
|
18
|
+
No "Update"...
|
19
|
+
No "Delete".
|
20
20
|
email: carlosjhr64@gmail.com
|
21
21
|
executables: []
|
22
22
|
extensions: []
|
@@ -24,13 +24,11 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- README.md
|
26
26
|
- lib/crstruct.rb
|
27
|
-
- lib/crstruct/open.rb
|
28
|
-
- lib/crstruct/registered.rb
|
29
27
|
homepage: https://github.com/carlosjhr64/crstruct
|
30
28
|
licenses:
|
31
29
|
- MIT
|
32
30
|
metadata: {}
|
33
|
-
post_install_message:
|
31
|
+
post_install_message:
|
34
32
|
rdoc_options: []
|
35
33
|
require_paths:
|
36
34
|
- lib
|
@@ -45,9 +43,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
43
|
- !ruby/object:Gem::Version
|
46
44
|
version: '0'
|
47
45
|
requirements:
|
48
|
-
- 'ruby: ruby 3.
|
49
|
-
rubygems_version: 3.
|
50
|
-
signing_key:
|
46
|
+
- 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
|
47
|
+
rubygems_version: 3.3.7
|
48
|
+
signing_key:
|
51
49
|
specification_version: 4
|
52
50
|
summary: Ruby gem for an extremely lite OpenStruct like class with attributes that
|
53
51
|
can only be set once, and then read thereafter.
|
data/lib/crstruct/open.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
module CRStruct
|
2
|
-
class Open
|
3
|
-
### h should support interface ###
|
4
|
-
# raise "need Hash(Symbol,Value)" unless
|
5
|
-
# [:to_h, :keys, :has_key?, :[], :[]=].all?{|_|h.respond_to?_} and
|
6
|
-
# h.keys.all?{|_|_.is_a? Symbol}
|
7
|
-
def initialize(h={})
|
8
|
-
@h = h
|
9
|
-
end
|
10
|
-
|
11
|
-
# Can it somehow be converted into a Hash?
|
12
|
-
# Most likely.
|
13
|
-
def to_h
|
14
|
-
@h.to_h
|
15
|
-
end
|
16
|
-
|
17
|
-
#########################################
|
18
|
-
### free?, set!, and get? ###
|
19
|
-
# make purposeful access to @h possible,
|
20
|
-
# and easier to subclass CRStruct.
|
21
|
-
#########################################
|
22
|
-
|
23
|
-
def free?(k)
|
24
|
-
!(@h.has_key?(k) or respond_to?(k))
|
25
|
-
end
|
26
|
-
def set!(k, v)
|
27
|
-
@h[k]=v
|
28
|
-
end
|
29
|
-
def get?(k)
|
30
|
-
@h[k]
|
31
|
-
end
|
32
|
-
|
33
|
-
def method_missing(key, *args, &proc)
|
34
|
-
if proc.nil?
|
35
|
-
case args.length
|
36
|
-
when 0
|
37
|
-
return get?(key) if @h.has_key? key
|
38
|
-
when 1
|
39
|
-
if key=~/^\w+=$/
|
40
|
-
k = key[0..-2].to_sym
|
41
|
-
return set!(k, args[0]) if free?(k)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
super
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
data/lib/crstruct/registered.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
module CRStruct
|
2
|
-
class Registered < Open
|
3
|
-
# Registered.new :a, :b, :c, :d
|
4
|
-
# Registered.new {a: 1, b: 2}, :c, :d
|
5
|
-
# Registered.new {a: 1, b: 2}, [:c, :d]
|
6
|
-
# (*keys).flatten! should result to an Array(Symbol)
|
7
|
-
def initialize(h, *keys)
|
8
|
-
keys.flatten!
|
9
|
-
if h.is_a? Symbol
|
10
|
-
keys.unshift(h)
|
11
|
-
h = {}
|
12
|
-
end
|
13
|
-
@r = keys
|
14
|
-
super(h)
|
15
|
-
end
|
16
|
-
|
17
|
-
def free?(k)
|
18
|
-
return false unless @r.include? k
|
19
|
-
super(k)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|