command_class 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db12173fa0f453ff13d6ca3d6e02ac6a00c51b15aaa307902c4c8fb0a1915a55
4
- data.tar.gz: 43265b25a5f2ecd473eacbcfc680b4f5ec52037dade4cb7297236e9fefe51c18
3
+ metadata.gz: 5d7721d152aaadf54c4b668e790c389f11c8f76e592c405954422041718b2a56
4
+ data.tar.gz: 416070df89717db87c2768d66f15bece693158f8987261fe3a163f469e92ca05
5
5
  SHA512:
6
- metadata.gz: 1af10ca2ecf0be56fe7dd74e8cc5c99545a92bbeb8b478e4121fc93975366096ad546744a2493c34169341daf5c59c519d8faf9d7ddc0474c50f107b1a087a21
7
- data.tar.gz: 375eb3cedd42259268a433fa0c9ec8bd0e1d91c5e87a962ea5a5146b7ad47262a7309fc64b52227b3f96250904431a7f451da687bebc395d6fa9ebbae0c5d3ce
6
+ metadata.gz: f392b0b33006919cf2551c84485cdb4e5d5bfa9255a96b1eef609aac3a297e83716ddae4a5216bc6252d9fdb968d3b510022ba02044c8799a49b4f518bccc474
7
+ data.tar.gz: fccc0ee2ba3118e18a30c239c372f445901cfe2f10be76314ffb0fe10174165fba5f54164f794b96813ddd7ac0f6b50e7f835c38d5005ff811f4b28b74af711e
data/lib/command_class.rb CHANGED
@@ -1,6 +1,18 @@
1
1
  class CommandClass
2
+
3
+ module Include
4
+ def command_class(dependencies:, inputs:, &blk)
5
+ CommandClass.commandify(self, dependencies, inputs, &blk)
6
+ end
7
+ end
8
+
2
9
  def self.new(dependencies:, inputs:, &blk)
3
- cmd_cls = Class.new
10
+ commandify(Class.new, dependencies, inputs, &blk)
11
+ end
12
+
13
+ # Turns the class into a command class
14
+ #
15
+ def self.commandify(cmd_cls, dependencies, inputs, &blk)
4
16
  cmd_cls.const_set('DEFAULT_DEPS', dependencies)
5
17
 
6
18
  cmd_cls.class_eval <<~RUBY
@@ -29,6 +41,7 @@ class CommandClass
29
41
 
30
42
  private
31
43
 
44
+
32
45
  # TODO: allow for unnamed as well
33
46
  def cmd_call_signature(inputs)
34
47
  inputs.map {|x| "#{x}:" }.join(', ')
@@ -1,73 +1,12 @@
1
1
  require 'rspec'
2
- require_relative"../lib/command_class"
2
+ require_relative '../lib/command_class'
3
+ require_relative './create_user'
3
4
 
4
- # Setup classes and collaborators to test
5
- #
6
- UserRepo = Class.new
7
- MyEmailService = Class.new
8
-
9
- module Errors
10
- class InvalidName < RuntimeError; end
11
- class InvalidEmail < RuntimeError; end
12
- class InvalidPassword < RuntimeError; end
13
- class EmailAlreadyExists < RuntimeError; end
14
- end
15
-
16
- CreateUser = CommandClass.new(
17
- dependencies: {user_repo: UserRepo, email_service: MyEmailService},
18
- inputs: [:name, :email, :password]
19
- ) do
20
-
21
- def call
22
- validate_input
23
- ensure_unique_email
24
- insert_user
25
- send_confirmation
26
- end
27
-
28
- private
29
-
30
- def validate_input
31
- validate_name
32
- validate_email
33
- validate_password
34
- end
35
-
36
- def ensure_unique_email
37
- email_exists = @user_repo.find_by_email(@email)
38
- raise Errors::EmailAlreadyExists if email_exists
39
- end
40
-
41
- def insert_user
42
- @user_repo.insert(name: @name, email: @email, password: @password)
43
- end
44
-
45
- def send_confirmation
46
- @email_service.send_signup_confirmation(name: @name, email: @email)
47
- end
48
-
49
- def validate_name
50
- valid = @name.size > 1
51
- raise Errors::InvalidName unless valid
52
- end
53
-
54
- def validate_email
55
- valid = @email =~ /@/
56
- raise Errors::InvalidEmail unless valid
57
- end
58
-
59
- def validate_password
60
- valid = @password.size > 5
61
- raise Errors::InvalidPassword unless valid
62
- end
63
-
64
- end
65
-
66
- # THE TESTS THEMSELVES
67
- #
68
5
  describe CommandClass do
69
6
 
