interactor-initializer 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/.rubocop.yml +11 -0
- data/Gemfile +3 -0
- data/README.md +50 -0
- data/Rakefile +3 -0
- data/interactor-initializer.gemspec +21 -0
- data/lib/interactor/initializer/attr_readers.rb +5 -0
- data/lib/interactor/initializer/call_methods.rb +9 -0
- data/lib/interactor/initializer/initialize.rb +35 -0
- data/lib/interactor/initializer/version.rb +5 -0
- data/lib/interactor/initializer.rb +24 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1f044791f85be189cabd9da79621ce3d3693909
|
4
|
+
data.tar.gz: 978fd420eac6c5289c81bb81ae73af930f43ba3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 676883689febe2cc7f639c65a7558028be891c65c1745d2c89a2be83e659cfdcb02e418aea052f15d878e902a586c953ccce340e38e1744477f1cd1cf4544ee5
|
7
|
+
data.tar.gz: 0c4ad289a43efedebd3847ce07823227ab03ae2a11edc75736f60d1de40d02a55472882308b41c50d591676a690aa716f73f7677def33b1046fc87d0fe14e9c7
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Interactor::Initializer
|
2
|
+
|
3
|
+
Dry interactor initializer
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'interactor-initializer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install interactor-initializer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Vouchers::Issue
|
27
|
+
include Interactor::Initializer
|
28
|
+
|
29
|
+
initialize_with :user
|
30
|
+
|
31
|
+
def run
|
32
|
+
puts "Voucher issued for #{user.full_name}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
Vouchers::Issue.for(user)
|
39
|
+
=> Voucher issued for Jonas Jonaitis
|
40
|
+
```
|
41
|
+
|
42
|
+
Interactor could be called with: `.for`, `.with` or `.run`
|
43
|
+
|
44
|
+
if keyword params are needed:
|
45
|
+
`initialize_with_keyword_params` should be used instead of `initialize_with`
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
initialize_with_keyword_params :user
|
49
|
+
Vouchers::Issue.for(user: good_user)
|
50
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'interactor/initializer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'interactor-initializer'
|
8
|
+
spec.platform = Gem::Platform::RUBY
|
9
|
+
spec.version = Interactor::Initializer::VERSION
|
10
|
+
spec.authors = ['Šarūnas Kūjalis']
|
11
|
+
spec.email = ['sarjalis@gmail.com']
|
12
|
+
spec.summary = 'Dry interactor initializer'
|
13
|
+
spec.homepage = 'https://github.com/vinted/interactor-initializer'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.test_files = `git ls-files -- {spec}/*`.split("\n")
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Interactor::Initializer::Initialize
|
2
|
+
def self.for(*args)
|
3
|
+
new(*args).run
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_reader :target_class, :attributes, :keyword_params
|
7
|
+
|
8
|
+
def initialize(target_class, attributes, keyword_params: false)
|
9
|
+
@target_class = target_class
|
10
|
+
@attributes = attributes
|
11
|
+
@keyword_params = keyword_params
|
12
|
+
end
|
13
|
+
|
14
|
+
def run
|
15
|
+
target_class.class_eval <<-RUBY
|
16
|
+
def initialize(#{initializer_signature(attributes)})
|
17
|
+
#{attr_assignments(attributes)}
|
18
|
+
end
|
19
|
+
RUBY
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def initializer_signature(attributes)
|
25
|
+
return attributes.join(', ') unless keyword_params
|
26
|
+
|
27
|
+
attributes.map { |attribute| "#{attribute}:" }.join(', ')
|
28
|
+
end
|
29
|
+
|
30
|
+
def attr_assignments(attributes)
|
31
|
+
attributes.inject('') do |assignments, attribute|
|
32
|
+
"#{assignments}@#{attribute} = #{attribute};"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'interactor/initializer/version'
|
2
|
+
|
3
|
+
module Interactor
|
4
|
+
module Initializer
|
5
|
+
def self.included(target_class)
|
6
|
+
target_class.extend ClassMethods
|
7
|
+
Interactor::Initializer::CallMethods.for(target_class)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
module_function
|
12
|
+
|
13
|
+
def initialize_with(*attributes)
|
14
|
+
Interactor::Initializer::Initialize.for(self, attributes)
|
15
|
+
Interactor::Initializer::AttrReaders.for(self, attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize_with_keyword_params(*attributes)
|
19
|
+
Interactor::Initializer::Initialize.for(self, attributes, keyword_params: true)
|
20
|
+
Interactor::Initializer::AttrReaders.for(self, attributes)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: interactor-initializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Šarūnas Kūjalis"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-07 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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
|
+
description:
|
42
|
+
email:
|
43
|
+
- sarjalis@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- Gemfile
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- interactor-initializer.gemspec
|
54
|
+
- lib/interactor/initializer.rb
|
55
|
+
- lib/interactor/initializer/attr_readers.rb
|
56
|
+
- lib/interactor/initializer/call_methods.rb
|
57
|
+
- lib/interactor/initializer/initialize.rb
|
58
|
+
- lib/interactor/initializer/version.rb
|
59
|
+
homepage: https://github.com/vinted/interactor-initializer
|
60
|
+
licenses: []
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Dry interactor initializer
|
82
|
+
test_files: []
|