usecasing 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWY3MzNkMWQ1ZDU2MDJkMWZjZTJjY2NjODBhMzkzZjU1MTlkYmUxMg==
5
+ data.tar.gz: !binary |-
6
+ ZjM1YzFhZGM2MWQ4ZjkxM2JhYTk0ZmU4YjAyYjk5MGEwZDNiZmIyMg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDg1OTBlNDhlNTAxZDU3MWViNTZjNTYyMTJiNDJlNzE3Y2MzNTdiNWJiNjc3
10
+ OWMxMzY5MzM0ZDkwNTdjNThlOWM2M2I0MDVkYWNlMWVjYjU2YzgwY2ZhNDU4
11
+ Mzg1ZGQzMmVhNzg1M2ZhNmQ2MjljZGE1MDljODRmNjAzYjBiMGQ=
12
+ data.tar.gz: !binary |-
13
+ NDYyNjhhNTM4NmFjMWU4Nzg1ZTQwZjg1NmEwNmY0YmRiMTkzZDFmYmJjN2Qz
14
+ YjNkODcxODk2MGZhYzM2OWMwYmJjNGMyYmE0YjE1YTE4NjczYWM1MTQ3NjA1
15
+ YjY5MTJhYjRjMTliNWMwYzhkZjRmZDdmOGM1NDI2NjM3NWRhZGE=
@@ -2,12 +2,41 @@ module UseCase
2
2
 
3
3
  class Context
4
4
 
5
- attr_reader :errors
5
+ class Errors
6
+
7
+ def initialize
8
+ @errors = Hash.new { |h,k| h[k.to_sym] = [] }
9
+ end
10
+
11
+ def all(delimiter= ", ", &block)
12
+ values = @errors.map {|key, value| value }.flatten
13
+ if block_given?
14
+ values.each &block
15
+ else
16
+ values.join(delimiter)
17
+ end
18
+ end
19
+
20
+ def [](index)
21
+ @errors[index.to_sym]
22
+ end
23
+
24
+ def push(key, value)
25
+ @errors[key.to_sym].push(value)
26
+ end
27
+
28
+ def empty?
29
+ @errors.keys.empty?
30
+ end
31
+
32
+ end
33
+
34
+ attr_accessor :errors
6
35
 
7
36
  def initialize(hash = {})
8
37
  raise ArgumentError.new('Must be a Hash') unless hash.is_a? ::Hash
9
38
  @values = symbolyze_keys(hash)
10
- @errors = []
39
+ @errors = Errors.new
11
40
  end
12
41
 
13
42
  def method_missing(method, *args, &block)
@@ -24,7 +53,7 @@ module UseCase
24
53
  end
25
54
 
26
55
  def failure(key, value)
27
- @errors.push({ key => value })
56
+ @errors.push(key, value)
28
57
  end
29
58
 
30
59
  private
@@ -1,3 +1,3 @@
1
1
  module Usecasing
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/spec/context_spec.rb CHANGED
@@ -45,8 +45,7 @@ describe UseCase::Context do
45
45
  it 'adds error messages to errors' do
46
46
  context = described_class.new({})
47
47
  context.failure(:email, 'email already exist')
48
- expect(context.errors.length).to eql(1)
49
- expect(context.errors.first).to eql({ email: 'email already exist' })
48
+ expect(context.errors[:email]).to eql(['email already exist'])
50
49
  end
51
50
 
52
51
  it 'fails when exist errors' do
@@ -55,5 +54,33 @@ describe UseCase::Context do
55
54
  expect(context.success?).to eql(false)
56
55
  end
57
56
 
57
+ it 'returns all messages indexed by key' do
58
+ context = described_class.new({})
59
+ context.failure(:email, 'first')
60
+ context.failure(:email, 'second')
61
+ expect(context.errors[:email]).to include('first')
62
+ expect(context.errors[:email]).to include('second')
63
+ expect(context.errors[:email].join(",")).to eql("first,second")
64
+ end
65
+
66
+ it 'returns all messages indexed by key' do
67
+ context = described_class.new({})
68
+ context.failure(:email, 'email')
69
+ context.failure(:base, 'base')
70
+ expect(context.errors.all("<br>")).to eql('email<br>base')
71
+ end
72
+
73
+ it 'returns all iterate over messages' do
74
+ context = described_class.new({})
75
+ context.failure(:email, 'email')
76
+ context.failure(:base, 'base')
77
+ @expected = ""
78
+ context.errors.all { |message| @expected.concat"<li>#{message}</li>" }
79
+ expect(@expected).to eql('<li>email</li><li>base</li>')
80
+ end
81
+
82
+
83
+
84
+
58
85
  end
59
86
 
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usecasing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
5
- prerelease:
4
+ version: 0.1.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Thiago Dantas
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-12-24 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: mocha
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -85,33 +78,26 @@ files:
85
78
  - usecasing.gemspec
86
79
  homepage: ''
87
80
  licenses: []
81
+ metadata: {}
88
82
  post_install_message:
89
83
  rdoc_options: []
90
84
  require_paths:
91
85
  - lib
92
86
  required_ruby_version: !ruby/object:Gem::Requirement
93
- none: false
94
87
  requirements:
95
88
  - - ! '>='
96
89
  - !ruby/object:Gem::Version
97
90
  version: '0'
98
- segments:
99
- - 0
100
- hash: 3815023741453968021
101
91
  required_rubygems_version: !ruby/object:Gem::Requirement
102
- none: false
103
92
  requirements:
104
93
  - - ! '>='
105
94
  - !ruby/object:Gem::Version
106
95
  version: '0'
107
- segments:
108
- - 0
109
- hash: 3815023741453968021
110
96
  requirements: []
111
97
  rubyforge_project:
112
- rubygems_version: 1.8.24
98
+ rubygems_version: 2.2.1
113
99
  signing_key:
114
- specification_version: 3
100
+ specification_version: 4
115
101
  summary: UseCase Driven Approach
116
102
  test_files:
117
103
  - spec/context_spec.rb