valued 0.1.2 → 0.4.0

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: 32117338fc8ee6ce822e3e057ea7c8604982976b634a6807f4d6775f76a194c1
4
- data.tar.gz: 9b55bea9f75eb48836be435fd8e1a7afec2ec75b4ed5d1acb5dd33b04f3c0496
3
+ metadata.gz: c09b1d932113939667c5e36981e3256b25483679ebc222131d377985152a70a9
4
+ data.tar.gz: 823e69bdf5587363b1cae66ce2bd68ed8f38aedcdca3bddac5e59b953c27414f
5
5
  SHA512:
6
- metadata.gz: b7ba659cce34b7115079682145a1b67799f167fa0592c7ad46be1768e309536738a1abacf476ff3d13ee6b50613a4be4ddfc4c780fe509d0ea5398abc2b58ce7
7
- data.tar.gz: ab42f48189a070eb8b82336bc72d1b784218f47bd945759646f6d0ee36449c14799380bda760e6ade3a9f4b35b68636cb1ff5f0e81f1f2f0fc3828adcc77ee41
6
+ metadata.gz: 7c050eb350406595297da5f44fddd9f8b87e7675ad72596eaf89b8de9075e4da48f873da3750d01f7bd47f986c56efe5dd295900a17f94c1d81aee22459d482f
7
+ data.tar.gz: bd235452be8f966895982d83b31650bd468af5922e27d97680f04f95e8557cfd87f5c82ca982dc36880b0f2b2b767d9c56fb573ec6e42ed6396c16f433e183e1
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0
4
+
5
+ - added a `update` method to all `Valued` and `Valued::Mutable` objects that
6
+ allows creating a duplicate of an object with changed attributes
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- valued (0.1.2)
4
+ valued (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -82,6 +82,39 @@ p quantity
82
82
  => #<Quantity amount=2 unit="m">
83
83
  ```
84
84
 
85
+ You can create a duplicate of your object with updated attributes by using `update`.
86
+
87
+ ```ruby
88
+ quantity = Quantity.new(unit: 'm', amount: 2)
89
+ p quantity
90
+ => #<Quantity amount=2 unit="m">
91
+ updated_quantity = quantity.update(unit: 'yard')
92
+ p updated_quantity
93
+ => #<Quantity amount=2 unit="yard">
94
+ ```
95
+
96
+ If you really need a mutable object, just use `Valued::Mutable` instead of `Valued`.
97
+
98
+ ```ruby
99
+ require 'valued'
100
+
101
+ class Quantity
102
+ include Valued::Mutable
103
+
104
+ attributes :unit, :amount
105
+ end
106
+ ```
107
+
108
+ By doing that, you also get a setter for every attribute.
109
+
110
+ ```ruby
111
+ quantity = Quantity.new
112
+ quantity.amount = 2
113
+ quantity.unit = 'm'
114
+ p quantity
115
+ => #<Quantity amount=2 unit="m">
116
+ ```
117
+
85
118
  ## License
86
119
 
87
120
  The gem is available as open source under the terms of the
@@ -1,10 +1,16 @@
1
+ require 'valued/mutable'
1
2
  require 'valued/version'
2
3
 
3
4
  module Valued
4
5
  module ClassMethods
6
+ def self.define_method(attr, klass)
7
+ klass.class_eval { attr_reader attr }
8
+ end
9
+
5
10
  def attributes(*attributes)
6
- attributes.each { |attr| attr_reader attr }
11
+ attributes.each { |attr| Valued::ClassMethods.define_method(attr, self) }
7
12
  define_method('_attributes') { attributes }
13
+ private :_attributes
8
14
  end
9
15
  end
10
16
 
@@ -34,6 +40,14 @@ module Valued
34
40
  end
35
41
  end
36
42
 
43
+ def update(new_attributes)
44
+ self.class.new(
45
+ _attributes.each_with_object({}) do |attribute, result|
46
+ result[attribute] = new_attributes[attribute] || self.send(attribute)
47
+ end
48
+ )
49
+ end
50
+
37
51
  def ==(other)
38
52
  _attributes.all? do |attribute|
39
53
  other.respond_to?(attribute) && send(attribute) == other.send(attribute)
@@ -0,0 +1,80 @@
1
+ module Valued
2
+ module Mutable
3
+ module ClassMethods
4
+ def self.define_method(attr, klass)
5
+ klass.class_eval { attr_accessor attr }
6
+ end
7
+
8
+ def attributes(*attributes)
9
+ attributes.each do |attr|
10
+ Valued::Mutable::ClassMethods.define_method(attr, self)
11
+ end
12
+ define_method('_attributes') { attributes }
13
+ private :_attributes
14
+ end
15
+ end
16
+
17
+ def self.define(*attrs, &block)
18
+ klass =
19
+ Class.new do
20
+ include Valued::Mutable
21
+
22
+ attributes(*attrs)
23
+ end
24
+ klass.class_eval(&block) if block_given?
25
+ klass
26
+ end
27
+
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
31
+
32
+ def initialize(attributes = {})
33
+ _attributes.each do |attribute|
34
+ if attributes.key?(attribute)
35
+ instance_variable_set("@#{attribute}", attributes.fetch(attribute))
36
+ end
37
+ end
38
+ end
39
+
40
+ def update(new_attributes)
41
+ self.class.new(
42
+ _attributes.each_with_object({}) do |attribute, result|
43
+ result[attribute] = new_attributes[attribute] || self.send(attribute)
44
+ end
45
+ )
46
+ end
47
+
48
+ def ==(other)
49
+ _attributes.all? do |attribute|
50
+ other.respond_to?(attribute) && send(attribute) == other.send(attribute)
51
+ end
52
+ end
53
+
54
+ def eql?(other)
55
+ self.class == other.class && self == other
56
+ end
57
+
58
+ def hash
59
+ (_attributes.map { |attribute| send(attribute) } + [self.class]).hash
60
+ end
61
+
62
+ def to_h
63
+ _attributes.each_with_object({}) do |attribute, hash|
64
+ hash[attribute] = send(attribute)
65
+ end
66
+ end
67
+
68
+ def to_s
69
+ inspect
70
+ end
71
+
72
+ def inspect
73
+ inspected_attributes =
74
+ _attributes.map do |attribute|
75
+ "#{attribute}=#{send(attribute).inspect}"
76
+ end.join(' ')
77
+ "#<#{self.class} #{inspected_attributes}>"
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,3 @@
1
1
  module Valued
2
- VERSION = '0.1.2'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valued
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Mainz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-27 00:00:00.000000000 Z
11
+ date: 2020-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.38'
83
- description:
83
+ description:
84
84
  email:
85
85
  - mainz.mario@googlemail.com
86
86
  executables: []
@@ -91,12 +91,14 @@ files:
91
91
  - ".rspec"
92
92
  - ".rubocop.yml"
93
93
  - ".travis.yml"
94
+ - CHANGELOG.md
94
95
  - Gemfile
95
96
  - Gemfile.lock
96
97
  - LICENSE.txt
97
98
  - README.md
98
99
  - Rakefile
99
100
  - lib/valued.rb
101
+ - lib/valued/mutable.rb
100
102
  - lib/valued/version.rb
101
103
  - valued.gemspec
102
104
  homepage: https://github.com/mmainz/valued
@@ -105,7 +107,7 @@ licenses:
105
107
  metadata:
106
108
  homepage_uri: https://github.com/mmainz/valued
107
109
  source_code_uri: https://github.com/mmainz/valued
108
- post_install_message:
110
+ post_install_message:
109
111
  rdoc_options: []
110
112
  require_paths:
111
113
  - lib
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  version: '0'
122
124
  requirements: []
123
125
  rubygems_version: 3.1.2
124
- signing_key:
126
+ signing_key:
125
127
  specification_version: 4
126
128
  summary: A Ruby gem that makes it easy to create value objects.
127
129
  test_files: []