simple_value 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6b5fa33c85148a84a10955b2964e59cf3e16c3b33a7cbf0236b69eb39d0aacce
4
+ data.tar.gz: d00257b7fa3d4f4dcaed10067d10188ba07ad112da972709db74235e7a7fa3c1
5
+ SHA512:
6
+ metadata.gz: 873cbb1bac3bc65a6f2c69361c5bd6e144fd8c3d244299fd3b274e8904ad21607ade2a3027726c9889f5b68e0feb2abf21ddd60dbc115d0abeb3d1916e74604b
7
+ data.tar.gz: f50110600cd0e1474b985caf54af0ea2241768c0004987a2164be668531df5dfb5f619fed36bdddadae06a57dfe5b7d9cc2fc065e081cd7745bc2d63128a1306
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_value.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Matous Vokal
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,93 @@
1
+ # SimpleValue
2
+
3
+ A tiny Ruby gem for using value objects in your models. Create a plain Ruby class to represent the value object. Then easily use it in a model by extending `SimpleValue::Model` and using the `value_object` macro.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_value'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_value
20
+
21
+ ## Usage
22
+
23
+ Let's suppose that we have a `User` model with a `phone` attribute. Currently, `User` validates the format of the phone number, which is stored as a string in the database.
24
+
25
+ ```ruby
26
+ class User < ApplicationRecord
27
+ before_validation :format_phone
28
+
29
+ validate :phone_validation
30
+
31
+ private
32
+
33
+ def format_phone
34
+ # logic for formatting the phone number
35
+ end
36
+
37
+ def phone_validation
38
+ # complicated validation logic
39
+ end
40
+ end
41
+ ```
42
+
43
+ The rest of the application is already written to use a string when interacting with a `User`. We can still move the validation and whatever other logic related to the phone number into a separate class.
44
+
45
+ Moving the logic into its own class also prevents duplication in case another model needs to have a phone number. It can then simply use the same value object.
46
+
47
+ ```ruby
48
+ class Phone
49
+ include ActiveModel::Validations
50
+
51
+ attr_reader :value
52
+
53
+ validate :validate_number
54
+
55
+ def initialize(value)
56
+ @value = format_number value
57
+ end
58
+
59
+ private
60
+
61
+ def format_number(number)
62
+ # logic for formatting the phone number
63
+ end
64
+
65
+ def validate_number
66
+ # complicated validation logic
67
+ end
68
+ end
69
+ ```
70
+
71
+ We can use the same DSL for validations in any class just by including `ActiveModel::Validations`. This way it is possible to easily move the phone validation from the `User` model to the new `Phone` class.
72
+
73
+ Currently this gem works for value objects that take a single required argument in their initializer. The value object also needs an `attr_reader` for its value (value is the default name, but it can be sometihng else).
74
+
75
+ The only thing left to do is to make the `User` model use the new value object. It needs to extends `SimpleValue::Model` in order to get access to the `value_object` macro.
76
+
77
+ ```ruby
78
+ class User < ApplicationRecord
79
+ extend SimpleValue::Model
80
+
81
+ value_object :phone, Phone, validate: true
82
+ end
83
+ ```
84
+
85
+ The first argument of `value_object` is a symbol with the name of the attribute used to initialize the value object. The second arument is the class used for the value object.
86
+
87
+ There are also two optional parameters for `value_object`. `validate` is set to false by default, if set to true, it will use whatever validations are in the value object and add the errors to the base of the model's errors.
88
+
89
+ The second optional parameter is the name of the `attr_reader` used to get the value from the value object. By default it is set to `:value`.
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_value"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ module SimpleValue
2
+ module Model
3
+ def value_object(attr_name, value_class, validate: false, value: :value)
4
+ attr_reader "#{attr_name}_object"
5
+
6
+ object_writer = "#{attr_name}_object=".to_sym
7
+ instance_reader = "@#{attr_name}_object".to_sym
8
+
9
+ define_method "#{attr_name}_object=" do |val|
10
+ instance_variable_set instance_reader, val
11
+ self[attr_name] = val.send value
12
+ end
13
+
14
+ define_method "#{attr_name}=" do |val|
15
+ send object_writer, value_class.new(val)
16
+ end
17
+
18
+ after_initialize do
19
+ send object_writer, value_class.new(self[attr_name])
20
+ end
21
+
22
+ return unless validate
23
+
24
+ validates_with Validator, "#{attr_name}_object".to_sym => attr_name
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ module SimpleValue
2
+ class Validator < ActiveModel::Validator
3
+ def validate(record)
4
+ options.each do |value_name, attribute_name|
5
+ value_object = record.send value_name
6
+ next if value_object.valid?
7
+
8
+ value_object.errors.each do |_, error|
9
+ record.errors.add attribute_name, error
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleValue
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'simple_value/version'
2
+
3
+ module SimpleValue
4
+ end
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "simple_value/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_value"
8
+ spec.version = SimpleValue::VERSION
9
+ spec.authors = ["Matous Vokal"]
10
+ spec.email = ["vokalmat@gmail.com"]
11
+
12
+ spec.summary = "Simple value objects for your models"
13
+ spec.description = "Tiny gem for moving logic from models into separate value objects"
14
+ spec.homepage = "https://github.com/Sorc96/simple_value"
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
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/Sorc96/simple_value"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 2.0"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+
39
+ spec.add_dependency "activemodel"
40
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matous Vokal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-15 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: '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: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Tiny gem for moving logic from models into separate value objects
56
+ email:
57
+ - vokalmat@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/simple_value.rb
70
+ - lib/simple_value/model.rb
71
+ - lib/simple_value/validator.rb
72
+ - lib/simple_value/version.rb
73
+ - simple_value.gemspec
74
+ homepage: https://github.com/Sorc96/simple_value
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ homepage_uri: https://github.com/Sorc96/simple_value
80
+ source_code_uri: https://github.com/Sorc96/simple_value
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simple value objects for your models
100
+ test_files: []