crstruct 0.1.191207
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +125 -0
- data/lib/crstruct.rb +8 -0
- data/lib/crstruct/open.rb +49 -0
- data/lib/crstruct/registered.rb +23 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 209a6ae14aeccf1e9a288b8cff5bc23d087db926e8b70bfb56a80584f91bd213
|
4
|
+
data.tar.gz: d831dbb9f99e53fbc4108ed912f03f6c183504a9f6b4db55a774dbc5d3f88bf8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb1664c894671291901cb2fde5045a617f8f36c279a688fc424cfbaff99210bdd53eb2cfb8654ed7494c65f8ab97ed7ffe367f1958e127388dee3d44fcded021
|
7
|
+
data.tar.gz: 6382accd098940a5f62f18e8e00491941cc206abd6379532442edd24c3dca926ac83f6dd521f2227dab361291372d5777dc95c1d8924a75f959a65325acef3c3
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# crstruct
|
2
|
+
|
3
|
+
* [github](https://www.github.com/carlosjhr64/crstruct)
|
4
|
+
* [rubygems](https://rubygems.org/gems/crstruct)
|
5
|
+
|
6
|
+
## DESCRIPTION
|
7
|
+
|
8
|
+
Ruby gem for an extremely lite OpenStruct like class with
|
9
|
+
attributes that can only be set once, and then read thereafter.
|
10
|
+
|
11
|
+
The "CR" in `CRStruct` is for "Create and Read"...
|
12
|
+
no "Update"...
|
13
|
+
no "Delete".
|
14
|
+
|
15
|
+
## INSTALL:
|
16
|
+
|
17
|
+
gem install crstruct
|
18
|
+
|
19
|
+
## SYNOPSIS
|
20
|
+
|
21
|
+
The following shows the intended use of CRStruct::Open:
|
22
|
+
|
23
|
+
require 'crstruct'
|
24
|
+
|
25
|
+
s = CRStruct::Open.new
|
26
|
+
s.a = "A" #=> "A"
|
27
|
+
|
28
|
+
begin
|
29
|
+
# Not allowed to reset s.a...
|
30
|
+
s.a = "B"
|
31
|
+
# as there's no actual s.a=...
|
32
|
+
rescue NoMethodError
|
33
|
+
s.b = "B" #=> "B"
|
34
|
+
end
|
35
|
+
|
36
|
+
if s.b == "B" #=> true
|
37
|
+
begin
|
38
|
+
# There is no s.c...
|
39
|
+
puts "s.c is #{s.c}"
|
40
|
+
rescue NoMethodError
|
41
|
+
s.c = "C" #=> "C"
|
42
|
+
# Output:
|
43
|
+
# s.a is A
|
44
|
+
# s.b is B
|
45
|
+
# s.c is C
|
46
|
+
puts "s.a is #{s.a}"
|
47
|
+
puts "s.b is #{s.b}"
|
48
|
+
puts "s.c is #{s.c}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
There's also a subclass CRStruct::Registered:
|
53
|
+
|
54
|
+
require 'crstruct'
|
55
|
+
|
56
|
+
s = CRStruct::Registered.new :a, :b, :c
|
57
|
+
s.a = "A" #=> "A"
|
58
|
+
s.b = "B" #=> "B"
|
59
|
+
s.c = "C" #=> "C"
|
60
|
+
begin
|
61
|
+
s.d = "D"
|
62
|
+
rescue NoMethodError
|
63
|
+
# Can't set :d as it was not registered with s.
|
64
|
+
end
|
65
|
+
# Output:
|
66
|
+
# #<CRStruct::Registered:0x0000561d37006b08
|
67
|
+
# @h={:a=>"A", :b=>"B", :c=>"C"},
|
68
|
+
# @r=[:a, :b, :c]>
|
69
|
+
pp s
|
70
|
+
|
71
|
+
|
72
|
+
## MORE
|
73
|
+
|
74
|
+
The following can be done, but
|
75
|
+
you'd be circumventing the intended use:
|
76
|
+
|
77
|
+
require 'crstruct'
|
78
|
+
|
79
|
+
s = CRStruct::Open.new a: "A", b: "B"
|
80
|
+
s.set!(:c, "C") #=> "C"
|
81
|
+
|
82
|
+
# s' internal hash does not have :d
|
83
|
+
if s.get?(:d).nil?
|
84
|
+
# Output:
|
85
|
+
# s.a is A
|
86
|
+
# s.b is B
|
87
|
+
# s.c is C
|
88
|
+
# s.d is nil
|
89
|
+
puts "s.a is #{s.get?(:a)}"
|
90
|
+
puts "s.b is #{s.get?(:b)}"
|
91
|
+
puts "s.c is #{s.get?(:c)}"
|
92
|
+
puts "s.d is #{s.get?(:d)}"
|
93
|
+
# Output:
|
94
|
+
# {:a=>"A", :b=>"B", :c=>"C"}
|
95
|
+
pp s.to_h
|
96
|
+
end
|
97
|
+
|
98
|
+
# The :free? method determines if a Symbol is avaible for setting.
|
99
|
+
s.free?(:to_s) #=> false
|
100
|
+
s.free?(:d) #=> true
|
101
|
+
|
102
|
+
## LICENSE:
|
103
|
+
|
104
|
+
(The MIT License)
|
105
|
+
|
106
|
+
Copyright (c) 2019 CarlosJHR64
|
107
|
+
|
108
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
109
|
+
a copy of this software and associated documentation files (the
|
110
|
+
'Software'), to deal in the Software without restriction, including
|
111
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
112
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
113
|
+
permit persons to whom the Software is furnished to do so, subject to
|
114
|
+
the following conditions:
|
115
|
+
|
116
|
+
The above copyright notice and this permission notice shall be
|
117
|
+
included in all copies or substantial portions of the Software.
|
118
|
+
|
119
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
120
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
121
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
122
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
123
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
124
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
125
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/crstruct.rb
ADDED
@@ -0,0 +1,49 @@
|
|
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 purposefull 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
|
49
|
+
|
@@ -0,0 +1,23 @@
|
|
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
|
23
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crstruct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.191207
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- carlosjhr64
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
Ruby gem for an extremely lite OpenStruct like class with
|
15
|
+
attributes that can only be set once, and then read thereafter.
|
16
|
+
|
17
|
+
The "CR" in `CRStruct` is for "Create and Read"...
|
18
|
+
no "Update"...
|
19
|
+
no "Delete".
|
20
|
+
email: carlosjhr64@gmail.com
|
21
|
+
executables: []
|
22
|
+
extensions: []
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- README.md
|
26
|
+
- lib/crstruct.rb
|
27
|
+
- lib/crstruct/open.rb
|
28
|
+
- lib/crstruct/registered.rb
|
29
|
+
homepage: https://github.com/carlosjhr64/crstruct
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements:
|
48
|
+
- 'ruby: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]'
|
49
|
+
rubygems_version: 3.0.3
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Ruby gem for an extremely lite OpenStruct like class with attributes that
|
53
|
+
can only be set once, and then read thereafter.
|
54
|
+
test_files: []
|