ruby-optics 0.0.1 → 0.1.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: c5a90f6ad934879e9b5522055637df2dc1386b6931c8c69ab7474f5dd1ba5591
4
- data.tar.gz: f165845f3ac79940f54071fa66dcbf673157af38dd69ae0094b89acb1c89326c
3
+ metadata.gz: 940e5fe97670b6b7f24cc4bc52036f68ae81655746d5c869a572738ee063be36
4
+ data.tar.gz: 83b101c570650bb94bea1da8bee87031397ff2ad2947beca8cab16a95cce541b
5
5
  SHA512:
6
- metadata.gz: 70ee1a8f3f1be4e7f4401c937dea535e19e85f7d39d6e54418fb48c6df93c4d81ac1e5bb1748df0ab6c1f033b6550d96b5a9073256643cd30c45d089b825c99b
7
- data.tar.gz: 0f9b3bd70954701427f663839c56a1ae00f35f1592efbf07a87bb956c41ae8938fab9f9df9a484531fe8e6b3884d526ddca379151a2d7cd70f41585063fae2b1
6
+ metadata.gz: ff68227c0e6702687bbe1c2bb7fa47a91fcf6ed96121f0405e846030466b498bdcac482899d4c5563129f82ff64d0c2c4e669355789fc3d5930eb62bab1a2164
7
+ data.tar.gz: fd2f4c09fa8ad9231f06ebc2b1b2823afd662d94096f95f69f010e962d146dff9bdb537178cff27dcb523d2cd5d7ed6cb604c695855cec29ab738c55edf36084
data/README.md CHANGED
@@ -2,13 +2,102 @@
2
2
 
3
3
  Simple library with common functional optics for Ruby.
4
4
 
5
- ## Links
6
-
7
5
  ## Supported Ruby versions
8
6
 
9
7
  This library officially supports the following Ruby versions:
10
8
 
11
- * MRI >= `2.5.1`
9
+ * MRI = `2.5.0`
10
+
11
+ ## Installation
12
+
13
+ Add this to your Gemfile
14
+ ```ruby
15
+ gem 'ruby-optics'
16
+ ```
17
+
18
+ and then run
19
+
20
+ ```
21
+ bundle install
22
+ ```
23
+
24
+ Or install it manually
25
+
26
+ ```
27
+ gem install ruby-optics
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ Lens - is an object which encapsulates getter/setter functionality for immutable data in composable way.
33
+ Any lens consist of two functions `set` and `get`. `get` describes how can retrieve a value from a whole object, and `set` - how to update this value without mutating the object.
34
+
35
+ Example for getting/setting value by key from hash:
36
+ ```ruby
37
+ def hash_get(key)
38
+ -> (hash) { hash[key] }
39
+ end
40
+
41
+ def hash_set(key)
42
+ -> (new_value, hash) { hash.merge(key => new_value) }
43
+ end
44
+ ```
45
+
46
+ To create lens you need to specify both these functions:
47
+
48
+ ```ruby
49
+ lens = Lens.new(hash_get(:foo), hash_set(:foo))
50
+ ```
51
+
52
+ Then you can use the lens to retrieve, set and modify value by the key in arbitrary hash:
53
+
54
+ ```ruby
55
+ object = { foo: 1, bar: 2 }
56
+
57
+ lens.get(object)
58
+ => 2
59
+
60
+ lens.set(3, object)
61
+ => { foo: 3, bar: 2 }
62
+
63
+ lens.modify(object) { |foo| foo * 10 }
64
+ => { foo: 10, bar: 2 }
65
+
66
+ object
67
+ => { foo: 1, bar: 2 }
68
+ ```
69
+
70
+ Note that `set` and `modify` operations don't mutate original value, thus keeping it unchanged.
71
+
72
+ The real power of lens comes from their compositional properties, which allows to creates lenses that works with arbitrary nested
73
+ objects by composing them from simpler ones.
74
+
75
+ ```ruby
76
+ foo_lens = Lens.new(hash_get(:foo), hash_set(:foo))
77
+ bar_lens = Lens.new(hash_get(:bar), hash_set(:bar))
78
+
79
+ lens = foo_lens.compose_lens(bar_lens)
80
+
81
+ object = { foo: { bar: 1 } }
82
+
83
+ lens.get(object)
84
+ => 1
85
+
86
+ lens.set(3, object)
87
+ => { foo: { bar: 3 } }
88
+
89
+ lens.modify(object) { |foo| foo * 10 }
90
+ => { foo: { bar: 10 } }
91
+
92
+ object
93
+ => { foo: { bar: 1 } }
94
+ ```
95
+
96
+ The library provides lens constructors for common cases, so you don't need to composes lenses yourself
97
+ ```ruby
98
+ lens = Optics.hash_lens(:foo, :bar)
99
+ ```
100
+
12
101
 
13
102
  ## License
14
103
 
@@ -4,5 +4,5 @@ require 'ruby-optics/optics'
4
4
  require 'ruby-optics/record/record'
5
5
 
6
6
  module Optics
7
- VERSION = "0.0.1"
7
+ VERSION = "0.1.0"
8
8
  end
@@ -12,10 +12,11 @@ module Record
12
12
  attribute_name = defined_attribute_params[:attribute_name]
13
13
  attribute_argument = args_hash[attribute_name]
14
14
  if attribute_argument.nil?
15
- if defined_attribute_params[:nullable]
15
+ default_value = defined_attribute_params[:default]
16
+ if defined_attribute_params[:nullable] || default_value
16
17
  instance_variable_set(
17
18
  :"@#{attribute_name}",
18
- defined_attribute_params[:default]
19
+ default_value
19
20
  )
20
21
  else
21
22
  raise ArgumentError.new(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-optics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniil Bober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-06 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler