pavlov 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,7 +29,6 @@ module Pavlov
29
29
  end
30
30
 
31
31
  require_relative 'pavlov/helpers'
32
- require_relative 'pavlov/utils'
33
32
  require_relative 'pavlov/access_denied'
34
33
  require_relative 'pavlov/validation_error'
35
34
  require_relative 'pavlov/validations'
@@ -37,5 +36,4 @@ require_relative 'pavlov/operation'
37
36
  require_relative 'pavlov/command'
38
37
  require_relative 'pavlov/query'
39
38
  require_relative 'pavlov/interactor'
40
- require_relative 'pavlov/entity'
41
39
  require_relative 'pavlov/version'
@@ -1,11 +1,9 @@
1
1
  require 'active_support/concern'
2
+ require_relative 'operation'
2
3
 
3
4
  module Pavlov
4
5
  module Command
5
6
  extend ActiveSupport::Concern
6
7
  include Pavlov::Operation
7
- def authorized?
8
- true
9
- end
10
8
  end
11
9
  end
@@ -7,12 +7,12 @@ module Pavlov
7
7
 
8
8
  def query name, *args
9
9
  args = add_pavlov_options args
10
- Pavlov.query name, *args, pavlov_options
10
+ Pavlov.query name, *args
11
11
  end
12
12
 
13
13
  def command name, *args
14
14
  args = add_pavlov_options args
15
- Pavlov.command name, *args, pavlov_options
15
+ Pavlov.command name, *args
16
16
  end
17
17
 
18
18
  def pavlov_options
@@ -1,5 +1,6 @@
1
1
  require 'active_support/concern'
2
2
  require 'active_support/inflector'
3
+ require 'pavlov/operation'
3
4
 
4
5
  module Pavlov
5
6
  module Interactor
@@ -16,5 +17,9 @@ module Pavlov
16
17
  @queue ||= :interactor_operations
17
18
  end
18
19
  end
20
+
21
+ def authorized?
22
+ raise NotImplementedError
23
+ end
19
24
  end
20
25
  end
@@ -1,47 +1,71 @@
1
1
  require 'active_support/concern'
2
+ require 'pavlov/validations'
3
+ require 'pavlov/helpers'
4
+ require_relative 'access_denied'
2
5
 
3
6
  module Pavlov
4
7
  module Operation
5
8
  extend ActiveSupport::Concern
6
9
  include Pavlov::Helpers
7
10
  include Pavlov::Validations
8
- include Pavlov::Utils
9
- def pavlov_options
10
- @options
11
+
12
+ def initialize(*params)
13
+ keys, names, @options = extract_arguments(params)
14
+ set_instance_variables keys, names
15
+ validate
16
+
17
+ check_authorization
18
+
19
+ finish_initialize if respond_to? :finish_initialize
11
20
  end
12
21
 
13
- def raise_unauthorized(message='Unauthorized')
14
- raise Pavlov::AccessDenied, message
22
+ def validate
23
+ if respond_to? :valid?
24
+ valid?
25
+ else
26
+ true
27
+ end
28
+ end
29
+
30
+ def call(*args, &block)
31
+ execute(*args, &block)
15
32
  end
16
33
 
17
- def check_authority
18
- raise_unauthorized unless respond_to? :authorized? and authorized?
34
+ private
35
+
36
+ def pavlov_options
37
+ @options
19
38
  end
20
39
 
21
- def initialize *params
40
+ def extract_arguments(params)
22
41
  keys = (respond_to? :arguments) ? arguments : []
23
42
  names = params.first(keys.length)
43
+
24
44
  if params.length == keys.length + 1
25
- @options = params.last
45
+ options = params.last
26
46
  elsif params.length == keys.length
27
- @options = {}
47
+ options = {}
28
48
  else
29
49
  raise "wrong number of arguments."
30
50
  end
31
51
 
52
+ [keys, names, options]
53
+ end
54
+
55
+ def set_instance_variables(keys, names)
32
56
  (keys.zip names).each do |pair|
33
57
  name = "@" + pair[0].to_s
