immutable_struct_ex 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +46 -3
- data/bin/console +1 -0
- data/lib/comparable.rb +18 -0
- data/lib/immutable.rb +16 -0
- data/lib/immutable_struct_ex/version.rb +1 -1
- data/lib/immutable_struct_ex.rb +4 -20
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ffcdbe2613240712bb17679f5399a1caa3253c264cf0c9c10a678a4caa4a3f7
|
4
|
+
data.tar.gz: 29ff8adba1c95169aa7552fb0f4eff84345c5f602ef2f9f1f79d1e2323bc2e37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be076b0ffa994a1ad8a5b351017cf4e7b1f82d2b67dc1b582d80cc96e2c2465ae1c381a957b8868751de2699ba5f9eec60336dc9b9d5f679061d7189ef2335fd
|
7
|
+
data.tar.gz: 19bbbbd8d226a8808546e89897060166bd7c74a8fa10a40ce1e1f0289a08e9b0b72fa99830f473d918ccb872446f782a014f86124cb9fc8f32487f13a56e87e3
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
### 0.2.1
|
2
|
+
* Enhancements
|
3
|
+
* General refactors/cleanup
|
4
|
+
* Added Immutable module; when extended on a struct, makes it immutable.
|
5
|
+
* Added Comparable module; when extended on a struct, makes it comparable to other structs and hashes.
|
6
|
+
|
1
7
|
### 0.1.1
|
2
8
|
* Enhancements
|
3
9
|
* Added this CHANGELOG.md
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
[![GitHub version](http://badge.fury.io/gh/gangelo%2Fimmutable_struct_ex.svg)](https://badge.fury.io/gh/gangelo%2Fimmutable_struct_ex)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/immutable_struct_ex.svg)](https://badge.fury.io/rb/immutable_struct_ex)
|
3
|
+
|
4
|
+
[![](http://ruby-gem-downloads-badge.herokuapp.com/immutable_struct_ex?type=total)](http://www.rubydoc.info/gems/immutable_struct_ex/)
|
5
|
+
[![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://www.rubydoc.info/gems/immutable_struct_ex/)
|
6
|
+
|
7
|
+
[![Report Issues](https://img.shields.io/badge/report-issues-red.svg)](https://github.com/gangelo/immutable_struct_ex/issues)
|
8
|
+
|
9
|
+
[![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg)](#license)
|
10
|
+
|
1
11
|
# ImmutableStructEx
|
2
12
|
|
3
13
|
_ImmutableStructEx_ is yet another immutable struct. What makes ImmutableStructEx different, is that it allows you to create immutable structs in one step _by default_. In other words, other immutable struct gems force you to first define the struct, then instantiate the struct object; or, define the struct and instantiate the struct object via chaining. For example:
|
@@ -19,13 +29,13 @@ ImmutableStructEx allows you do this in one step:
|
|
19
29
|
|
20
30
|
```ruby
|
21
31
|
immutable_struct_ex = ImmutableStructEx.new(first: 'John', last: 'Doe', phone: '(201) 230-7281')
|
22
|
-
immutable_struct_ex.first
|
32
|
+
immutable_struct_ex.first
|
23
33
|
#=> 'John'
|
24
34
|
immutable_struct_ex[:first]
|
25
35
|
#=> 'John'
|
26
|
-
immutable_struct_ex.last
|
36
|
+
immutable_struct_ex.last
|
27
37
|
#=> 'Doe'
|
28
|
-
immutable_struct_ex.phone
|
38
|
+
immutable_struct_ex.phone
|
29
39
|
#=> '(201) 230-7281'
|
30
40
|
```
|
31
41
|
### Immutable
|
@@ -50,6 +60,39 @@ immutable_struct_ex.john?
|
|
50
60
|
#=> true
|
51
61
|
```
|
52
62
|
|
63
|
+
### Other Examples
|
64
|
+
```ruby
|
65
|
+
# Redactable, immutable struct
|
66
|
+
user = { username: 'jdoe', password: 'p@55w0rD', ssn: '123-70-9182' }
|
67
|
+
immutable_struct_ex = ImmutableStructEx.new(**user) do
|
68
|
+
REDACT = %i(password ssn).freeze
|
69
|
+
|
70
|
+
def inspect
|
71
|
+
to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_s
|
75
|
+
super.to_s.tap do |string|
|
76
|
+
REDACT.each do |redact|
|
77
|
+
string.gsub!(/( #{Regexp.quote(redact.to_s)}=")(.*?)(")/, '\1[REDACTED]\3')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_h
|
83
|
+
super.to_h.tap do |hash|
|
84
|
+
REDACT.each { |redact| hash[redact] = '[REDACTED]' }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
immutable_struct_ex.inspect
|
90
|
+
#=> "#<struct username=\"jdoe\", password=\"[REDACTED]\", ssn=\"[REDACTED]\">"
|
91
|
+
|
92
|
+
immutable_struct_ex.to_h
|
93
|
+
#=> {:username=>"jdoe", :password=>"[REDACTED]", :ssn=>"[REDACTED]"}
|
94
|
+
```
|
95
|
+
|
53
96
|
## Installation
|
54
97
|
|
55
98
|
Add this line to your application's Gemfile:
|
data/bin/console
CHANGED
data/lib/comparable.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImmutableStructEx
|
4
|
+
# Makes a struct comparable with another struct or hash when extended.
|
5
|
+
module Comparable
|
6
|
+
class << self
|
7
|
+
def extended(struct)
|
8
|
+
struct.instance_eval do
|
9
|
+
def ==(other)
|
10
|
+
return false unless other.is_a?(Hash) || other.is_a?(Struct)
|
11
|
+
|
12
|
+
to_h == other.to_h
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/immutable.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImmutableStructEx
|
4
|
+
# Makes a struct immutable when extended.
|
5
|
+
module Immutable
|
6
|
+
class << self
|
7
|
+
def extended(struct)
|
8
|
+
[:[], *struct.members].each do |method|
|
9
|
+
struct.instance_eval do
|
10
|
+
undef :"#{method}="
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/immutable_struct_ex.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'immutable_struct_ex/version'
|
4
|
+
require 'comparable'
|
5
|
+
require 'immutable'
|
4
6
|
|
5
7
|
# Defines the methods used to create/manage the ImmutableStructEx struct.
|
6
8
|
module ImmutableStructEx
|
@@ -8,27 +10,9 @@ module ImmutableStructEx
|
|
8
10
|
def new(**hash, &block)
|
9
11
|
options_struct = Struct.new(*hash.keys, keyword_init: true, &block)
|
10
12
|
options_struct.new(**hash).tap do |struct|
|
11
|
-
|
12
|
-
|
13
|
-
<<~RUBY
|
14
|
-
undef :"#{method}="
|
15
|
-
RUBY
|
16
|
-
end
|
17
|
-
end
|
18
|
-
evaluate(struct) do
|
19
|
-
<<~RUBY
|
20
|
-
def ==(object)
|
21
|
-
return false unless object.respond_to? :to_h
|
22
|
-
|
23
|
-
to_h == object.to_h
|
24
|
-
end
|
25
|
-
RUBY
|
26
|
-
end
|
13
|
+
struct.extend Comparable
|
14
|
+
struct.extend Immutable
|
27
15
|
end
|
28
16
|
end
|
29
|
-
|
30
|
-
def evaluate(struct)
|
31
|
-
struct.instance_eval yield
|
32
|
-
end
|
33
17
|
end
|
34
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immutable_struct_ex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gene M. Angelo, Jr.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-byebug
|
@@ -171,6 +171,8 @@ files:
|
|
171
171
|
- bin/console
|
172
172
|
- bin/setup
|
173
173
|
- immutable_struct_ex.gemspec
|
174
|
+
- lib/comparable.rb
|
175
|
+
- lib/immutable.rb
|
174
176
|
- lib/immutable_struct_ex.rb
|
175
177
|
- lib/immutable_struct_ex/version.rb
|
176
178
|
homepage: https://github.com/gangelo/immutable_struct_ex
|