valued 0.2.0 → 0.4.1

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: f51821642a08c2513844a7c47456c28b6b7a547796574178b8da0c109ab82712
4
- data.tar.gz: f54ccf4641b836351d2ee0d48b98f91284718786cd070596cb75768f43b552e2
3
+ metadata.gz: 01e8a133b1b6b38c5ad222c7d03a3ea68ca57925700a81c305dc9d66b8cd83a9
4
+ data.tar.gz: a03c152ff273d2ba821a1f96d3bc456b011b78908ccddd2f54ad0e1f8b041765
5
5
  SHA512:
6
- metadata.gz: 40d747b3afeaf897065822bf5346657405eaffe250147a8f2ae7ef24cbf1ad8fa172fcecc34b1d5af9d84fb8b8db27003aff43b43f37325bb0df35be42f226c0
7
- data.tar.gz: 9d33834008135bfb88116bfa7d090613993a8f8645b715487a124bd61a13df974a82017616566e481df3727227e89b7d4c7f0d812f5dfbe49c81d314800447ad
6
+ metadata.gz: 456eb9787c0b033387957c1cd516ccf1e17a177eb6e4b9878351a38424d96ac309e1eadfc79b4db943d0ed213edbfcb07b9089ed0a03aadc03d25a7bc467c7f0
7
+ data.tar.gz: 5cea8f73ca85b74825ef922a21a684968195a88ac5466b9d33e8be2b27391c3a454494ce5364cd60c84c2ec44ac0b4e6c0de684f16d1d8a7522f58f66043d2ce
@@ -0,0 +1,35 @@
1
+ name: Continuous Integration
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+ branches: [master]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ["3.0"]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v2
18
+ - name: Setup Node.js 🖲
19
+ uses: actions/setup-node@v2.4.0
20
+ with:
21
+ node-version: 16.x
22
+ check-latest: true
23
+ - run: npm ci
24
+
25
+ - name: Check formatting 🔍
26
+ run: npm run lint
27
+
28
+ - name: Setup Ruby 💎
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby-version }}
32
+ bundler-cache: true
33
+
34
+ - name: Run tests ⚙️
35
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ node_modules
data/.tool-versions ADDED
@@ -0,0 +1,2 @@
1
+ ruby 3.0.2
2
+ nodejs 16.6.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## 0.4.1
4
+
5
+ - fix bug that prevented setting an attribute to `nil` with `update`
6
+
7
+ ## 0.4.0
8
+
9
+ - remove possibility to use attributes with question marks
10
+
11
+ ## 0.3.0
12
+
13
+ - added a `update` method to all `Valued` and `Valued::Mutable` objects that allows creating a duplicate of an object with changed attributes
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- valued (0.2.0)
4
+ valued (0.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -14,7 +14,7 @@ GEM
14
14
  ast (~> 2.4.0)
15
15
  rainbow (3.0.0)
16
16
  rake (13.0.1)
17
- rexml (3.2.4)
17
+ rexml (3.2.5)
18
18
  rspec (3.9.0)
19
19
  rspec-core (~> 3.9.0)
20
20
  rspec-expectations (~> 3.9.0)
@@ -53,4 +53,4 @@ DEPENDENCIES
53
53
  valued!
54
54
 
55
55
  BUNDLED WITH
56
- 2.1.4
56
+ 2.2.22
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
data/lib/valued.rb CHANGED
@@ -1,30 +1,11 @@
1
+ require 'valued/mutable'
1
2
  require 'valued/version'
2
3
 
3
4
  module Valued
4
5
  module ClassMethods
5
- def self.normalized_attributes(attributes)
6
- attributes.each_with_object(attributes) do |attr, result|
7
- result << attr.to_s.chomp('?').to_sym if attr.to_s.end_with?('?')
8
- end
9
- end
10
-
11
- def self.define_method(attr, klass)
12
- klass.class_eval do
13
- if attr.to_s.end_with?('?')
14
- define_method(attr) do
15
- instance_variable_get("@#{attr.to_s.chomp('?')}")
16
- end
17
- else
18
- attr_reader attr
19
- end
20
- end
21
- end
22
-
23
6
  def attributes(*attributes)
24
- attributes.each { |attr| Valued::ClassMethods.define_method(attr, self) }
25
- define_method('_attributes') do
26
- Valued::ClassMethods.normalized_attributes(attributes)
27
- end
7
+ attributes.each { |attribute| self.class_eval { attr_reader attribute } }
8
+ define_method('_attributes') { attributes }
28
9
  private :_attributes
29
10
  end
30
11
  end
@@ -48,13 +29,25 @@ module Valued
48
29
  _attributes.each do |attribute|
49
30
  if attributes.key?(attribute)
50
31
  instance_variable_set(
51
- "@#{attribute.to_s.chomp('?')}",
32
+ "@#{attribute}",
52
33
  attributes.fetch(attribute).freeze
53
34
  )
54
35
  end
55
36
  end
56
37
  end
57
38
 
39
+ def update(new_attributes)
40
+ self.class.new(
41
+ _attributes.each_with_object({}) do |attribute, result|
42
+ if new_attributes.key?(attribute)
43
+ result[attribute] = new_attributes[attribute]
44
+ else
45
+ result[attribute] = self.send(attribute)
46
+ end
47
+ end
48
+ )
49
+ end
50
+
58
51
  def ==(other)
59
52
  _attributes.all? do |attribute|
60
53
  other.respond_to?(attribute) && send(attribute) == other.send(attribute)
@@ -81,9 +74,9 @@ module Valued
81
74
 
82
75
  def inspect
83
76
  inspected_attributes =
84
- _attributes.map do |attribute|
85
- "#{attribute}=#{send(attribute).inspect}"
86
- end.join(' ')
77
+ _attributes
78
+ .map { |attribute| "#{attribute}=#{send(attribute).inspect}" }
79
+ .join(' ')
87
80
  "#<#{self.class} #{inspected_attributes}>"
88
81
  end
89
82
  end
@@ -1,32 +1,11 @@
1
1
  module Valued
2
2
  module Mutable
3
3
  module ClassMethods
4
- def self.normalized_attributes(attributes)
5
- attributes.each_with_object(attributes) do |attr, result|
6
- result << attr.to_s.chomp('?').to_sym if attr.to_s.end_with?('?')
7
- end
8
- end
9
-
10
- def self.define_method(attr, klass)
11
- klass.class_eval do
12
- if attr.to_s.end_with?('?')
13
- define_method(attr) do
14
- instance_variable_get("@#{attr.to_s.chomp('?')}")
15
- end
16
- attr_writer "#{attr.to_s.chomp('?')}".to_sym
17
- else
18
- attr_accessor attr
19
- end
20
- end
21
- end
22
-
23
4
  def attributes(*attributes)
24
- attributes.each do |attr|
25
- Valued::Mutable::ClassMethods.define_method(attr, self)
26
- end
27
- define_method('_attributes') do
28
- Valued::Mutable::ClassMethods.normalized_attributes(attributes)
5
+ attributes.each do |attribute|
6
+ self.class_eval { attr_accessor attribute }
29
7
  end
8
+ define_method('_attributes') { attributes }
30
9
  private :_attributes
31
10
  end
32
11
  end
@@ -49,14 +28,23 @@ module Valued
49
28
  def initialize(attributes = {})
50
29
  _attributes.each do |attribute|
51
30
  if attributes.key?(attribute)
52
- instance_variable_set(
53
- "@#{attribute.to_s.chomp('?')}",
54
- attributes.fetch(attribute)
55
- )
31
+ instance_variable_set("@#{attribute}", attributes.fetch(attribute))
56
32
  end
57
33
  end
58
34
  end
59
35
 
36
+ def update(new_attributes)
37
+ self.class.new(
38
+ _attributes.each_with_object({}) do |attribute, result|
39
+ if new_attributes.key?(attribute)
40
+ result[attribute] = new_attributes[attribute]
41
+ else
42
+ result[attribute] = self.send(attribute)
43
+ end
44
+ end
45
+ )
46
+ end
47
+
60
48
  def ==(other)
61
49
  _attributes.all? do |attribute|
62
50
  other.respond_to?(attribute) && send(attribute) == other.send(attribute)
@@ -83,9 +71,9 @@ module Valued
83
71
 
84
72
  def inspect
85
73
  inspected_attributes =
86
- _attributes.map do |attribute|
87
- "#{attribute}=#{send(attribute).inspect}"
88
- end.join(' ')
74
+ _attributes
75
+ .map { |attribute| "#{attribute}=#{send(attribute).inspect}" }
76
+ .join(' ')
89
77
  "#<#{self.class} #{inspected_attributes}>"
90
78
  end
91
79
  end
@@ -1,3 +1,3 @@
1
1
  module Valued
2
- VERSION = '0.2.0'
2
+ VERSION = '0.4.1'
3
3
  end
data/package-lock.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "valued",
3
+ "lockfileVersion": 2,
4
+ "requires": true,
5
+ "packages": {
6
+ "": {
7
+ "devDependencies": {
8
+ "@prettier/plugin-ruby": "^1.6.1",
9
+ "prettier": "^2.3.2"
10
+ }
11
+ },
12
+ "node_modules/@prettier/plugin-ruby": {
13
+ "version": "1.6.1",
14
+ "resolved": "https://registry.npmjs.org/@prettier/plugin-ruby/-/plugin-ruby-1.6.1.tgz",
15
+ "integrity": "sha512-PGDCATgVTQz0s/NB9nStiXVCIr+hG/XnKeAO/kguaHrNf8VwCpP5Ul+/KQao0z0QFBy2PDY8kWptfDQCa7WMXg==",
16
+ "dev": true,
17
+ "dependencies": {
18
+ "prettier": ">=1.10"
19
+ }
20
+ },
21
+ "node_modules/prettier": {
22
+ "version": "2.3.2",
23
+ "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz",
24
+ "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==",
25
+ "dev": true,
26
+ "bin": {
27
+ "prettier": "bin-prettier.js"
28
+ },
29
+ "engines": {
30
+ "node": ">=10.13.0"
31
+ }
32
+ }
33
+ },
34
+ "dependencies": {
35
+ "@prettier/plugin-ruby": {
36
+ "version": "1.6.1",
37
+ "resolved": "https://registry.npmjs.org/@prettier/plugin-ruby/-/plugin-ruby-1.6.1.tgz",
38
+ "integrity": "sha512-PGDCATgVTQz0s/NB9nStiXVCIr+hG/XnKeAO/kguaHrNf8VwCpP5Ul+/KQao0z0QFBy2PDY8kWptfDQCa7WMXg==",
39
+ "dev": true,
40
+ "requires": {
41
+ "prettier": ">=1.10"
42
+ }
43
+ },
44
+ "prettier": {
45
+ "version": "2.3.2",
46
+ "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz",
47
+ "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==",
48
+ "dev": true
49
+ }
50
+ }
51
+ }
data/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "valued",
3
+ "scripts": {
4
+ "prettify": "prettier \"**/*.{ru,rb,yml,yaml,md}\" --ignore-path=\".gitignore\"",
5
+ "lint": "npm run prettify -- --check",
6
+ "format": "npm run prettify -- --write"
7
+ },
8
+ "devDependencies": {
9
+ "@prettier/plugin-ruby": "^1.6.1",
10
+ "prettier": "^2.3.2"
11
+ }
12
+ }
data/valued.gemspec CHANGED
@@ -17,10 +17,11 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.files =
19
19
  Dir.chdir(File.expand_path('..', __FILE__)) do
