ryo.rb 0.4.6 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -11
- data/lib/ryo/json.rb +41 -0
- data/lib/ryo/{lazy.rb → memo.rb} +1 -1
- data/lib/ryo/reflect.rb +4 -3
- data/lib/ryo/version.rb +1 -1
- data/lib/ryo.rb +12 -8
- data/spec/readme_spec.rb +2 -2
- data/spec/ryo_basic_object_spec.rb +7 -0
- data/spec/ryo_json_spec.rb +26 -0
- data/spec/ryo_object_spec.rb +7 -0
- data/spec/ryo_spec.rb +16 -0
- metadata +6 -4
- /data/share/ryo.rb/examples/{7_ryo_lazy.rb → 7_ryo_memo.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63de7b5fef56bfd65a818a1ee0b37617f90778c51f53688fa603c76b25bbfd47
|
4
|
+
data.tar.gz: 574f61630e3e966f85a73e149250741807ee0bebef9d69dd072a5e612eeabe3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e5fdc169813d8ac506f06a3d5cf3adeeda8b693d4dedd8a85d0944405107579c5fdf10d1a3b21729cba5a20c244c94b32794d797ceb2f1408ff97b4ad37e271
|
7
|
+
data.tar.gz: 01dc1c068bc3c565d90b4662678e9bd7d59a508488c80bbcfcf09652f481a7a4df4fb779c50683440ebff5615c7ac6b138712298a5e99f7abf8859f1dd7f55d8
|
data/README.md
CHANGED
@@ -55,21 +55,19 @@ p point.multiply.call(2)
|
|
55
55
|
# [10, 20]
|
56
56
|
```
|
57
57
|
|
58
|
-
#### Ryo.
|
58
|
+
#### Ryo.memo
|
59
59
|
|
60
|
-
The following example demonstrates
|
61
|
-
[`Ryo.
|
62
|
-
|
63
|
-
for the first time. It is similar to a Ryo function
|
64
|
-
that the `#call` method be used, and after the property is accessed for the
|
65
|
-
first time the lazy value is replaced by the evaluated value:
|
60
|
+
The following example demonstrates
|
61
|
+
[`Ryo.memo`](https://0x1eef.github.io/x/ryo.rb/Ryo.html#memo-class_method).
|
62
|
+
`Ryo.memo` returns a value that becomes memoized after a property is
|
63
|
+
accessed for the first time. It is similar to a Ryo function:
|
66
64
|
|
67
65
|
```ruby
|
68
66
|
require "ryo"
|
69
67
|
|
70
|
-
point_x = Ryo(x: Ryo.
|
71
|
-
point_y = Ryo({y: Ryo.
|
72
|
-
point = Ryo({sum: Ryo.
|
68
|
+
point_x = Ryo(x: Ryo.memo { 5 })
|
69
|
+
point_y = Ryo({y: Ryo.memo { 10 }}, point_x)
|
70
|
+
point = Ryo({sum: Ryo.memo { x + y }}, point_y)
|
73
71
|
print "point.x = ", point.x, "\n"
|
74
72
|
print "point.y = ", point.y, "\n"
|
75
73
|
print "point.sum = ", point.sum, "\n"
|
@@ -359,7 +357,7 @@ are available as sources.
|
|
359
357
|
|
360
358
|
```ruby
|
361
359
|
# Gemfile
|
362
|
-
gem "ryo.rb", github: "0x1eef/ryo.rb", tag: "v0.4.
|
360
|
+
gem "ryo.rb", github: "0x1eef/ryo.rb", tag: "v0.4.7"
|
363
361
|
```
|
364
362
|
|
365
363
|
**Rubygems.org**
|
data/lib/ryo/json.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
##
|
2
|
+
# The {Ryo::JSON Ryo::JSON} module provides a number of methods
|
3
|
+
# for coercing JSON data into a Ryo object. It must be required
|
4
|
+
# separately to Ryo (ie: require "ryo/json"), and the methods of
|
5
|
+
# this module are then available on the {Ryo Ryo} module.
|
6
|
+
module Ryo::JSON
|
7
|
+
require "json"
|
8
|
+
extend self
|
9
|
+
|
10
|
+
##
|
11
|
+
# @param [String] path
|
12
|
+
# The path to a JSON file.
|
13
|
+
#
|
14
|
+
# @param [Ryo] object
|
15
|
+
# {Ryo::Object Ryo::Object}, or {Ryo::BasicObject Ryo::BasicObject}.
|
16
|
+
# Defaults to {Ryo::Object Ryo::Object}.
|
17
|
+
#
|
18
|
+
# @raise [SystemCallError]
|
19
|
+
# Might raise a number of Errno exceptions.
|
20
|
+
#
|
21
|
+
# @return [Ryo::Object, Ryo::BasicObject]
|
22
|
+
# Returns a Ryo object.
|
23
|
+
def from_json_file(path, object: Ryo::Object)
|
24
|
+
from_json File.binread(path), object:
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# @param [String] blob
|
29
|
+
# A blob of JSON.
|
30
|
+
#
|
31
|
+
# @param [Ryo] object
|
32
|
+
# {Ryo::Object Ryo::Object}, or {Ryo::BasicObject Ryo::BasicObject}.
|
33
|
+
# Defaults to {Ryo::Object Ryo::Object}.
|
34
|
+
#
|
35
|
+
# @return (see Ryo::JSON#from_json_file)
|
36
|
+
def from_json(blob, object: Ryo::Object)
|
37
|
+
object.from JSON.parse(blob)
|
38
|
+
end
|
39
|
+
|
40
|
+
Ryo.extend(self)
|
41
|
+
end
|
data/lib/ryo/{lazy.rb → memo.rb}
RENAMED
data/lib/ryo/reflect.rb
CHANGED
@@ -251,10 +251,11 @@ module Ryo::Reflect
|
|
251
251
|
#
|
252
252
|
# @return [Boolean]
|
253
253
|
# Returns true when the given object is an instance
|
254
|
-
# of {Ryo::
|
255
|
-
def
|
256
|
-
Ryo::
|
254
|
+
# of {Ryo::Memo Ryo::Memo}.
|
255
|
+
def memo?(obj)
|
256
|
+
Ryo::Memo === obj
|
257
257
|
end
|
258
|
+
alias_method :lazy?, :memo?
|
258
259
|
|
259
260
|
##
|
260
261
|
# @example
|
data/lib/ryo/version.rb
CHANGED
data/lib/ryo.rb
CHANGED
@@ -21,7 +21,7 @@ module Ryo
|
|
21
21
|
require_relative "ryo/basic_object"
|
22
22
|
require_relative "ryo/object"
|
23
23
|
require_relative "ryo/function"
|
24
|
-
require_relative "ryo/
|
24
|
+
require_relative "ryo/memo"
|
25
25
|
require_relative "ryo/enumerable"
|
26
26
|
|
27
27
|
extend Ryo::Reflect
|
@@ -61,15 +61,18 @@ module Ryo
|
|
61
61
|
end
|
62
62
|
|
63
63
|
##
|
64
|
-
# Creates a
|
64
|
+
# Creates a memoized Ryo value.
|
65
65
|
#
|
66
66
|
# @param [Proc] b
|
67
|
-
# A
|
67
|
+
# A Proc that is memoized after being accessed for the first time.
|
68
68
|
#
|
69
|
-
# @return [Ryo::
|
70
|
-
# Returns an instance of {Ryo::
|
71
|
-
def self.
|
72
|
-
Ryo::
|
69
|
+
# @return [Ryo::Memo]
|
70
|
+
# Returns an instance of {Ryo::Memo Ryo::Memo}.
|
71
|
+
def self.memo(&b)
|
72
|
+
Ryo::Memo.new(&b)
|
73
|
+
end
|
74
|
+
class << Ryo
|
75
|
+
alias_method :lazy, :memo
|
73
76
|
end
|
74
77
|
|
75
78
|
##
|
@@ -107,7 +110,7 @@ module Ryo
|
|
107
110
|
property = property.to_s
|
108
111
|
if Ryo.property?(self, property)
|
109
112
|
v = @_table[property]
|
110
|
-
Ryo.
|
113
|
+
Ryo.memo?(v) ? self[property] = v.call : v
|
111
114
|
else
|
112
115
|
return unless @_proto
|
113
116
|
Ryo.call_method(@_proto, property)
|
@@ -158,6 +161,7 @@ module Ryo
|
|
158
161
|
def to_h
|
159
162
|
Ryo.table_of(self, recursive: true)
|
160
163
|
end
|
164
|
+
alias_method :to_hash, :to_h
|
161
165
|
|
162
166
|
##
|
163
167
|
# @private
|
data/spec/readme_spec.rb
CHANGED
@@ -72,8 +72,8 @@ RSpec.describe "README.md examples" do
|
|
72
72
|
it { is_expected.to eq("5\n10") }
|
73
73
|
end
|
74
74
|
|
75
|
-
context "when given
|
76
|
-
let(:file) { "
|
75
|
+
context "when given 7_ryo_memo.rb" do
|
76
|
+
let(:file) { "7_ryo_memo.rb" }
|
77
77
|
it { is_expected.to eq("point.x = 5\npoint.y = 10\npoint.sum = 15") }
|
78
78
|
end
|
79
79
|
end
|
@@ -64,5 +64,12 @@ RSpec.describe Ryo::BasicObject do
|
|
64
64
|
it { expect(h).to be_instance_of(Hash) }
|
65
65
|
it { expect(h["wheels"]).to be_instance_of(Hash) }
|
66
66
|
it { expect(h).to eq({"name" => "ford", "wheels" => {"quantity" => 4}}) }
|
67
|
+
|
68
|
+
context "when given to Hash#merge" do
|
69
|
+
subject { {}.merge(car) }
|
70
|
+
let(:car) { Ryo(name: "ford") }
|
71
|
+
it { is_expected.to be_instance_of(Hash) }
|
72
|
+
it { is_expected.to eq({"name" => "ford"}) }
|
73
|
+
end
|
67
74
|
end
|
68
75
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "setup"
|
4
|
+
require "ryo/json"
|
5
|
+
require "fileutils"
|
6
|
+
|
7
|
+
RSpec.describe Ryo::JSON do
|
8
|
+
describe ".from_json_file" do
|
9
|
+
subject(:ryo) { described_class.from_json_file(path, object:) }
|
10
|
+
before { File.binwrite path, JSON.dump(x: 20, y: 40) }
|
11
|
+
after { FileUtils.rm(path) }
|
12
|
+
let(:path) { File.join(__dir__, "test.json") }
|
13
|
+
|
14
|
+
context "with Ryo::Object" do
|
15
|
+
let(:object) { Ryo::Object }
|
16
|
+
it { is_expected.to be_instance_of(Ryo::Object) }
|
17
|
+
it { is_expected.to eq("x" => 20, "y" => 40) }
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with Ryo::BasicObject" do
|
21
|
+
let(:object) { Ryo::BasicObject }
|
22
|
+
it { expect(Ryo::BasicObject === ryo).to be(true) }
|
23
|
+
it { is_expected.to eq("x" => 20, "y" => 40) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/ryo_object_spec.rb
CHANGED
@@ -59,6 +59,13 @@ RSpec.describe "Ryo objects" do
|
|
59
59
|
it { expect(h).to be_instance_of(Hash) }
|
60
60
|
it { expect(h["wheels"]).to be_instance_of(Hash) }
|
61
61
|
it { expect(h).to eq({"name" => "ford", "wheels" => {"quantity" => 4}}) }
|
62
|
+
|
63
|
+
context "when given to Hash#merge" do
|
64
|
+
subject { {}.merge(car) }
|
65
|
+
let(:car) { Ryo(name: "ford") }
|
66
|
+
it { is_expected.to be_instance_of(Hash) }
|
67
|
+
it { is_expected.to eq({"name" => "ford"}) }
|
68
|
+
end
|
62
69
|
end
|
63
70
|
|
64
71
|
describe "when a property overshadows a method" do
|
data/spec/ryo_spec.rb
CHANGED
@@ -127,4 +127,20 @@ RSpec.describe Ryo do
|
|
127
127
|
it { is_expected.to eq(true) }
|
128
128
|
end
|
129
129
|
end
|
130
|
+
|
131
|
+
describe ".memo" do
|
132
|
+
let(:point) do
|
133
|
+
Ryo(x: Ryo.memo { "5".dup }, y: Ryo.memo { "10".dup })
|
134
|
+
end
|
135
|
+
|
136
|
+
it "returns a value" do
|
137
|
+
expect(point.x).to eq("5")
|
138
|
+
expect(point.y).to eq("10")
|
139
|
+
end
|
140
|
+
|
141
|
+
it "memoizes a value" do
|
142
|
+
expect(point.x).to equal(point.x)
|
143
|
+
expect(point.y).to equal(point.y)
|
144
|
+
end
|
145
|
+
end
|
130
146
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ryo.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- '0x1eef'
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|
@@ -116,8 +116,9 @@ files:
|
|
116
116
|
- lib/ryo/builder.rb
|
117
117
|
- lib/ryo/enumerable.rb
|
118
118
|
- lib/ryo/function.rb
|
119
|
+
- lib/ryo/json.rb
|
119
120
|
- lib/ryo/keywords.rb
|
120
|
-
- lib/ryo/
|
121
|
+
- lib/ryo/memo.rb
|
121
122
|
- lib/ryo/object.rb
|
122
123
|
- lib/ryo/reflect.rb
|
123
124
|
- lib/ryo/version.rb
|
@@ -134,11 +135,12 @@ files:
|
|
134
135
|
- share/ryo.rb/examples/4.1_basicobject_ryo_basicobject_from.rb
|
135
136
|
- share/ryo.rb/examples/5_collisions_resolution_strategy.rb
|
136
137
|
- share/ryo.rb/examples/6_beyond_hash_objects.rb
|
137
|
-
- share/ryo.rb/examples/
|
138
|
+
- share/ryo.rb/examples/7_ryo_memo.rb
|
138
139
|
- share/ryo.rb/examples/setup.rb
|
139
140
|
- spec/readme_spec.rb
|
140
141
|
- spec/ryo_basic_object_spec.rb
|
141
142
|
- spec/ryo_enumerable_spec.rb
|
143
|
+
- spec/ryo_json_spec.rb
|
142
144
|
- spec/ryo_keywords_spec.rb
|
143
145
|
- spec/ryo_object_spec.rb
|
144
146
|
- spec/ryo_prototypes_spec.rb
|
File without changes
|