valued 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94acb8c6bb54e7ae03a0ed7c3be248769fab10001972b83a16469960e1428b4b
4
+ data.tar.gz: a50be06601f251cf3c426137f12c78473ff4a864de3f86fb596ba5cf377805ab
5
+ SHA512:
6
+ metadata.gz: e885b302bd91434e22f8cc7b028a0d3d95aff9a82c63969bd408b76d085badc8fe2154965b8869a605ba92b17b23225dac4b640409a42c3fa4d0ba83267c6c07
7
+ data.tar.gz: 5fc71d72c26842ab38341721c11c2d1c72ba97cc4e14e416d0565db5a970f332392715b3fadfca4ac904258530b9d7f2dcfa61ea40581e940adad48b1ea97e53
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,84 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+
5
+ AllCops:
6
+ Exclude:
7
+ - db/**/*
8
+ - config/**/*
9
+ - script/**/*
10
+ - Rakefile
11
+ - Guardfile
12
+ - bin/*
13
+ - tmp/**/*
14
+ TargetRubyVersion: 2.6
15
+
16
+ Style:
17
+ Enabled: false
18
+
19
+ Layout:
20
+ Enabled: false
21
+
22
+ Style/HashEachMethods:
23
+ Enabled: false
24
+
25
+ Style/HashTransformKeys:
26
+ Enabled: false
27
+
28
+ Style/HashTransformValues:
29
+ Enabled: false
30
+
31
+ Metrics/AbcSize:
32
+ Enabled: false
33
+
34
+ Naming/VariableNumber:
35
+ EnforcedStyle: snake_case
36
+
37
+ Metrics/BlockLength:
38
+ CountComments: false
39
+ Max: 25
40
+ Exclude:
41
+ - "spec/**/*.rb"
42
+ - "test/**/*.rb"
43
+ - "Rakefile"
44
+ - "*.gemspec"
45
+ - "apps/api/application.rb"
46
+ - "**/*.rake"
47
+
48
+ Metrics/ClassLength:
49
+ CountComments: false
50
+ Exclude:
51
+ - "spec/**/*.rb"
52
+ - "test/**/*.rb"
53
+
54
+ Metrics/MethodLength:
55
+ Enabled: true
56
+ Exclude:
57
+ - "spec/**/*.rb"
58
+ - "test/**/*.rb"
59
+
60
+ Metrics/ModuleLength:
61
+ CountComments: false
62
+ Exclude:
63
+ - "spec/**/*.rb"
64
+ - "test/**/*.rb"
65
+
66
+ require: rubocop-rspec
67
+
68
+ RSpec/DescribedClass:
69
+ Enabled: false
70
+
71
+ RSpec/MultipleExpectations:
72
+ Enabled: false
73
+
74
+ RSpec/ExampleLength:
75
+ Enabled: false
76
+
77
+ RSpec/MessageSpies:
78
+ Enabled: false
79
+
80
+ RSpec/MessageChain:
81
+ Enabled: false
82
+
83
+ RSpec/AnyInstance:
84
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in valued.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ valued (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ diff-lcs (1.3)
11
+ jaro_winkler (1.5.4)
12
+ parallel (1.19.1)
13
+ parser (2.7.0.4)
14
+ ast (~> 2.4.0)
15
+ rainbow (3.0.0)
16
+ rake (13.0.1)
17
+ rexml (3.2.4)
18
+ rspec (3.9.0)
19
+ rspec-core (~> 3.9.0)
20
+ rspec-expectations (~> 3.9.0)
21
+ rspec-mocks (~> 3.9.0)
22
+ rspec-core (3.9.1)
23
+ rspec-support (~> 3.9.1)
24
+ rspec-expectations (3.9.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-mocks (3.9.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-support (3.9.2)
31
+ rubocop (0.80.1)
32
+ jaro_winkler (~> 1.5.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.7.0.1)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ rexml
37
+ ruby-progressbar (~> 1.7)
38
+ unicode-display_width (>= 1.4.0, < 1.7)
39
+ rubocop-rspec (1.38.1)
40
+ rubocop (>= 0.68.1)
41
+ ruby-progressbar (1.10.1)
42
+ unicode-display_width (1.6.1)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler (~> 2.0)
49
+ rake (~> 13.0)
50
+ rspec (~> 3.0)
51
+ rubocop (~> 0.80)
52
+ rubocop-rspec (~> 1.38)
53
+ valued!
54
+
55
+ BUNDLED WITH
56
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Mario Mainz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ [![Build Status](https://travis-ci.org/mmainz/valued.svg?branch=master)](https://travis-ci.org/mmainz/valued)
2
+
3
+ # Valued
4
+
5
+ Valued is a Ruby gem that allows you to easily define value objects.
6
+
7
+ In the past, I mainly used `Struct` for objects of this kind. However, structs
8
+ in Ruby have some drawbacks, like the fact that they implement hash access with
9
+ `[]`, which is undesirable if you want to use duck typing. Also, they require
10
+ all their attributes to always be passed to their constructor, which makes it
11
+ cumbersome to construct them from a JSON payload that may miss some optional
12
+ attribute.
13
+
14
+ You can of course also just implement those objects yourself, but that requires
15
+ a lot of boilerplate, like implementing `==`, `inspect`, `to_h` and other
16
+ methods you almost always need.
17
+
18
+ With Valued, you can automatically define a `Struct`-like class that behaves
19
+ properly.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'valued'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install valued
36
+
37
+ ## Usage
38
+
39
+ Just include `Valued` in your class and then define `attributes`.
40
+
41
+ ```ruby
42
+ require 'valued'
43
+
44
+ class Quantity
45
+ include Valued
46
+
47
+ attributes :unit, :amount
48
+ end
49
+ ```
50
+
51
+ After that, you can use your class similarly to a struct. You construct an
52
+ instance of it with a hash:
53
+
54
+ ```ruby
55
+ quantity = Quantity.new(unit: 'm', amount: 2)
56
+ p quantity.amount
57
+ => 2
58
+ p quantity.unit
59
+ => 'm'
60
+ ```
61
+
62
+ If you omit a parameter in the constructor, it is set to `nil`.
63
+
64
+ ```ruby
65
+ quantity = Quantity.new(amount: 2)
66
+ p quantity.unit
67
+ => nil
68
+ ```
69
+
70
+ Classes defined with valued automatically implement `==`, `eql?`, `hash`,
71
+ `to_h`, `to_s` and `inspect`.
72
+
73
+ ```ruby
74
+ quantity = Quantity.new(amount: 2, unit: 'm')
75
+ second_quantity = Quantity.new(amount: 2, unit: 'm')
76
+ quantity == second_quantity
77
+ => true
78
+
79
+ quantity.to_h
80
+ => {:amount=>2, :unit=>"m"}
81
+ p quantity
82
+ => #<Quantity amount=2 unit="m">
83
+ ```
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the
88
+ [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+ RuboCop::RakeTask.new
8
+
9
+ task default: %i[spec rubocop]
@@ -0,0 +1,3 @@
1
+ module Valued
2
+ VERSION = '0.1.0'
3
+ end
data/lib/valued.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'valued/version'
2
+
3
+ module Valued
4
+ module ClassMethods
5
+ def attributes(*attributes)
6
+ attributes.each { |attr| attr_reader attr }
7
+ define_method('_attributes') { attributes }
8
+ end
9
+ end
10
+
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ def initialize(attributes = {})
16
+ _attributes.each do |attribute|
17
+ if attributes.key?(attribute)
18
+ instance_variable_set(
19
+ "@#{attribute}",
20
+ attributes.fetch(attribute).freeze
21
+ )
22
+ end
23
+ end
24
+ end
25
+
26
+ def ==(other)
27
+ _attributes.all? do |attribute|
28
+ other.respond_to?(attribute) && send(attribute) == other.send(attribute)
29
+ end
30
+ end
31
+
32
+ def eql?(other)
33
+ self.class == other.class && self == other
34
+ end
35
+
36
+ def hash
37
+ (_attributes.map { |attribute| send(attribute) } + [self.class]).hash
38
+ end
39
+
40
+ def to_h
41
+ _attributes.each_with_object({}) do |attribute, hash|
42
+ hash[attribute] = send(attribute)
43
+ end
44
+ end
45
+
46
+ def to_s
47
+ inspect
48
+ end
49
+
50
+ def inspect
51
+ inspected_attributes =
52
+ _attributes.map do |attribute|
53
+ "#{attribute}=#{send(attribute).inspect}"
54
+ end.join(' ')
55
+ "#<#{self.class} #{inspected_attributes}>"
56
+ end
57
+ end
data/valued.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'valued/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'valued'
7
+ spec.version = Valued::VERSION
8
+ spec.authors = ['Mario Mainz']
9
+ spec.email = %w[mainz.mario@googlemail.com]
10
+
11
+ spec.summary = 'A Ruby gem that makes it easy to create value objects.'
12
+ spec.homepage = 'https://github.com/mmainz/valued'
13
+ spec.license = 'MIT'
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/mmainz/valued'
17
+
18
+ spec.files =
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
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = %w[lib]
27
+
28
+ spec.add_development_dependency 'bundler', '~> 2.0'
29
+ spec.add_development_dependency 'rake', '~> 13.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop', '~> 0.80'
32
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.38'
33
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valued
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mario Mainz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.80'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.80'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.38'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.38'
83
+ description:
84
+ email:
85
+ - mainz.mario@googlemail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/valued.rb
100
+ - lib/valued/version.rb
101
+ - valued.gemspec
102
+ homepage: https://github.com/mmainz/valued
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ homepage_uri: https://github.com/mmainz/valued
107
+ source_code_uri: https://github.com/mmainz/valued
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.1.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: A Ruby gem that makes it easy to create value objects.
127
+ test_files: []