20
- `git ls-files -z`.split("\x0").reject do |f|
21
- f.match(%r{^(test|spec|features)/})
22
- end
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
23
22
  end
23
+ end
24
+
24
25
  spec.bindir = 'exe'
25
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
27
  spec.require_paths = %w[lib]
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.2.0
4
+ version: 0.4.1
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-04-16 00:00:00.000000000 Z
11
+ date: 2021-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,17 +80,19 @@ 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: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
+ - ".github/workflows/ci.yml"
90
91
  - ".gitignore"
91
92
  - ".rspec"
92
93
  - ".rubocop.yml"
93
- - ".travis.yml"
94
+ - ".tool-versions"
95
+ - CHANGELOG.md
94
96
  - Gemfile
95
97
  - Gemfile.lock
96
98
  - LICENSE.txt
@@ -99,6 +101,8 @@ files:
99
101
  - lib/valued.rb
100
102
  - lib/valued/mutable.rb
101
103
  - lib/valued/version.rb
104
+ - package-lock.json
105
+ - package.json
102
106
  - valued.gemspec
103
107
  homepage: https://github.com/mmainz/valued
104
108
  licenses:
@@ -106,7 +110,7 @@ licenses:
106
110
  metadata:
107
111
  homepage_uri: https://github.com/mmainz/valued
108
112
  source_code_uri: https://github.com/mmainz/valued
109
- post_install_message:
113
+ post_install_message:
110
114
  rdoc_options: []
111
115
  require_paths:
112
116
  - lib
@@ -121,8 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
125
  - !ruby/object:Gem::Version
122
126
  version: '0'
123
127
  requirements: []
124
- rubygems_version: 3.1.2
125
- signing_key:
128
+ rubygems_version: 3.2.22
129
+ signing_key:
126
130
  specification_version: 4
127
131
  summary: A Ruby gem that makes it easy to create value objects.
128
132
  test_files: []
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.7.0
7
- before_install: gem install bundler -v 2.0.2