valuables 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +18 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/valuables.rb +5 -0
- data/lib/valuables/deep_freeze.rb +35 -0
- data/lib/valuables/entity.rb +88 -0
- data/lib/valuables/version.rb +3 -0
- data/valuables.gemspec +34 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42a28204127e78a53b98cb474d99647083f6a2d1
|
4
|
+
data.tar.gz: c9e9ae645d5845b84eb0bf72a67de525dd16d896
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 79c3f9a2c91f5debfec1af2128b0181f46ea04ec2c50dec7eea9db56682e9acab8fc94c536d0d376c6c8e2db5c5ab7e7d4b5733671d8166c45cc6b076c56015e
|
7
|
+
data.tar.gz: 7110281df5e13ac135ff0c47dc501fd69e916f8683ce9a68a6072f6e4ad608a07aac967b065ab882153a2a7f10e957afeaca4ebd25f85265dd16162c28402326
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Arjan van der Gaag
|
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,84 @@
|
|
1
|
+
# Valuables
|
2
|
+
|
3
|
+
Sometimes, your objects are only data and no behaviour. These are value objects,
|
4
|
+
and they are defined by their _contents_. These objects are immutable, so it is
|
5
|
+
safe to let them propagate throughout the system.
|
6
|
+
|
7
|
+
Being immutable, value objects cannot be modified; their contents are set once
|
8
|
+
on initialisation. Also, being identified by their contents, two entities with
|
9
|
+
the same contents are considered equal.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'valuables'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install valuables
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Article
|
31
|
+
include Valuables::Entity
|
32
|
+
attributes :title, :author, :created_at
|
33
|
+
|
34
|
+
def human_created_at
|
35
|
+
super.strftime '%c'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
article = Article.new(title: 'On Values', author: 'J. Example', created_at: Time.now)
|
40
|
+
article.title
|
41
|
+
# => 'On Values'
|
42
|
+
|
43
|
+
article = Article.new(title: 'On Values')
|
44
|
+
# => ArgumentError: missing attributes: author, created_at
|
45
|
+
|
46
|
+
article = Article.new(title: 'On Values', author: 'J. Example', created_at: Time.now, tags: ['news'])
|
47
|
+
# => ArgumentError: unexpected attributes: tags
|
48
|
+
|
49
|
+
article1 = Article.new(title: 'On Values', author: 'J. Example', created_at: Time.now)
|
50
|
+
article2 = Article.new(title: 'On Values', author: 'J. Example', created_at: Time.now)
|
51
|
+
article1 == article2
|
52
|
+
# => true
|
53
|
+
|
54
|
+
article3 = Article.new(title: 'On Values', author: 'J. Example', created_at: Time.now)
|
55
|
+
article4 = Article.new(title: 'More Values', author: 'R. Demo', created_at: Time.now)
|
56
|
+
article3 == article4
|
57
|
+
# => false
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
## Development
|
62
|
+
|
63
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
64
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
65
|
+
prompt that will allow you to experiment.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
68
|
+
release a new version, update the version number in `version.rb`, and then run
|
69
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
70
|
+
git commits and tags, and push the `.gem` file to
|
71
|
+
[rubygems.org](https://rubygems.org).
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
Bug reports and pull requests are welcome on GitHub at
|
76
|
+
https://github.com/avdgaag/valuables. This project is intended to be a safe,
|
77
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
78
|
+
the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
79
|
+
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT
|
84
|
+
License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
RDoc::Task.new :doc do |t|
|
6
|
+
t.main = 'README.md'
|
7
|
+
t.rdoc_files.include('README.md', 'LICENSE.txt', 'CODE_OF_CONDUCT.md', 'lib/**/*.rb')
|
8
|
+
t.rdoc_dir = 'doc'
|
9
|
+
t.options << '--exclude=test/.* --quiet'
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.libs << 'test'
|
14
|
+
t.libs << 'lib'
|
15
|
+
t.test_files = FileList['test/**/*_test.rb']
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: :test
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "valuables"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/valuables.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Valuables
|
2
|
+
##
|
3
|
+
# Deep freezing functionality for Ruby objects
|
4
|
+
#
|
5
|
+
# Freeze Ruby objects like a regular +#freeze+ would do, but also freeze any
|
6
|
+
# included values. This comes with support for commonly used Ruby classes,
|
7
|
+
# such as +Hash+, +Array+ and +Range+.
|
8
|
+
#
|
9
|
+
# To make custom objects work with +DeepFreeze+, define a +#deep_freeze+
|
10
|
+
# method.
|
11
|
+
module DeepFreeze
|
12
|
+
module_function
|
13
|
+
|
14
|
+
# Like Ruby's +#freeze+ but recurses into contained values.
|
15
|
+
def deep_freeze(obj)
|
16
|
+
if obj.respond_to?(:deep_freeze)
|
17
|
+
obj.deep_freeze
|
18
|
+
elsif obj.kind_of?(Hash)
|
19
|
+
obj.reduce({}) do |acc, (key, value)|
|
20
|
+
acc.merge deep_freeze(key) => deep_freeze(value)
|
21
|
+
end.freeze
|
22
|
+
elsif obj.kind_of?(Array)
|
23
|
+
obj.reduce([]) do |acc, value|
|
24
|
+
acc << deep_freeze(value)
|
25
|
+
end.freeze
|
26
|
+
elsif obj.kind_of?(Range)
|
27
|
+
deep_freeze(obj.begin)..deep_freeze(obj.end).freeze
|
28
|
+
elsif obj.kind_of?(Symbol)
|
29
|
+
obj
|
30
|
+
else
|
31
|
+
obj.dup.freeze
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require_relative './deep_freeze'
|
2
|
+
|
3
|
+
module Valuables
|
4
|
+
##
|
5
|
+
# Include +Entity+ to make a class an immutable value object.
|
6
|
+
module Entity
|
7
|
+
def self.included(cls) # :nodoc:
|
8
|
+
cls.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
##
|
13
|
+
# Define an explicit list of attribute names.
|
14
|
+
#
|
15
|
+
# When you explicitly define attributes, you ensure objects can only be
|
16
|
+
# created with those attributes. Missing or unexpected attributes will
|
17
|
+
# trigger an +ArgumentError+.
|
18
|
+
def attributes(*names)
|
19
|
+
return @attributes unless names.any?
|
20
|
+
@attributes = names
|
21
|
+
mod = Module.new do
|
22
|
+
names.each do |name|
|
23
|
+
define_method(name) do
|
24
|
+
@attributes.fetch(name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
include mod
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :hash # :nodoc:
|
33
|
+
|
34
|
+
attr_reader :attributes # :nodoc:
|
35
|
+
protected :attributes
|
36
|
+
|
37
|
+
##
|
38
|
+
# Initialize a new entity
|
39
|
+
#
|
40
|
+
# Provide the entity's attributes as key/value pairs in +kwargs+. Any
|
41
|
+
# attributes you provide will be duplicated and frozen, so you need not
|
42
|
+
# worry about later modifications to any values passed in.
|
43
|
+
def initialize(**kwargs)
|
44
|
+
assert_required_attributes(kwargs)
|
45
|
+
assert_no_extra_attributes(kwargs)
|
46
|
+
@attributes = Valuables::DeepFreeze.deep_freeze(kwargs)
|
47
|
+
@hash = self.class.hash ^ @attributes.hash
|
48
|
+
freeze
|
49
|
+
end
|
50
|
+
|
51
|
+
# Comparse two objects
|
52
|
+
#
|
53
|
+
# Two entities are considered equal if they are instances of the same class
|
54
|
+
# and their internal attributes are equal.
|
55
|
+
def ==(other)
|
56
|
+
self.class == other.class &&
|
57
|
+
attributes == other.attributes
|
58
|
+
end
|
59
|
+
alias_method :eql?, :==
|
60
|
+
|
61
|
+
# Get a Hash representation of this entity.
|
62
|
+
def to_h
|
63
|
+
@attributes
|
64
|
+
end
|
65
|
+
|
66
|
+
def inspect # :nodoc:
|
67
|
+
"#<#{self.class} #{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(' ')}>"
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def assert_required_attributes(attrs)
|
73
|
+
return unless self.class.attributes
|
74
|
+
missing_attributes = self.class.attributes - attrs.keys
|
75
|
+
if missing_attributes.any?
|
76
|
+
raise ArgumentError, 'Missing attributes: ' + missing_attributes.join(', ')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def assert_no_extra_attributes(attrs)
|
81
|
+
return unless self.class.attributes
|
82
|
+
extra_attributes = attrs.keys - self.class.attributes
|
83
|
+
if extra_attributes.any?
|
84
|
+
raise ArgumentError, 'Unexpected attributes: ' + extra_attributes.join(', ')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/valuables.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'valuables/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'valuables'
|
8
|
+
spec.version = Valuables::VERSION
|
9
|
+
spec.authors = ['Arjan van der Gaag']
|
10
|
+
spec.email = ['arjan.vandergaag@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Immutable value objects in Ruby}
|
13
|
+
spec.description = <<-EOS
|
14
|
+
Sometimes, your objects are only data and no behaviour. These are value
|
15
|
+
objects, and they are defined by their _contents_. These objects are immutable,
|
16
|
+
so it is safe to let them propagate throughout the system.
|
17
|
+
|
18
|
+
Being immutable, value objects cannot be modified; their contents are set once
|
19
|
+
on initialisation. Also, being identified by their contents, two entities with
|
20
|
+
the same contents are considered equal.
|
21
|
+
EOS
|
22
|
+
|
23
|
+
spec.homepage = 'http://avdgaag.github.io/valuables'
|
24
|
+
spec.license = 'MIT'
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'minitest'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valuables
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arjan van der Gaag
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-10 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: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: |
|
56
|
+
Sometimes, your objects are only data and no behaviour. These are value
|
57
|
+
objects, and they are defined by their _contents_. These objects are immutable,
|
58
|
+
so it is safe to let them propagate throughout the system.
|
59
|
+
|
60
|
+
Being immutable, value objects cannot be modified; their contents are set once
|
61
|
+
on initialisation. Also, being identified by their contents, two entities with
|
62
|
+
the same contents are considered equal.
|
63
|
+
email:
|
64
|
+
- arjan.vandergaag@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- ".gitignore"
|
70
|
+
- ".travis.yml"
|
71
|
+
- CODE_OF_CONDUCT.md
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- bin/console
|
77
|
+
- bin/setup
|
78
|
+
- lib/valuables.rb
|
79
|
+
- lib/valuables/deep_freeze.rb
|
80
|
+
- lib/valuables/entity.rb
|
81
|
+
- lib/valuables/version.rb
|
82
|
+
- valuables.gemspec
|
83
|
+
homepage: http://avdgaag.github.io/valuables
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.4.8
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Immutable value objects in Ruby
|
107
|
+
test_files: []
|
108
|
+
has_rdoc:
|