70
- context "full CreateUser example" do
7
+ # NOTE: See create_user.rb for example usage.
8
+ #
9
+ context "CreateUser with ::Include syntax and custom errors" do
71
10
  let(:email_svc) { spy('email') }
72
11
  let(:user_repo) { spy('user_repo') }
73
12
  let(:valid_name) { 'John' }
@@ -105,13 +44,13 @@ describe CommandClass do
105
44
  it 'errors for a short name' do
106
45
  expect do
107
46
  create_user.(name: 'x', email: valid_email, password: valid_pw)
108
- end.to raise_error(Errors::InvalidName)
47
+ end.to raise_error(CreateUser::InvalidName)
109
48
  end
110
49
 
111
50
  it 'errors on an invalid email' do
112
51
  expect do
113
52
  create_user.(name: valid_email, email: 'bad_email', password: valid_pw)
114
- end.to raise_error(Errors::InvalidEmail)
53
+ end.to raise_error(CreateUser::InvalidEmail)
115
54
  end
116
55
  end
117
56
 
@@ -129,7 +68,7 @@ describe CommandClass do
129
68
  it 'errors' do
130
69
  expect do
131
70
  create_user.(name: valid_name, email: valid_email, password: valid_pw)
132
- end.to raise_error(Errors::EmailAlreadyExists)
71
+ end.to raise_error(CreateUser::EmailAlreadyExists)
133
72
  end
134
73
  end
135
74
  end
@@ -0,0 +1,74 @@
1
+ require 'rspec'
2
+ require_relative '../lib/command_class'
3
+ require_relative './create_user2'
4
+
5
+ describe CommandClass do
6
+
7
+ context "Full CreateUser2 example using legacy syntax" do
8
+ let(:email_svc) { spy('email') }
9
+ let(:user_repo) { spy('user_repo') }
10
+ let(:valid_name) { 'John' }
11
+ let(:valid_email) { 'john@gmail.com' }
12
+ let(:valid_pw) { 'secret' }
13
+
14
+ describe "happy path" do
15
+ let(:happy_repo) do
16
+ user_repo.tap do |x|
17
+ allow(x).to receive(:find_by_email).and_return(nil)
18
+ end
19
+ end
20
+
21
+ subject(:create_user) do
22
+ CreateUser2.new(user_repo: happy_repo, email_service: email_svc)
23
+ end
24
+
25
+ it 'inserts the user into the db' do
26
+ create_user.(name: valid_name, email: valid_email, password: valid_pw)
27
+ expect(user_repo).to have_received(:insert)
28
+ end
29
+
30
+ it 'sends the confirmation email' do
31
+ create_user.(name: valid_name, email: valid_email, password: valid_pw)
32
+ expect(email_svc).to have_received(:send_signup_confirmation)
33
+ end
34
+
35
+ end
36
+
37
+ describe "invalid user input" do
38
+ subject(:create_user) do
39
+ CreateUser2.new(user_repo: user_repo, email_service: email_svc)
40
+ end
41
+
42
+ it 'errors for a short name' do
43
+ expect do
44
+ create_user.(name: 'x', email: valid_email, password: valid_pw)
45
+ end.to raise_error(Errors::InvalidName)
46
+ end
47
+
48
+ it 'errors on an invalid email' do
49
+ expect do
50
+ create_user.(name: valid_email, email: 'bad_email', password: valid_pw)
51
+ end.to raise_error(Errors::InvalidEmail)
52
+ end
53
+ end
54
+
55
+ describe "existing email" do
56
+ let(:repo_with_email) do
57
+ user_repo.tap do |x|
58
+ allow(x).to receive(:find_by_email).and_return('user obj')
59
+ end
60
+ end
61
+
62
+ subject(:create_user) do
63
+ CreateUser2.new(user_repo: repo_with_email, email_service: email_svc)
64
+ end
65
+
66
+ it 'errors' do
67
+ expect do
68
+ create_user.(name: valid_name, email: valid_email, password: valid_pw)
69
+ end.to raise_error(Errors::EmailAlreadyExists)
70
+ end
71
+ end
72
+ end
73
+
74
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonah Goldstein
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-19 00:00:00.000000000 Z
11
+ date: 2019-03-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/command_class.rb
20
20
  - spec/command_class_spec.rb
21
+ - spec/old_command_class_spec.rb
21
22
  homepage: https://github.com/jonahx/command_class
22
23
  licenses:
23
24
  - MIT
@@ -44,3 +45,4 @@ specification_version: 4
44
45
  summary: Create functional command objects without boilerplate
45
46
  test_files:
46
47
  - spec/command_class_spec.rb
48
+ - spec/old_command_class_spec.rb