crstruct 0.1.191207 → 1.0.221218

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
  SHA256:
3
- metadata.gz: 209a6ae14aeccf1e9a288b8cff5bc23d087db926e8b70bfb56a80584f91bd213
4
- data.tar.gz: d831dbb9f99e53fbc4108ed912f03f6c183504a9f6b4db55a774dbc5d3f88bf8
3
+ metadata.gz: a713b076e07ecbd1b25b13a7326cb761a311d77a96184a749feab77ed8d618e5
4
+ data.tar.gz: b104e26fd619dd30524785342c6a087ccaf39ad8a4cbedb5e83e74280bfa311b
5
5
  SHA512:
6
- metadata.gz: bb1664c894671291901cb2fde5045a617f8f36c279a688fc424cfbaff99210bdd53eb2cfb8654ed7494c65f8ab97ed7ffe367f1958e127388dee3d44fcded021
7
- data.tar.gz: 6382accd098940a5f62f18e8e00491941cc206abd6379532442edd24c3dca926ac83f6dd521f2227dab361291372d5777dc95c1d8924a75f959a65325acef3c3
6
+ metadata.gz: 1ee0c530bda11e5559a28b21df33bc142af213a980f67a13ae0d8f7519a2f3a2b5d3a82ec57778d1542c4d67641bcb864270951d7687366365959ebac8f2176f
7
+ data.tar.gz: dfa6d07b64926709ac873aa5c6be86e80bc46f8a30a88e8d8bf9f7f19b7ccac27a24d5a9b24e7946e04f523ab29afbde22752db357c11d768151434966ac064c
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
- # crstruct
1
+ # CRStruct
2
2
 
3
+ * [VERSION 1.0.221218](https://www.github.com/carlosjhr64/crstruct/releases)
3
4
  * [github](https://www.github.com/carlosjhr64/crstruct)
4
5
  * [rubygems](https://rubygems.org/gems/crstruct)
5
6
 
@@ -9,101 +10,60 @@ Ruby gem for an extremely lite OpenStruct like class with
9
10
  attributes that can only be set once, and then read thereafter.
10
11
 
11
12
  The "CR" in `CRStruct` is for "Create and Read"...
12
- no "Update"...
13
- no "Delete".
13
+ No "Update"...
14
+ No "Delete".
14
15
 
15
16
  ## INSTALL:
16
-
17
- gem install crstruct
18
-
17
+ ```console
18
+ $ gem install crstruct
19
+ ```
19
20
  ## SYNOPSIS
20
21
 
21
22
  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
-
23
+ ```ruby
24
+ require 'crstruct'
25
+
26
+ s = CRStruct::Open.new
27
+ s.a = "A"
28
+ s.a #=> "A"
29
+
30
+ begin
31
+ # Not allowed to reset s.a...
32
+ s.a = "B" # raises error
33
+ rescue FrozenError
34
+ s.b = "B"
35
+ end
36
+ s.a #=> "A"
37
+ s.b #=> "B"
38
+
39
+ begin
40
+ s.c # There is no s.c so raises
41
+ rescue NoMethodError
42
+ s.c = "C"
43
+ end
44
+ s.a #=> "A"
45
+ s.b #=> "B"
46
+ s.c #=> "C"
47
+ ```
52
48
  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
-
49
+ ```ruby
50
+ s = CRStruct::Registered.new :a, :b, :c
51
+ s.a = "A"
52
+ s.b = "B"
53
+ s.c = "C"
54
+ begin
55
+ s.d = "D"
56
+ rescue KeyError
57
+ # Can't set :d as it was not registered with s.
58
+ end
59
+ s
60
+ #~> ^#<CRStruct::Registered:0x\h+ @keys=\[:a, :b, :c\]>$
61
+ ```
102
62
  ## LICENSE:
103
63
 
104
64
  (The MIT License)
105
65
 
106
- Copyright (c) 2019 CarlosJHR64
66
+ Copyright (c) 2022 CarlosJHR64
107
67
 
108
68
  Permission is hereby granted, free of charge, to any person obtaining
109
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.1.191207'
3
- require 'crstruct/open'
4
- autoload :Registered, 'crstruct/registered'
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.1.191207
4
+ version: 1.0.221218
5
5
  platform: ruby
6
6
  authors:
7
- - carlosjhr64
7
+ - CarlosJHR64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-07 00:00:00.000000000 Z
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
- no "Update"...
19
- no "Delete".
18
+ No "Update"...
19
+ No "Delete".
20
20
  email: carlosjhr64@gmail.com
21
21
  executables: []
22
22
  extensions: []
@@ -24,8 +24,6 @@ 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
@@ -45,8 +43,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
43
  - !ruby/object:Gem::Version
46
44
  version: '0'
47
45
  requirements:
48
- - 'ruby: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]'
49
- rubygems_version: 3.0.3
46
+ - 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
47
+ rubygems_version: 3.3.7
50
48
  signing_key:
51
49
  specification_version: 4
52
50
  summary: Ruby gem for an extremely lite OpenStruct like class with attributes that
data/lib/crstruct/open.rb DELETED
@@ -1,49 +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 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
-
@@ -1,23 +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
23
-