interactor-initializer 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
+ SHA1:
3
+ metadata.gz: e1f044791f85be189cabd9da79621ce3d3693909
4
+ data.tar.gz: 978fd420eac6c5289c81bb81ae73af930f43ba3f
5
+ SHA512:
6
+ metadata.gz: 676883689febe2cc7f639c65a7558028be891c65c1745d2c89a2be83e659cfdcb02e418aea052f15d878e902a586c953ccce340e38e1744477f1cd1cf4544ee5
7
+ data.tar.gz: 0c4ad289a43efedebd3847ce07823227ab03ae2a11edc75736f60d1de40d02a55472882308b41c50d591676a690aa716f73f7677def33b1046fc87d0fe14e9c7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ LineLength:
2
+ Max: 100
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ SignalException:
8
+ EnforcedStyle: only_raise
9
+
10
+ ClassAndModuleChildren:
11
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
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,3 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :spec
@@ -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,5 @@
1
+ class Interactor::Initializer::AttrReaders
2
+ def self.for(target_class, attributes)
3
+ target_class.class_eval { attr_reader(*attributes) }
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class Interactor::Initializer::CallMethods
2
+ METHOD_NAMES = %i(for with run).freeze
3
+
4
+ def self.for(target_class)
5
+ METHOD_NAMES.each do |method_name|
6
+ target_class.define_singleton_method(method_name) { |*args| new(*args).run }
7
+ end
8
+ end
9
+ 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,5 @@
1
+ module Interactor
2
+ module Initializer
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ 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: []