with_ease 0.0.0 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb2fe2e40c1ff0b1a90a49ac10de9b1ce58265af246dbf5827be968979cf9b7c
4
- data.tar.gz: 2339362eba09d95578f6335e747e3afe5b018f919160e7282ca5431836bc0ba9
3
+ metadata.gz: 1acaa86f823eeec13d846b6af27f39e94ec5bffa9d19fa81ce78994680efbfa5
4
+ data.tar.gz: 2c23c88589ed075d3289e27528e9d8ceb1acda6eeee0a45e6c21cae6a95240ef
5
5
  SHA512:
6
- metadata.gz: 588e084c2df28c6824fc52f61f03b2a922c29a0dd9d8b4a3321872a90ef4ce9679ec02c746d3b67c3592b9cd57e88786a64a8bc4a04ea91e150b0ae30f30c885
7
- data.tar.gz: 9333f074ca9fdd12e8a0d72ec37de12a8605506da317b72baef1326b1ac91c03c1d3f6cd757505e90c94739ac83587ba0ba9f90dbeaf30a0bdf2504df61e1d2f
6
+ metadata.gz: a6cb7b523b7481b4696242a2addd533cd08c158f6652e8e8ab11b2aa6cbeb262abfbde383a14ad51e3dda8013ff45ba431901fdcd075636aaf48d73c10aac6f8
7
+ data.tar.gz: 2f5c8b59bf12285100c0cabf7a592a55117a8b6c0e0e9d946141d14c76290c24c2770a8bc519210ac4b58cf3bf7c5b45a73292d570aec7e6f79d97a87f46937e
data/README.md CHANGED
@@ -1,16 +1,175 @@
1
- [![Gem Version](https://badge.fury.io/rb/lab42_base.svg)](http://badge.fury.io/rb/lab42_base)
2
- [![Gem Downloads](https://img.shields.io/gem/dt/lab42_base.svg)](https://rubygems.org/gems/lab42_base)
1
+ [![Gem Version](https://badge.fury.io/rb/with_ease.svg)](http://badge.fury.io/rb/with_ease)
2
+ [![Gem Downloads](https://img.shields.io/gem/dt/with_ease.svg)](https://rubygems.org/gems/with_ease)
3
3
 
4
- # lab42_base
4
+ # WithEase
5
5
 
6
- Finally extracting the minimum code I use literally everywhere...
6
+ Very basic but useful extensions to Ruby.
7
7
 
8
- ## Which Are
8
+ **N.B.:** still in alpha
9
9
 
10
- - [Extensions to `OpenStruct`](spec/open_struct)
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
+ #### Extend the `&:<method>` cludge from symbols to arrays with `Array#.to_proc`
55
+
56
+ My philosophy on this, is either use both or hate both
57
+
58
+ [c.f for details](speculations/array_to_proc.md)
59
+
60
+ ## Quick Start
61
+
62
+ All functionality is described in detail in the [speculations](speculations) (see links above for
63
+ the specific speculations of a functionality).
64
+
65
+ But here is a quick overview (also tested with the [speculate_about gem](https://rubygems.org/gems/speculate_about) as all other speculations).
66
+
67
+ ### Context: `OpenStruct`
68
+
69
+ Given
70
+ ```ruby
71
+ require 'with_ease'
72
+
73
+ let(:subject) { OpenStruct.new(a: 1, b: 2) }
74
+ ```
75
+
76
+ Then we can use it mostly like a `Hash` instance.
77
+ ```ruby
78
+ expect(subject.merge(a: 11, c: 31)).to eq(OpenStruct.new(a: 11, b: 2, c:31))
79
+ ```
80
+ And we can pattern match
81
+ ```ruby
82
+ subject => {a:, b: 2}
83
+ expect(a).to eq(1)
84
+ ```
85
+
86
+ ### Context: `ClosedStruct`
87
+
88
+ Given
89
+ ```ruby
90
+ require 'with_ease'
91
+ ClosedStruct = WithEase::ClosedStruct
92
+
93
+ let(:subject) { ClosedStruct.new(a: 1, b: 2) }
94
+ ```
95
+
96
+ Then we can do pretty much the same as with `OpenStruct`
97
+ ```ruby
98
+ subject.update(a: 0)
99
+ expect(subject).to eq(ClosedStruct.new(a: 0, b: 2))
100
+
101
+ ```
102
+
103
+ But you must not add a key
104
+ ```ruby
105
+ expect { subject.update(c: 3) }
106
+ .to raise_error(KeyError)
107
+ expect{ subject.merge(c: 3) }
108
+ .to raise_error(KeyError)
109
+ ```
110
+
111
+ [c.f for details](speculations/closed_struct.md)
112
+
113
+ ### Context: `Iterator`
114
+
115
+ As mentioned above you can ~easily~ (it's not complicated but it is not easy!) create
116
+ an iterator in Ruby, howver this one is so much simpler.
117
+
118
+ Given
119
+ ```ruby
120
+ require 'with_ease'
121
+ Iterator = WithEase::Iterator
122
+
123
+ let(:subject) { Iterator.new(0, &:succ) }
124
+ ```
125
+
126
+ Then we can just advance it
127
+ ```ruby
128
+ expect(subject.value).to be_zero
129
+ expect(subject.advance.value).to eq(1)
130
+ ```
131
+
132
+ It is an `Enumerable` of course and quite lazy, if you are intereted
133
+ [here is more](speculations/iterator.md)
134
+
135
+
136
+
137
+ [c.f for details](speculations/iterator.md)
138
+
139
+ ### Context: `Forward`
140
+
141
+ Given
142
+ ```ruby
143
+ require 'with_ease'
144
+
145
+ Forward = WithEase::Forward
146
+ ```
147
+
148
+ **N.B.**: We could have used `require 'with_ease/import_all'` instead.
149
+
150
+ And
151
+ ```ruby
152
+ class Wrapper
153
+ extend Forward
154
+ forward :empty?, :size, to: :@array
155
+ def initialize(array) = @array = array.to_a
156
+ end
157
+
158
+ let(:subject) { Wrapper.new(0..9) }
159
+ ```
160
+
161
+ Then we can see that
162
+ ```ruby
163
+ expect(subject).not_to be_empty
164
+ expect(subject.size).to eq(10)
165
+ ```
166
+ But also
167
+ ```ruby
168
+ expect(Wrapper.new([])).to be_empty
169
+ ```
170
+
171
+
172
+ [c.f. for details](speculations/forward.md)
14
173
 
15
174
  ## Author
16
175
 
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'array_to_proc'
4
+ require_relative 'closed_struct'
5
+ require_relative 'forward'
6
+ require_relative 'iterator'
7
+ require_relative 'open_struct'
8
+
9
+ ClosedStruct = WithEase::ClosedStruct
10
+ Forward = WithEase::Forward
11
+ Iterator = WithEase::Iterator
12
+
13
+ require_relative 'constructor_functions'
14
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Forwardable
4
- def forward *methods, to:
5
- def_delegators to, *methods
3
+ class Array
4
+ def to_proc
5
+ -> { it.send(*self) }
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'forwardable'
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 Forwardable
9
+ extend Forward
10
10
  include HashApi
11
11
 
12
12
  forward :deconstruct_keys, :fetch, :to_a, :to_h, to: :@data
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'closed_struct'
4
+ require_relative 'iterator'
5
+ require_relative 'open_struct'
6
+
7
+ def ClosedStruct(**k) = WithEase::ClosedStruct.new(**k)
8
+ def Iterator(init, &blk) = WithEase::Iterator.new(init, &blk)
9
+ def OpenStruct(**k) = WithEase::OpenStruct.new(**k)
10
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WithEase
4
+ module Forward
5
+ def self.extended(into) = into.extend Forwardable
6
+
7
+ def forward *methods, to:
8
+ def_delegators to, *methods
9
+ end
10
+ end
11
+ end
12
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -6,6 +6,11 @@ module WithEase
6
6
 
7
7
  attr_reader :advancer, :value
8
8
 
9
+ def advance = drop
10
+ def advance_fn = -> { drop }
11
+
12
+ def call = advance
13
+
9
14
  def drop(n=1)
10
15
  n.times do
11
16
  @value = @advancer.(value)
@@ -14,14 +19,28 @@ module WithEase
14
19
  end
15
20
  def drop_fn = -> (n=1) { drop n }
16
21
 
22
+ def drop_while(&blk)
23
+ loop do
24
+ return self unless blk.(value)
25
+ advance
26
+ end
27
+ end
28
+
29
+ def map(&blk)
30
+ lazy.map(&blk)
31
+ end
17
32
 
18
33
  def each
19
34
  loop do
20
35
  yield value
21
- drop
36
+ advance
22
37
  end
23
38
  end
39
+
24
40
  def take_fn = -> (n) { take n }
41
+ def take_while_fn = -> (&blk) { take_while(&blk) }
42
+
43
+ def to_proc = ->(*) { value }
25
44
 
26
45
  private
27
46
  def initialize(value, &advancer)
@@ -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 in block form" unless keys.empty?
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 'forwardable'
4
+ require_relative 'forward'
5
5
  require_relative 'open_struct/hash_api'
6
6
  class OpenStruct
7
- extend Forwardable
7
+ extend WithEase::Forward
8
8
  forward :deconstruct_keys, :fetch, :to_a, to: :to_h
9
9
  forward :deconstruct, to: :to_a
10
10
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WithEase
4
- VERSION = "0.0.0"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  # SPDX-License-Identifier: AGPL-3.0-or-later
data/lib/with_ease.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'with_ease/closed_struct'
4
+ require_relative 'with_ease/forward'
5
+ require_relative 'with_ease/iterator'
6
+ require_relative 'with_ease/open_struct'
7
+
8
+ # SPDX-License-Identifier: AGPL-3.0-or-later
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.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -33,10 +33,14 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - LICENSE
35
35
  - README.md
36
+ - lib/with_ease.rb
37
+ - lib/with_ease/all.rb
38
+ - lib/with_ease/array_to_proc.rb
36
39
  - lib/with_ease/base.rb
37
40
  - lib/with_ease/closed_struct.rb
38
41
  - lib/with_ease/closed_struct/hash_api.rb
39
- - lib/with_ease/forwardable.rb
42
+ - lib/with_ease/constructor_functions.rb
43
+ - lib/with_ease/forward.rb
40
44
  - lib/with_ease/import_base.rb
41
45
  - lib/with_ease/iterator.rb
42
46
  - lib/with_ease/open_struct.rb