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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e90c5b42d1b364a6be77786df22b887017d3ce4965822f7152af7bcb1c617748
4
- data.tar.gz: ec5d807404158d8dd1e9df47ee1fa15fb6be64ae2086942fbe9c1fd834be7331
3
+ metadata.gz: 63de7b5fef56bfd65a818a1ee0b37617f90778c51f53688fa603c76b25bbfd47
4
+ data.tar.gz: 574f61630e3e966f85a73e149250741807ee0bebef9d69dd072a5e612eeabe3d
5
5
  SHA512:
6
- metadata.gz: ef8f408777db206d7fa5e3c7ced8d8a5fdfdaa15ad6e39f25921de20c421f0bef90d1aa1e474cbc6f6ab23d17fa314bd6513d3b9e322732d4adf21fb6c76b9c9
7
- data.tar.gz: db93fad9aa4eeffdecbba86fa2ec970851eb007d3fecf0602e631584091f28d06dd3997464306267012bf893319f5209813b8cb99024e0281a5048da39f2a0a5
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.lazy
58
+ #### Ryo.memo
59
59
 
60
- The following example demonstrates a lazy Ryo value.
61
- [`Ryo.lazy`](https://0x1eef.github.io/x/ryo.rb/Ryo.html#lazy-class_method)
62
- creates a lazy value that is not evaluated until a property is accessed
63
- for the first time. It is similar to a Ryo function but it does not require
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.lazy { 5 })
71
- point_y = Ryo({y: Ryo.lazy { 10 }}, point_x)
72
- point = Ryo({sum: Ryo.lazy { x + y }}, point_y)
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.6"
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
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Ryo::Lazy < Ryo::Function
3
+ class Ryo::Memo < Ryo::Function
4
4
  end
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::Lazy Ryo::Lazy}.
255
- def lazy?(obj)
256
- Ryo::Lazy === obj
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ryo
4
- VERSION = "0.4.6"
4
+ VERSION = "0.5.0"
5
5
  end
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/lazy"
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 lazy Ryo value.
64
+ # Creates a memoized Ryo value.
65
65
  #
66
66
  # @param [Proc] b
67
- # A proc that is evaluated when a property is first accessed.
67
+ # A Proc that is memoized after being accessed for the first time.
68
68
  #
69
- # @return [Ryo::Lazy]
70
- # Returns an instance of {Ryo::Lazy Ryo::Lazy}.
71
- def self.lazy(&b)
72
- Ryo::Lazy.new(&b)
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.lazy?(v) ? self[property] = v.call : v
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 7_ryo_lazy.rb" do
76
- let(:file) { "7_ryo_lazy.rb" }
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
@@ -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.6
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-02-01 00:00:00.000000000 Z
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/lazy.rb
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/7_ryo_lazy.rb
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