assertion 0.2.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +1 -1
- data/lib/assertion.rb +8 -4
- data/lib/assertion/base.rb +5 -3
- data/lib/assertion/dsl/attribute.rb +44 -0
- data/lib/assertion/dsl/attributes.rb +54 -0
- data/lib/assertion/dsl/builder.rb +81 -0
- data/lib/assertion/dsl/caller.rb +26 -0
- data/lib/assertion/dsl/inversion.rb +36 -0
- data/lib/assertion/guard.rb +2 -7
- data/lib/assertion/inverter.rb +2 -10
- data/lib/assertion/translator.rb +1 -1
- data/lib/assertion/version.rb +1 -1
- data/spec/unit/assertion/base_spec.rb +30 -137
- data/spec/unit/assertion/dsl/attribute_spec.rb +53 -0
- data/spec/unit/assertion/dsl/attributes_spec.rb +89 -0
- data/spec/unit/assertion/dsl/builder_spec.rb +123 -0
- data/spec/unit/assertion/dsl/caller_spec.rb +23 -0
- data/spec/unit/assertion/dsl/inversion_spec.rb +18 -0
- data/spec/unit/assertion/guard_spec.rb +10 -49
- data/spec/unit/assertion/inverter_spec.rb +4 -26
- data/spec/unit/assertion/translator_spec.rb +2 -0
- data/spec/unit/assertion_spec.rb +3 -116
- metadata +17 -5
- data/lib/assertion/base_dsl.rb +0 -75
- data/lib/assertion/dsl.rb +0 -77
- data/lib/assertion/guard_dsl.rb +0 -39
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assertion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: transproc
|
@@ -107,10 +107,12 @@ files:
|
|
107
107
|
- config/metrics/yardstick.yml
|
108
108
|
- lib/assertion.rb
|
109
109
|
- lib/assertion/base.rb
|
110
|
-
- lib/assertion/
|
111
|
-
- lib/assertion/dsl.rb
|
110
|
+
- lib/assertion/dsl/attribute.rb
|
111
|
+
- lib/assertion/dsl/attributes.rb
|
112
|
+
- lib/assertion/dsl/builder.rb
|
113
|
+
- lib/assertion/dsl/caller.rb
|
114
|
+
- lib/assertion/dsl/inversion.rb
|
112
115
|
- lib/assertion/guard.rb
|
113
|
-
- lib/assertion/guard_dsl.rb
|
114
116
|
- lib/assertion/inflector.rb
|
115
117
|
- lib/assertion/invalid_error.rb
|
116
118
|
- lib/assertion/inversion.rb
|
@@ -124,6 +126,11 @@ files:
|
|
124
126
|
- spec/shared/i18n.rb
|
125
127
|
- spec/spec_helper.rb
|
126
128
|
- spec/unit/assertion/base_spec.rb
|
129
|
+
- spec/unit/assertion/dsl/attribute_spec.rb
|
130
|
+
- spec/unit/assertion/dsl/attributes_spec.rb
|
131
|
+
- spec/unit/assertion/dsl/builder_spec.rb
|
132
|
+
- spec/unit/assertion/dsl/caller_spec.rb
|
133
|
+
- spec/unit/assertion/dsl/inversion_spec.rb
|
127
134
|
- spec/unit/assertion/guard_spec.rb
|
128
135
|
- spec/unit/assertion/inflector/to_path_spec.rb
|
129
136
|
- spec/unit/assertion/inflector/to_snake_path_spec.rb
|
@@ -168,6 +175,11 @@ test_files:
|
|
168
175
|
- spec/unit/assertion/inflector/to_snake_path_spec.rb
|
169
176
|
- spec/unit/assertion/inflector/to_snake_spec.rb
|
170
177
|
- spec/unit/assertion/inflector/to_path_spec.rb
|
178
|
+
- spec/unit/assertion/dsl/builder_spec.rb
|
179
|
+
- spec/unit/assertion/dsl/caller_spec.rb
|
180
|
+
- spec/unit/assertion/dsl/inversion_spec.rb
|
181
|
+
- spec/unit/assertion/dsl/attribute_spec.rb
|
182
|
+
- spec/unit/assertion/dsl/attributes_spec.rb
|
171
183
|
- spec/unit/assertion/translator_spec.rb
|
172
184
|
- spec/unit/assertion/inverter_spec.rb
|
173
185
|
- spec/unit/assertion/inversion_spec.rb
|
data/lib/assertion/base_dsl.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Assertion
|
4
|
-
|
5
|
-
# Provides methods to describe and apply assertions
|
6
|
-
#
|
7
|
-
module BaseDSL
|
8
|
-
|
9
|
-
# Initializes an assertion with some attributes (data) and then calls it
|
10
|
-
#
|
11
|
-
# @param (see Assertion::Base.new)
|
12
|
-
#
|
13
|
-
# @return (see Assertion::Base#call)
|
14
|
-
#
|
15
|
-
def [](args = nil)
|
16
|
-
new(args).call
|
17
|
-
end
|
18
|
-
|
19
|
-
# Initializes the intermediate inverter with `new` and `[]` methods
|
20
|
-
#
|
21
|
-
# The inverter can be used to initialize the assertion, that describes
|
22
|
-
# just the opposite statement to the current one
|
23
|
-
#
|
24
|
-
# @example
|
25
|
-
# IsAdult = Assertion.about :name, :age do
|
26
|
-
# age >= 18
|
27
|
-
# end
|
28
|
-
#
|
29
|
-
# joe = { name: 'Joe', age: 19 }
|
30
|
-
#
|
31
|
-
# IsAdult[joe].valid? # => true
|
32
|
-
# IsAdult.not[joe].valid? # => false
|
33
|
-
#
|
34
|
-
# @return [Assertion::Inverter]
|
35
|
-
#
|
36
|
-
def not
|
37
|
-
Inverter.new(self)
|
38
|
-
end
|
39
|
-
|
40
|
-
# List of attributes defined for the assertion
|
41
|
-
#
|
42
|
-
# @return [Array<Symbol>]
|
43
|
-
#
|
44
|
-
def attributes
|
45
|
-
@attributes ||= []
|
46
|
-
end
|
47
|
-
|
48
|
-
# Declares new attribute(s) by name(s)
|
49
|
-
#
|
50
|
-
# @param [#to_sym, Array<#to_sym>] names
|
51
|
-
#
|
52
|
-
# @return [undefined]
|
53
|
-
#
|
54
|
-
# @raise [NameError]
|
55
|
-
# When an instance method with one of given names is already exist.
|
56
|
-
#
|
57
|
-
def attribute(*names)
|
58
|
-
names.flatten.map(&:to_sym).each(&method(:__add_attribute__))
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
def __add_attribute__(name)
|
64
|
-
__check_attribute__(name)
|
65
|
-
attributes << define_method(name) { attributes[name] }
|
66
|
-
end
|
67
|
-
|
68
|
-
def __check_attribute__(name)
|
69
|
-
return unless (instance_methods << :check).include? name
|
70
|
-
fail NameError.new "#{self}##{name} is already defined"
|
71
|
-
end
|
72
|
-
|
73
|
-
end # module BaseDSL
|
74
|
-
|
75
|
-
end # module Assertion
|
data/lib/assertion/dsl.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Assertion
|
4
|
-
|
5
|
-
# Provides methods to build assertions and guards
|
6
|
-
#
|
7
|
-
module DSL
|
8
|
-
|
9
|
-
# Builds the subclass of `Assertion::Base` with predefined `attributes`
|
10
|
-
# and implementation of the `#check` method.
|
11
|
-
#
|
12
|
-
# @example
|
13
|
-
# IsMan = Assertion.about :age, :gender do
|
14
|
-
# (age >= 18) && (gender == :male)
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# # This is the same as:
|
18
|
-
# class IsMan < Assertion::Base
|
19
|
-
# attribute :age, :gender
|
20
|
-
#
|
21
|
-
# def check
|
22
|
-
# (age >= 18) && (gender == :male)
|
23
|
-
# end
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# @param [Symbol, Array<Symbol>] attributes
|
27
|
-
# The list of attributes for the new assertion
|
28
|
-
# @param [Proc] block
|
29
|
-
# The content for the `check` method
|
30
|
-
#
|
31
|
-
# @return [Class] The specific assertion class
|
32
|
-
#
|
33
|
-
def about(*attributes, &block)
|
34
|
-
__build__(Base, attributes, :check, &block)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Builds the subclass of `Assertion::Guard` with given attribute
|
38
|
-
# (alias for the `object`) and implementation of the `#state` method.
|
39
|
-
#
|
40
|
-
# @example
|
41
|
-
# VoterOnly = Assertion.guards :user do
|
42
|
-
# IsAdult[user.attributes] & IsCitizen[user.attributes]
|
43
|
-
# end
|
44
|
-
#
|
45
|
-
# # This is the same as:
|
46
|
-
# class VoterOnly < Assertion::Guard
|
47
|
-
# alias_method :user, :object
|
48
|
-
#
|
49
|
-
# def state
|
50
|
-
# IsAdult[user.attributes] & IsCitizen[user.attributes]
|
51
|
-
# end
|
52
|
-
# end
|
53
|
-
#
|
54
|
-
# @param [Symbol] attribute
|
55
|
-
# The alias for the `object` attribute
|
56
|
-
# @param [Proc] block
|
57
|
-
# The content for the `state` method
|
58
|
-
#
|
59
|
-
# @return [Class] The specific guard class
|
60
|
-
#
|
61
|
-
def guards(attribute = nil, &block)
|
62
|
-
__build__(Guard, attribute, :state, &block)
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def __build__(type, attributes, name, &block)
|
68
|
-
klass = Class.new(type)
|
69
|
-
klass.public_send(:attribute, attributes) if attributes
|
70
|
-
klass.__send__(:define_method, name, &block) if block_given?
|
71
|
-
|
72
|
-
klass
|
73
|
-
end
|
74
|
-
|
75
|
-
end # module DSL
|
76
|
-
|
77
|
-
end # module Assertion
|
data/lib/assertion/guard_dsl.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Assertion
|
2
|
-
|
3
|
-
# Provides methods to describe and apply guards
|
4
|
-
#
|
5
|
-
module GuardDSL
|
6
|
-
|
7
|
-
# Initializes and guard for the provided object and calls it immediately
|
8
|
-
#
|
9
|
-
# @param [Object] object The object whose state should be tested
|
10
|
-
#
|
11
|
-
# @return (see Assertion::Guard#call)
|
12
|
-
#
|
13
|
-
# @raise (see Assertion::Guard#call)
|
14
|
-
#
|
15
|
-
def [](object)
|
16
|
-
new(object).call
|
17
|
-
end
|
18
|
-
|
19
|
-
# Adds alias to the [#object] method
|
20
|
-
#
|
21
|
-
# @param [#to_sym] name
|
22
|
-
#
|
23
|
-
# @return [undefined]
|
24
|
-
#
|
25
|
-
def attribute(name)
|
26
|
-
__check_attribute__(name)
|
27
|
-
alias_method name, :object
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def __check_attribute__(key)
|
33
|
-
return unless (instance_methods << :state).include? key.to_sym
|
34
|
-
fail NameError.new "#{self}##{key} is already defined"
|
35
|
-
end
|
36
|
-
|
37
|
-
end # module GuardDSL
|
38
|
-
|
39
|
-
end # module Assertion
|