with_ease 0.0.0 → 0.0.1
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 +162 -8
- data/lib/with_ease/closed_struct.rb +2 -2
- data/lib/with_ease/forward.rb +12 -0
- data/lib/with_ease/iterator.rb +2 -0
- data/lib/with_ease/open_struct/hash_api.rb +1 -1
- data/lib/with_ease/open_struct.rb +2 -2
- data/lib/with_ease/version.rb +1 -1
- data/lib/with_ease.rb +8 -0
- metadata +3 -2
- data/lib/with_ease/forwardable.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b82599593f37f0f889da475eff808db8fb46259754cd245d11ea842f6e454325
|
4
|
+
data.tar.gz: 3bf87be2c1b028d522205597c7d952a055bab0907ccc8f2d805ac40902a148ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab419bec4a95ac73a27eed1746a9c231d6c381f318449a92fdba9b77be68a8ebf42a94c855a20fcc627e11f8415f7a5889116e50fb4f1c1d2ae517018d9b5abe
|
7
|
+
data.tar.gz: aea48838989fd9360f3054eea73c7aee0d5968ab43089bb612e2342656b30e81deecb530cc4ebeac2cae390cdb2111e5949c52c7cf3a8e3f1cfe50439a7028ad
|
data/README.md
CHANGED
@@ -1,16 +1,170 @@
|
|
1
|
-
[](http://badge.fury.io/rb/with_ease)
|
2
|
+
[](https://rubygems.org/gems/with_ease)
|
3
3
|
|
4
|
-
#
|
4
|
+
# WithEase
|
5
5
|
|
6
|
-
|
6
|
+
Very basic but useful extensions to Ruby.
|
7
7
|
|
8
|
-
|
8
|
+
**N.B.:** still in alpha
|
9
9
|
|
10
|
-
|
11
|
-
- [Extensions to `Forwardable`](spec/forwardable)
|
12
|
-
- [`WithEase::ClosedStruct`](spec/closed_struct)
|
10
|
+
## Installation
|
13
11
|
|
12
|
+
```sh
|
13
|
+
gem install with_ease
|
14
|
+
```
|
15
|
+
|
16
|
+
With bundler
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'with_ease'
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'with_ease'
|
26
|
+
```
|
27
|
+
|
28
|
+
For more sophisticated tools and imports into the current tools look at the [speculations](speculations)
|
29
|
+
|
30
|
+
In all the speculations we assume that `require 'with_ease'` has been used in the setup (`spec_helper.rb` or
|
31
|
+
`speculations_helper.rb`)
|
32
|
+
|
33
|
+
## Synopsis
|
34
|
+
|
35
|
+
What can you do `WithEase`.
|
36
|
+
|
37
|
+
#### Have an `OpenStruct` with a complete Hash like API
|
38
|
+
|
39
|
+
- merge, update, slice
|
40
|
+
- pattern matching
|
41
|
+
|
42
|
+
#### Have a `ClosedStruct` with the same API but fixed keys
|
43
|
+
|
44
|
+
#### Have a lean wrapper `Forward` around `Forwardable` which is actually **readable!**
|
45
|
+
|
46
|
+
[c.f. for details](speculations/forward.md)
|
47
|
+
|
48
|
+
#### Have an `Iterator` class (and constructor function if you want).
|
49
|
+
|
50
|
+
Nothing you cannot do with `Enumerator::Lazy` but so much simpler again.
|
51
|
+
|
52
|
+
[c.f for details](speculations/iterator.md)
|
53
|
+
|
54
|
+
|
55
|
+
## Quick Start
|
56
|
+
|
57
|
+
All functionality is described in detail in the [speculations](speculations) (see links above for
|
58
|
+
the specific speculations of a functionality).
|
59
|
+
|
60
|
+
But here is a quick overview (also tested with the [speculate_about gem](https://rubygems.org/gems/speculate_about) as all other speculations).
|
61
|
+
|
62
|
+
### Context: `OpenStruct`
|
63
|
+
|
64
|
+
Given
|
65
|
+
```ruby
|
66
|
+
require 'with_ease'
|
67
|
+
|
68
|
+
let(:subject) { OpenStruct.new(a: 1, b: 2) }
|
69
|
+
```
|
70
|
+
|
71
|
+
Then we can use it mostly like a `Hash` instance.
|
72
|
+
```ruby
|
73
|
+
expect(subject.merge(a: 11, c: 31)).to eq(OpenStruct.new(a: 11, b: 2, c:31))
|
74
|
+
```
|
75
|
+
And we can pattern match
|
76
|
+
```ruby
|
77
|
+
subject => {a:, b: 2}
|
78
|
+
expect(a).to eq(1)
|
79
|
+
```
|
80
|
+
|
81
|
+
### Context: `ClosedStruct`
|
82
|
+
|
83
|
+
Given
|
84
|
+
```ruby
|
85
|
+
require 'with_ease'
|
86
|
+
ClosedStruct = WithEase::ClosedStruct
|
87
|
+
|
88
|
+
let(:subject) { ClosedStruct.new(a: 1, b: 2) }
|
89
|
+
```
|
90
|
+
|
91
|
+
Then we can do pretty much the same as with `OpenStruct`
|
92
|
+
```ruby
|
93
|
+
subject.update(a: 0)
|
94
|
+
expect(subject).to eq(ClosedStruct.new(a: 0, b: 2))
|
95
|
+
|
96
|
+
```
|
97
|
+
|
98
|
+
But you must not add a key
|
99
|
+
```ruby
|
100
|
+
expect { subject.update(c: 3) }
|
101
|
+
.to raise_error(KeyError)
|
102
|
+
expect{ subject.merge(c: 3) }
|
103
|
+
.to raise_error(KeyError)
|
104
|
+
```
|
105
|
+
|
106
|
+
[c.f for details](speculations/closed_struct.md)
|
107
|
+
|
108
|
+
### Context: `Iterator`
|
109
|
+
|
110
|
+
As mentioned above you can ~easily~ (it's not complicated but it is not easy!) create
|
111
|
+
an iterator in Ruby, howver this one is so much simpler.
|
112
|
+
|
113
|
+
Given
|
114
|
+
```ruby
|
115
|
+
require 'with_ease'
|
116
|
+
Iterator = WithEase::Iterator
|
117
|
+
|
118
|
+
let(:subject) { Iterator.new(0, &:succ) }
|
119
|
+
```
|
120
|
+
|
121
|
+
Then we can just advance it
|
122
|
+
```ruby
|
123
|
+
expect(subject.value).to be_zero
|
124
|
+
expect(subject.advance.value).to eq(1)
|
125
|
+
```
|
126
|
+
|
127
|
+
It is an `Enumerable` of course and quite lazy, if you are intereted
|
128
|
+
[here is more](speculations/iterator.md)
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
[c.f for details](speculations/iterator.md)
|
133
|
+
|
134
|
+
### Context: `Forward`
|
135
|
+
|
136
|
+
Given
|
137
|
+
```ruby
|
138
|
+
require 'with_ease'
|
139
|
+
|
140
|
+
Forward = WithEase::Forward
|
141
|
+
```
|
142
|
+
|
143
|
+
**N.B.**: We could have used `require 'with_ease/import_all'` instead.
|
144
|
+
|
145
|
+
And
|
146
|
+
```ruby
|
147
|
+
class Wrapper
|
148
|
+
extend Forward
|
149
|
+
forward :empty?, :size, to: :@array
|
150
|
+
def initialize(array) = @array = array.to_a
|
151
|
+
end
|
152
|
+
|
153
|
+
let(:subject) { Wrapper.new(0..9) }
|
154
|
+
```
|
155
|
+
|
156
|
+
Then we can see that
|
157
|
+
```ruby
|
158
|
+
expect(subject).not_to be_empty
|
159
|
+
expect(subject.size).to eq(10)
|
160
|
+
```
|
161
|
+
But also
|
162
|
+
```ruby
|
163
|
+
expect(Wrapper.new([])).to be_empty
|
164
|
+
```
|
165
|
+
|
166
|
+
|
167
|
+
[c.f. for details](speculations/forward.md)
|
14
168
|
|
15
169
|
## Author
|
16
170
|
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative 'forward'
|
4
4
|
require_relative 'closed_struct/hash_api'
|
5
5
|
|
6
6
|
module WithEase
|
7
7
|
class ClosedStruct
|
8
8
|
|
9
|
-
extend
|
9
|
+
extend Forward
|
10
10
|
include HashApi
|
11
11
|
|
12
12
|
forward :deconstruct_keys, :fetch, :to_a, :to_h, to: :@data
|
data/lib/with_ease/iterator.rb
CHANGED
@@ -35,7 +35,7 @@ module WithEase
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def _merge_with_blk(key, **keys, &blk)
|
38
|
-
raise ArgumentError, "no **keys arguments allowed
|
38
|
+
raise ArgumentError, "no **keys arguments allowed if block given" unless keys.empty?
|
39
39
|
value = fetch(key)
|
40
40
|
_merge(key => blk.(value))
|
41
41
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'ostruct'
|
4
|
-
require_relative '
|
4
|
+
require_relative 'forward'
|
5
5
|
require_relative 'open_struct/hash_api'
|
6
6
|
class OpenStruct
|
7
|
-
extend
|
7
|
+
extend WithEase::Forward
|
8
8
|
forward :deconstruct_keys, :fetch, :to_a, to: :to_h
|
9
9
|
forward :deconstruct, to: :to_a
|
10
10
|
|
data/lib/with_ease/version.rb
CHANGED
data/lib/with_ease.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_ease
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
@@ -33,10 +33,11 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- LICENSE
|
35
35
|
- README.md
|
36
|
+
- lib/with_ease.rb
|
36
37
|
- lib/with_ease/base.rb
|
37
38
|
- lib/with_ease/closed_struct.rb
|
38
39
|
- lib/with_ease/closed_struct/hash_api.rb
|
39
|
-
- lib/with_ease/
|
40
|
+
- lib/with_ease/forward.rb
|
40
41
|
- lib/with_ease/import_base.rb
|
41
42
|
- lib/with_ease/iterator.rb
|
42
43
|
- lib/with_ease/open_struct.rb
|