34
58
  value = pair[1]
35
59
  instance_variable_set(name, value)
36
60
  end
61
+ end
37
62
 
38
- validate if respond_to? :validate
39
- check_authority
40
- finish_initialize if respond_to? :finish_initialize
63
+ def raise_unauthorized(message='Unauthorized')
64
+ raise Pavlov::AccessDenied, message
41
65
  end
42
66
 
43
- def call
44
- self.execute
67
+ def check_authorization
68
+ raise_unauthorized if respond_to? :authorized?, true and not authorized?
45
69
  end
46
70
 
47
71
  module ClassMethods
@@ -57,6 +81,10 @@ module Pavlov
57
81
  define_method :arguments do
58
82
  keys
59
83
  end
84
+
85
+ class_eval do
86
+ attr_reader(*keys)
87
+ end
60
88
  end
61
89
 
62
90
  # make our interactors behave as Resque jobs
@@ -1,12 +1,9 @@
1
1
  require 'active_support/concern'
2
+ require_relative 'operation'
2
3
 
3
4
  module Pavlov
4
5
  module Query
5
6
  extend ActiveSupport::Concern
6
7
  include Pavlov::Operation
7
-
8
- def authorized?
9
- true
10
- end
11
8
  end
12
9
  end
@@ -1,21 +1,18 @@
1
- require_relative 'validations/errors'
2
- require_relative 'validations/not_valid'
3
-
4
1
  module Pavlov
5
2
  module Validations
6
- def errors
7
- @errors ||= Pavlov::Validations::Errors.new
8
- end
9
-
10
3
  def validate_hexadecimal_string param_name, param
11
- raise Pavlov::ValidationError, "#{param_name.to_s} should be an hexadecimal string." unless param.is_a? String and /\A[\da-fA-F]+\Z/.match param
4
+ #warn "[DEPRECATION] `validate_hexadecimal_string` is deprecated. Please use the ActiveModel validation instead."
5
+ raise Pavlov::ValidationError, "#{param_name.to_s} should be an hexadecimal string." unless param.is_a? String and /\A[\da-fA-F]+\Z/.match param
12
6
  end
13
7
 
14
8
  def validate_regex param_name, param, regex, message
9
+ #warn "[DEPRECATION] `validate_integer` is deprecated. Please use the default format_of ActiveModel validator instead."
15
10
  raise Pavlov::ValidationError, "#{param_name.to_s} #{message}" unless regex.match param
16
11
  end
17
12
 
18
- def validate_integer param_name, param
13
+ def validate_integer param_name, param, opts = {}
14
+ #warn "[DEPRECATION] `validate_integer` is deprecated. Please use the default numericality ActiveModel validator with the :only_integer option instead."
15
+ return if opts[:allow_blank] && param.blank?
19
16
  raise Pavlov::ValidationError, "#{param_name.to_s} should be an integer." unless param.is_a? Integer
20
17
  end
21
18
 
@@ -24,14 +21,22 @@ module Pavlov
24
21
  end
25
22
 
26
23
  def validate_string param_name, param
24
+ #warn "[DEPRECATION] `validate_string` is deprecated. Please use the ActiveModel validation instead."
27
25
  raise Pavlov::ValidationError, "#{param_name.to_s} should be a string." unless param.is_a? String
28
26
  end
29
27
 
28
+ def validate_nonempty_string param_name, param
29
+ #warn "[DEPRECATION] `validate_string` is deprecated. Please use the ActiveModel validation with the :non_empty option instead."
30
+ raise Pavlov::ValidationError, "#{param_name.to_s} should be a nonempty string." unless param.is_a?(String) && not(param.nil?) && not(param.empty?)
31
+ end
32
+
30
33
  def validate_integer_string param_name, param
34
+ #warn "[DEPRECATION] `validate_integer_string` is deprecated. Please use the ActiveModel validation instead."
31
35
  raise Pavlov::ValidationError, "#{param_name.to_s} should be an integer string." unless param.is_a? String and /\A\d+\Z/.match param
32
36
  end
33
37
 
34
38
  def validate_not_nil param_name, param
