adama 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 4e62f6593e1502cfac772a33f167c0bbac14635c
4
- data.tar.gz: 2eda051000fc7e546346b3db4eda8a7830542dee
3
+ metadata.gz: 2e5e10093e87083de3399386195d70dc6e1aa5ea
4
+ data.tar.gz: 558937d4e7c5b20ea59017c91f00099f3e19b52e
5
5
  SHA512:
6
- metadata.gz: 8e2c2916e4431d88986df3fc430d0e09ff6a0868011cb30310c2af39b468dc542c74dc74083fdd6c8011f446355d5a9a29f6de1591578dda9618c53dbb78b711
7
- data.tar.gz: 504500aec933751018fb61dac3d943ed3e5654cca601de17063ddbe4d1ac69f20582d7e49d7740d08a4afd0edecd4e315e10acfcc7814c4a81e78c9c7da64a1e
6
+ metadata.gz: ac8141a914574111e31dfd7c74eac7d630a69cee6a47a878111563220951cacd0d8ae8edc0aa8fd1a4787b035dfcf6ad13bc5985ef1c2197d0cd2f812a771efc
7
+ data.tar.gz: 70ff0ecff1640414b2e28939adac0926bcc47edb266d4a65d77a817cb0fcdb8eb4519c4719fa25cf9f2119c0f7bd0bba35fb8e35daea088f47c091f8b64a4f68
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Commander Adama
2
2
 
3
- Commander Adama is a bare bones command pattern library inspired by Collective Idea's [Interactor](https://github.com/collectiveidea/interactor) gem.
3
+ Adama is a bare bones command pattern library inspired by Collective Idea's [Interactor](https://github.com/collectiveidea/interactor) gem.
4
4
 
5
5
  Commands are small classes that represent individual units of work. Each command is executed by a client "calling" it. An invoker is a class responsible for the execution of one or more commands.
6
6
 
@@ -38,8 +38,10 @@ At this point our command `DestroyCylons` doesn't do much. As explained above, t
38
38
  class DestroyCylons
39
39
  include Adama::Command
40
40
 
41
+ validate_presence_of :captain
42
+
41
43
  def call
42
- got_destroy_cylons(kwargs[:captain])
44
+ got_destroy_cylons(captain)
43
45
  end
44
46
 
45
47
  def rollback
@@ -48,7 +50,7 @@ class DestroyCylons
48
50
  end
49
51
  ```
50
52
 
51
- The kwargs are available within the `#call` and `#rollback` instance methods due to an `attr_reader` in the `Adama::Command` module.
53
+ Each validated attribute is available as an attr_accessor on the instance of the command, so you can reference them directly in the `#call` method. within the `#call` and `#rollback` instance methods due to an `attr_reader` in the `Adama::Command` module.
52
54
 
53
55
  ### Invoker
54
56
 
data/adama.gemspec CHANGED
@@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "bundler", "~> 1.14"
34
34
  spec.add_development_dependency "rake", "~> 10.0"
35
35
  spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "pry"
36
37
  end
data/lib/adama.rb CHANGED
@@ -1,4 +1,6 @@
1
+ require 'pry'
1
2
  require "adama/version"
2
3
  require "adama/errors"
4
+ require "adama/validator"
3
5
  require "adama/command"
4
6
  require "adama/invoker"
data/lib/adama/command.rb CHANGED
@@ -16,6 +16,7 @@ module Adama
16
16
  # Internal: Install Command's behavior in the given class.
17
17
  def self.included(base)
18
18
  base.class_eval do
19
+ prepend Validator
19
20
  extend ClassMethods
20
21
  attr_reader :kwargs
21
22
  end
@@ -23,12 +24,12 @@ module Adama
23
24
 
24
25
  module ClassMethods
25
26
  # public invoke a command
26
- def call(kwargs = {})
27
- new(kwargs).tap(&:run)
27
+ def call(**kwargs)
28
+ new(**kwargs).tap(&:run)
28
29
  end
29
30
  end
30
31
 
31
- def initialize(kwargs = {})
32
+ def initialize(**kwargs)
32
33
  @kwargs = kwargs
33
34
  end
34
35
 
@@ -0,0 +1,100 @@
1
+ module Adama
2
+ module Validator
3
+ def self.prepended(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def validates_presence_of(*attributes)
9
+ # Assign the validator if it exists, ortherwise create a new one and
10
+ # append it to the validators array
11
+ unless validator = validators.find { |v| v.is_a? PresenceValidator }
12
+ validator = PresenceValidator.new
13
+ validators << validator
14
+ end
15
+
16
+ # Append the passed in attributes. This will result in a list
17
+ # of unique attributes.
18
+ validator.merge_new_attributes(*attributes)
19
+ end
20
+
21
+ # Maintain an array of validators
22
+ def validators
23
+ @validators ||= []
24
+ end
25
+ end
26
+
27
+ # This module is meant to be prepended to another module
28
+ # Call the child class initializer first, this will set kwargs
29
+ # Then validate
30
+ def initialize(**kwargs)
31
+ super(**kwargs)
32
+ validate!
33
+ end
34
+
35
+ def errors
36
+ @errors ||= {}
37
+ end
38
+
39
+ def valid?
40
+ @valid
41
+ end
42
+
43
+ # Iterate over the validators registered, and for each validator
44
+ # call `validate!` passing in the instance of the class this module
45
+ # was prepended to.
46
+ def validate!
47
+ @valid = true
48
+ self.class.validators.each do |validator|
49
+ validator.validate! self
50
+ merge_errors validator.errors
51
+ @valid = validator.valid? && @valid
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # Create a uniform error output, per attribute
58
+ def merge_errors(new_errors)
59
+ errors.merge!(new_errors) do |key, oldval, newval|
60
+ (newval.is_a?(Array) ? (oldval + newval) : (oldval << newval)).uniq
61
+ end
62
+ end
63
+
64
+ class PresenceValidator
65
+ attr_accessor :errors
66
+
67
+ def initialize(*attributes)
68
+ @attributes = attributes.flatten
69
+ @errors = {}
70
+ end
71
+
72
+ def valid?
73
+ @valid
74
+ end
75
+
76
+ def merge_new_attributes(*attributes)
77
+ @attributes |= attributes
78
+ end
79
+
80
+ def validate!(instance)
81
+ @valid = true
82
+ kwargs = instance.kwargs
83
+ @attributes.each do |attribute|
84
+ present = kwargs.include? attribute
85
+ if present
86
+ set_instance_variable instance, attribute, kwargs[attribute]
87
+ else
88
+ @errors[attribute] = ['attribute missing']
89
+ end
90
+ @valid = present && @valid
91
+ end
92
+ end
93
+
94
+ def set_instance_variable(instance, key, value)
95
+ instance.instance_variable_set "@#{key}", value
96
+ instance.class.class_eval { attr_accessor :"#{key}" }
97
+ end
98
+ end
99
+ end
100
+ end
data/lib/adama/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Adama
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adama
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dradford
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-24 00:00:00.000000000 Z
11
+ date: 2017-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Command and Invoker pattern.
56
70
  email:
57
71
  - damienradford@gmail.com
@@ -73,6 +87,7 @@ files:
73
87
  - lib/adama/command.rb
74
88
  - lib/adama/errors.rb
75
89
  - lib/adama/invoker.rb
90
+ - lib/adama/validator.rb
76
91
  - lib/adama/version.rb
77
92
  homepage: https://github.com/bugcrowd/adama
78
93
  licenses: