sebastian 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a35d63983f5e2eaedd683ffc4b2c9c3d98a957e8d8e444ba443dcad89d46f08e
4
- data.tar.gz: 30d960204eed664fd943d4836db07dec49dea38704f94ff104d93a22e83ad967
3
+ metadata.gz: 9ab2d8d9e8a7fbc7a094400734528428bf2a8e9892b3ebaecdf8294d16ee4228
4
+ data.tar.gz: 3e15780d1c06f21f3e02d81c500d86d05f66f7feb7fe6712139b8d5f64965ad5
5
5
  SHA512:
6
- metadata.gz: 884b593eddc3e221cdeb10d2e3fe4c2b60b74a6c7199ce6d764e64d0d9e4c5e02b744406ab46d0d2a96e26e55890bfe47bcec8aa410e8429a4f47654d1c07304
7
- data.tar.gz: 43bb1ebdf5e098af0e2ebc2d85e3ac433277f1e7954c599eb8f2fb68af29815239940c57c69f69d798b66cbdd0e8d6bc79fc10622adb72a61e785dd5b6ae8390
6
+ metadata.gz: fafc2308d0dfa6fa55da5ab486953941f202d5ba004fc2e0c648146227e66a8281e171371b33e99f4d77603dc28e137253db1fd71a91a572c1262e9ee8b5f4de
7
+ data.tar.gz: 89c012ce7d31517d427d995ccee0eb3b1654e8e89e4e928923fefaef13c25856d52c84dba100f963be2ab2845ef4aefb7b8aa9631328d6694921329d8c9309eb
data/README.md CHANGED
@@ -30,8 +30,8 @@ That covers the basics. Let's put it all together into a simple example that squ
30
30
 
31
31
  ```ruby
32
32
  class CreatePayment < Sebastian::Base
33
- attr_accessor :amount
34
- attr_accessor :email
33
+ param :amount
34
+ param :email
35
35
 
36
36
  validates :amount, numericality: { only_integer: true }
37
37
 
@@ -43,11 +43,11 @@ class CreatePayment < Sebastian::Base
43
43
  end
44
44
 
45
45
  def create_payment
46
- Payment.create!(customer: create_customer, amount: amount)
46
+ Payment.create(customer: create_customer, amount: amount)
47
47
  end
48
48
 
49
49
  def create_customer
50
- Customer.create!(email: email)
50
+ Customer.create(email: email)
51
51
  end
52
52
  end
53
53
  ```
@@ -66,8 +66,17 @@ result.ok?
66
66
  # => false
67
67
  result.errors.messages
68
68
  # => {:payment=>["is not a valid"]}
69
- result.value
70
- # => Sebastian::ResultHasErrorsError: Cannot call value while the service has errors, you should call #ok? first to check
69
+ result.value!
70
+ # => Sebastian::InvalidResultError: Payment is not valid
71
+ ```
72
+
73
+ You can also use `.perform!` to execute. It's like `.perform` but more dangerous. It doesn't return an result. If the result would be invalid, it will instead raise an error. But if the result would be valid, it simply returns the value.
74
+
75
+ ```ruby
76
+ CreatePayment.perform!(email: 'ciel@phantomhive.com', amount: 500)
77
+ # => "payment_created"
78
+ CreatePayment.perform!(amount: 500)
79
+ # =>
71
80
  ```
72
81
 
73
82
  ### Validations
@@ -75,11 +84,22 @@ result.value
75
84
  These validations aren't provided by Sebastian. They're from ActiveModel. You can also use any custom validations you wrote yourself in your services.
76
85
 
77
86
  ```ruby
78
- result = CreatePayment.perform(email: 'ciel@phantomhive.com', amount: '5,00')
87
+ class ValidatePayment < Sebastian::Validation
88
+ params :amount, :email
89
+
90
+ validates :email, presence: true
91
+ validates :amount, numericality: { only_integer: true }
92
+ end
93
+ ```
94
+
95
+ This works the same as `Sebastian::Base` except that it is not needed to specify `#execute`.
96
+
97
+ ```ruby
98
+ result = ValidatePayment.perform(amount: '5,00')
79
99
  result.ok?
80
100
  # => false
81
101
  result.errors.messages
82
- # => {:amount=>["is not a number"]}
102
+ # => {{:email=>["can't be blank"], :amount=>["is not a number"]}
83
103
  ```
84
104
 
85
105
  ## Development
@@ -29,6 +29,13 @@ module Sebastian
29
29
  def perform(params = {})
30
30
  new(params).perform
31
31
  end
32
+
33
+ def perform!(params = {})
34
+ new(params).perform!
35
+ end
36
+
37
+ alias params attr_accessor
38
+ alias param params
32
39
  end
33
40
 
34
41
  def initialize(params = {})
@@ -39,6 +46,10 @@ module Sebastian
39
46
  Result.new(value: valid? ? execute : nil, errors: errors)
40
47
  end
41
48
 
49
+ def perform!
50
+ perform.value!
51
+ end
52
+
42
53
  protected
43
54
 
44
55
  def execute
@@ -8,5 +8,5 @@ module Sebastian
8
8
  NotImplementedError = Class.new(Error)
9
9
 
10
10
  # Raised if value is called while there are errors.
11
- ResultHasErrorsError = Class.new(Error)
11
+ InvalidResultError = Class.new(Error)
12
12
  end
@@ -13,10 +13,10 @@ module Sebastian
13
13
  # result = Sebastian::Result.new(value: 'foo', errors: ['bar'])
14
14
  # result.ok?
15
15
  # # => false
16
- # result.value
16
+ # result.value!
17
17
  # # => Sebastian::ServiceReturnedErrorsError
18
18
  class Result
19
- attr_reader :errors
19
+ attr_reader :errors, :value
20
20
 
21
21
  def initialize(value:, errors:)
22
22
  @value = value
@@ -27,12 +27,9 @@ module Sebastian
27
27
  @errors.empty?
28
28
  end
29
29
 
30
- def value
30
+ def value!
31
31
  return @value if ok?
32
- raise(
33
- ResultHasErrorsError,
34
- 'Cannot call value while the service has errors, you should call #ok? first to check'
35
- )
32
+ raise InvalidResultError, errors.full_messages.join(', ')
36
33
  end
37
34
 
38
35
  def to_s
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sebastian
4
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sebastian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sander Tuin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-23 00:00:00.000000000 Z
11
+ date: 2018-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel