attr_value_object 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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/attr_value_object.gemspec +38 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/attr_value_object.rb +25 -0
- data/lib/attr_value_object/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ba9a732a062c36f2c123294b54a9f71b5f4794856e562cf77e63c22848c73fcf
|
4
|
+
data.tar.gz: 2eaab29218324bc6e3e5fc99c7d44028a497f0c95370ddfcfb5b54049d5334f7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ddb6da268f5389e0d3a9e01664bf6fc16d81c7013f2a2ef02ba210d550afa0db5864ae834bcb55623d467baca718e263bcef6faaa7fccb63075d397c4d6c92a
|
7
|
+
data.tar.gz: d994cf8e7271cf73a1884620d766cc0fb9c20a12a50197a8245ce26526eb0b015d1692f1a47d24b4cb8150802603270e83453f8c608be4b4da6fe974ff52cd1b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attr_value_object (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (5.2.0)
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
|
+
i18n (>= 0.7, < 2)
|
12
|
+
minitest (~> 5.1)
|
13
|
+
tzinfo (~> 1.1)
|
14
|
+
concurrent-ruby (1.0.5)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
i18n (1.0.1)
|
17
|
+
concurrent-ruby (~> 1.0)
|
18
|
+
minitest (5.11.3)
|
19
|
+
rake (10.5.0)
|
20
|
+
rspec (3.7.0)
|
21
|
+
rspec-core (~> 3.7.0)
|
22
|
+
rspec-expectations (~> 3.7.0)
|
23
|
+
rspec-mocks (~> 3.7.0)
|
24
|
+
rspec-core (3.7.1)
|
25
|
+
rspec-support (~> 3.7.0)
|
26
|
+
rspec-expectations (3.7.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.7.0)
|
29
|
+
rspec-mocks (3.7.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.7.0)
|
32
|
+
rspec-support (3.7.1)
|
33
|
+
thread_safe (0.3.6)
|
34
|
+
tzinfo (1.2.5)
|
35
|
+
thread_safe (~> 0.1)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
activesupport
|
42
|
+
attr_value_object!
|
43
|
+
bundler (~> 1.16)
|
44
|
+
rake (~> 10.0)
|
45
|
+
rspec (~> 3.0)
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Shun MIZUKAMI
|
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,65 @@
|
|
1
|
+
# Getting started
|
2
|
+
|
3
|
+
`attr_value_object` is a minimalistic module.
|
4
|
+
|
5
|
+
`attr_value_object` is useful in the case when you have a model with multiple fields to represent the dependent property and you want to separate logic. For example, address of user. The fields are `postcode`, `prefecture_id`, `street` and `building`. If you are using Ruby on Rails, you will add columns into users with a migration. You may want to make them indicating explicitly a same property (address). So the `User` model looks like following.
|
6
|
+
|
7
|
+
```rb
|
8
|
+
# ActiveRecord is not required.
|
9
|
+
# It's enough for `attr_value_object` to work if only these accessors are defined.
|
10
|
+
|
11
|
+
class User
|
12
|
+
attr_accessor :address_postcode,
|
13
|
+
:address_prefecture_id,
|
14
|
+
:address_street,
|
15
|
+
:address_building
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
If another model has the same property (address) and you want to `JOIN` based on `addresses`, it's good to create `addresses` table. But there is the case you don't need such a function. However, you may still want to separate logic. Then, you can do it with creating `Address` class and specifying `attr_value_object`.
|
20
|
+
|
21
|
+
```rb
|
22
|
+
class Address
|
23
|
+
attr_accessor :postcode,
|
24
|
+
:prefecture_id,
|
25
|
+
:street,
|
26
|
+
:building
|
27
|
+
end
|
28
|
+
|
29
|
+
class User
|
30
|
+
extend AttrValueObject
|
31
|
+
|
32
|
+
attr_accessor :address_postcode,
|
33
|
+
:address_prefecture_id,
|
34
|
+
:address_street,
|
35
|
+
:address_building
|
36
|
+
|
37
|
+
attr_value_object :address
|
38
|
+
end
|
39
|
+
|
40
|
+
address = Address.new
|
41
|
+
address.postcode = '1000000'
|
42
|
+
address.prefecture_id = 1
|
43
|
+
address.street = 'Foo Bar Street'
|
44
|
+
address.building = 'AVO Building 34F'
|
45
|
+
|
46
|
+
user = User.new
|
47
|
+
|
48
|
+
# setter
|
49
|
+
user.address = address
|
50
|
+
user.address_postcode # => '1000000'
|
51
|
+
|
52
|
+
# getter
|
53
|
+
user.address # => #<Address> always new instance
|
54
|
+
```
|
55
|
+
|
56
|
+
# API
|
57
|
+
|
58
|
+
### `#attr_value_object(name, options = {})`
|
59
|
+
|
60
|
+
The binding class is inferred from name unless `class_name` option is specified.
|
61
|
+
|
62
|
+
# ToDo
|
63
|
+
|
64
|
+
- work with rails validations
|
65
|
+
- attribute mapping flexibility
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "attr_value_object/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "attr_value_object"
|
8
|
+
spec.version = AttrValueObject::VERSION
|
9
|
+
spec.authors = ["Shun MIZUKAMI"]
|
10
|
+
spec.email = ["norainu234@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Binds attributes into value object"
|
13
|
+
spec.description = "defines getter and setter via Value Object to separate logic from model"
|
14
|
+
spec.homepage = "https://github.com/mizukami234/attr_value_object"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
# spec.bindir = "exe"
|
32
|
+
# spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "attr_value_object"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'attr_value_object/version'
|
2
|
+
require 'active_support/core_ext/string'
|
3
|
+
|
4
|
+
module AttrValueObject
|
5
|
+
def attr_value_object(name, options = {})
|
6
|
+
klass = Object.const_get(options.fetch(:class_name, name.to_s.camelize))
|
7
|
+
|
8
|
+
define_method(name) do
|
9
|
+
mappings = methods.map { |m| m.match(/\A#{name}_([0-9a-z_]+)\z/).try { |match| [m, match[1]] } }.compact
|
10
|
+
params = {}
|
11
|
+
mappings.each do |source, target|
|
12
|
+
params[target.to_sym] = send(source.to_sym)
|
13
|
+
end
|
14
|
+
klass.new(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
define_method("#{name}=") do |obj|
|
18
|
+
mappings = methods.map { |m| m.match(/\A#{name}_([0-9a-z_]+)=\z/).try { |match| [m, match[1]] } }.compact
|
19
|
+
mappings.each do |source, target|
|
20
|
+
send(source, obj.send(target))
|
21
|
+
end
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_value_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shun MIZUKAMI
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-12 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: 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
|
+
description: defines getter and setter via Value Object to separate logic from model
|
56
|
+
email:
|
57
|
+
- norainu234@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- attr_value_object.gemspec
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/attr_value_object.rb
|
74
|
+
- lib/attr_value_object/version.rb
|
75
|
+
homepage: https://github.com/mizukami234/attr_value_object
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata:
|
79
|
+
allowed_push_host: https://rubygems.org
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.7.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Binds attributes into value object
|
100
|
+
test_files: []
|