39
+ #warn "[DEPRECATION] `validate_not_nil` is deprecated. Please use the default presence ActiveModel validator instead."
35
40
  raise Pavlov::ValidationError, "#{param_name.to_s} should not be nil." if param.nil?
36
41
  end
37
42
  end
@@ -2,5 +2,5 @@ module Pavlov
2
2
  # We're doing this because we might write tests that deal
3
3
  # with other versions of bundler and we are unsure how to
4
4
  # handle this better.
5
- VERSION = "0.0.1"
6
- end
5
+ VERSION = "0.1.0"
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pavlov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-01-14 00:00:00.000000000 Z
16
+ date: 2013-07-03 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activesupport
@@ -32,7 +32,23 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.2.7
34
34
  - !ruby/object:Gem::Dependency
35
- name: minitest
35
+ name: activemodel
36
+ requirement: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 3.2.7
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 3.2.7
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
36
52
  requirement: !ruby/object:Gem::Requirement
37
53
  none: false
38
54
  requirements:
@@ -48,7 +64,7 @@ dependencies:
48
64
  - !ruby/object:Gem::Version
49
65
  version: '0'
50
66
  - !ruby/object:Gem::Dependency
51
- name: minitest-stub-const
67
+ name: guard
52
68
  requirement: !ruby/object:Gem::Requirement
53
69
  none: false
54
70
  requirements:
@@ -64,7 +80,7 @@ dependencies:
64
80
  - !ruby/object:Gem::Version
65
81
  version: '0'
66
82
  - !ruby/object:Gem::Dependency
67
- name: guard
83
+ name: guard-bundler
68
84
  requirement: !ruby/object:Gem::Requirement
69
85
  none: false
70
86
  requirements:
@@ -80,7 +96,7 @@ dependencies:
80
96
  - !ruby/object:Gem::Version
81
97
  version: '0'
82
98
  - !ruby/object:Gem::Dependency
83
- name: guard-bundler
99
+ name: guard-rspec
84
100
  requirement: !ruby/object:Gem::Requirement
85
101
  none: false
86
102
  requirements:
@@ -96,7 +112,7 @@ dependencies:
96
112
  - !ruby/object:Gem::Version
97
113
  version: '0'
98
114
  - !ruby/object:Gem::Dependency
99
- name: guard-minitest
115
+ name: terminal-notifier-guard
100
116
  requirement: !ruby/object:Gem::Requirement
101
117
  none: false
102
118
  requirements:
@@ -159,6 +175,22 @@ dependencies:
159
175
  - - ! '>='
160
176
  - !ruby/object:Gem::Version
161
177
  version: '0'
178
+ - !ruby/object:Gem::Dependency
179
+ name: coveralls
180
+ requirement: !ruby/object:Gem::Requirement
181
+ none: false
182
+ requirements:
183
+ - - ! '>='
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ type: :development
187
+ prerelease: false
188
+ version_requirements: !ruby/object:Gem::Requirement
189
+ none: false
190
+ requirements:
191
+ - - ! '>='
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
162
194
  description: Pavlov is a opinionated toolbox to help you architect your Ruby project.
163
195
  email:
164
196
  - jan+pavlov@deelstra.org
@@ -169,17 +201,12 @@ files:
169
201
  - lib/pavlov.rb
170
202
  - lib/pavlov/access_denied.rb
171
203
  - lib/pavlov/command.rb
172
- - lib/pavlov/entity.rb
173
204
  - lib/pavlov/helpers.rb
174
- - lib/pavlov/helpers/safe_evaluator.rb
175
205
  - lib/pavlov/interactor.rb
176
206
  - lib/pavlov/operation.rb
177
207
  - lib/pavlov/query.rb
178
- - lib/pavlov/utils.rb
179
208
  - lib/pavlov/validation_error.rb
180
209
  - lib/pavlov/validations.rb
181
- - lib/pavlov/validations/errors.rb
182
- - lib/pavlov/validations/not_valid.rb
183
210
  - lib/pavlov/version.rb
184
211
  homepage: https://github.com/Factlink/pavlov/
185
212
  licenses: []
@@ -193,18 +220,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
193
220
  - - ! '>='
194
221
  - !ruby/object:Gem::Version
195
222
  version: '0'
196
- segments:
197
- - 0
198
- hash: -4075067472850494356
199
223
  required_rubygems_version: !ruby/object:Gem::Requirement
200
224
  none: false
201
225
  requirements:
202
226
  - - ! '>='
203
227
  - !ruby/object:Gem::Version
204
228
  version: '0'
205
- segments:
206
- - 0
207
- hash: -4075067472850494356
208
229
  requirements: []
209
230
  rubyforge_project:
210
231
  rubygems_version: 1.8.23
@@ -1,61 +0,0 @@
1
- require_relative 'helpers/safe_evaluator'
2
-
3
- module Pavlov
4
- class Entity
5
- def self.attributes *args
6
- args.each do |attribute_name|
7
- define_attribute_writer attribute_name
8
- attr_reader attribute_name
9
- end
10
- end
11
-
12
- def self.new hash = {}, &block
13
- super().send :mutate, hash, &block
14
- end
15
-
16
- def update hash = {}, &block
17
- self.dup.send :mutate, hash, &block
18
- end
19
-
20
- private
21
- def mutate hash, &block
22
- copy_hash_values hash
23
- safely_evaluate_against(&block) if block_given?
24
- validate if respond_to? :validate
25
- self
26
- end
27
-
28
- def copy_hash_values hash
29
- make_temporary_mutatable do
30
- hash.each {|key,value| send("#{key}=",value)}
31
- end
32
- end
33
-
34
- def safely_evaluate_against &block
35
- make_temporary_mutatable do
36
- caller_instance = eval "self", block.binding
37
- evaluator = Pavlov::Helpers::SafeEvaluator.new(self, caller_instance)
38
- evaluator.instance_eval(&block)
39
- end
40
- self
41
- end
42
-
43
- def make_temporary_mutatable &block
44
- @mutable = true
45
- block.call
46
- @mutable = false
47
- end
48
-
49
- def self.define_attribute_writer attribute_name
50
- define_method "#{attribute_name}=" do |new_value|
51
- raise_not_mutable unless @mutable
52
- instance_variable_symbol = "@#{attribute_name}".to_sym
53
- instance_variable_set instance_variable_symbol, new_value
54
- end
55
- end
56
-
57
- def raise_not_mutable
58
- raise "This entity is immutable, please use 'instance = #{self.class.name}.new do; self.attribute = 'value'; end' or 'instance = instance.update do; self.attribute = 'value'; end'."
59
- end
60
- end
61
- end
@@ -1,17 +0,0 @@
1
- module Pavlov
2
- module Helpers
3
- class SafeEvaluator < BasicObject
4
- def initialize target_instance, caller_instance
5
- @target_instance, @caller_instance = target_instance, caller_instance
6
- end
7
-
8
- def method_missing method_name, *args
9
- if method_name[-1] == '='
10
- @target_instance.public_send method_name, *args
11
- else
12
- @caller_instance.send method_name, *args
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,7 +0,0 @@
1
- module Pavlov
2
- module Utils
3
- def hash_with_index(index, list)
4
- list.each_with_object({}) {|u, hash| hash[u.send(index)] = u}
5
- end
6
- end
7
- end
@@ -1,21 +0,0 @@
1
- module Pavlov
2
- module Validations
3
- module Errors
4
- include Enumerable
5
-
6
- def initialize
7
- @messages = {}
8
- end
9
-
10
- def each &block
11
- @messages.each_key do |attribute|
12
- @messages[attribute.to_s].each { |error| block.call attribute, error }
13
- end
14
- end
15
-
16
- def add attribute, error_message
17
- (@messages[attribute.to_s] ||= []) << error_message
18
- end
19
- end
20
- end
21
- end
@@ -1,7 +0,0 @@
1
- module Pavlov
2
- module Validations
3
- class NotValid < StandardError
4
- attr_accessor :errors
5
- end
6
- end
7
- end