interface-dsl 0.1.3 → 0.1.4
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/Gemfile +1 -0
- data/README.md +73 -5
- data/interface-dsl.gemspec +3 -0
- data/lib/interface/dsl/version.rb +1 -1
- data/lib/interface/dsl.rb +2 -2
- data/lib/interface/port_entity.rb +20 -8
- data/lib/interface.rb +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a08f65100277da590913ecc15c42d7c5428a8d42
|
4
|
+
data.tar.gz: 17227276991c60ae9f4e9dee377492666c0a085d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b430dfe570938c708800edb0af3acab804552a4ca390167f560cd00b2ad679e75b1f20f9fc704eb9bd57339565c015c82fcf66cf3fc5e20610c01a404921001
|
7
|
+
data.tar.gz: 8208b4c19043b6328d0d924ecc084685828d1759d98c4c1c9ef1b477095b8c86c752ec9d2bcb7c5bcbbb0d1dd70a78c5f4dfc7e2503a9e11dfd9307df1eccf3e
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Interface::
|
1
|
+
# Interface::DSL
|
2
2
|
|
3
3
|
## Installation
|
4
4
|
|
@@ -47,12 +47,12 @@ class CodeMonkeyAPI
|
|
47
47
|
extend Interface::DSL
|
48
48
|
|
49
49
|
# Define some interface
|
50
|
-
interface(:midnight_api) do
|
50
|
+
interface(:midnight_api) do
|
51
51
|
|
52
52
|
# Define some endpoint
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
defpoint(:emergency_push) do |op|
|
54
|
+
describe "Push the code, fall asleep"
|
55
|
+
handler MidnightCodingOperation
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -76,6 +76,74 @@ MidnightCoderAPI = CodeMonkeyAPI.midnight_api # specific API
|
|
76
76
|
MidnightCoderAPI.emergency_push.call # usage
|
77
77
|
```
|
78
78
|
|
79
|
+
## [HOT] Extend your API at any relevant point
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
# Our BASE API class
|
83
|
+
class HumanoidAPI
|
84
|
+
# Enable DSL
|
85
|
+
extend Interface::DSL
|
86
|
+
|
87
|
+
# declare main API
|
88
|
+
interface(:base_functions) do
|
89
|
+
defpoint(:jump) do
|
90
|
+
describe "Do jump!"
|
91
|
+
handler BaseRobotJump
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# API Extension class
|
97
|
+
class JumpExtension
|
98
|
+
# Enable DSL
|
99
|
+
extend Interface::DSL
|
100
|
+
|
101
|
+
# declare extended API
|
102
|
+
interface(:jumping) do
|
103
|
+
defpoint(:on_one_leg) do
|
104
|
+
describe "Carefully jumps on one leg"
|
105
|
+
handler RobotJumpingMaster
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
Current legitimate API for HumanoidAPI is
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
HumanoidAPI.base_functions.jump
|
115
|
+
```
|
116
|
+
|
117
|
+
Let's extend it with `JumpExtension`
|
118
|
+
```ruby
|
119
|
+
HumanoidAPI.base_functions.extend_api(as: 'weird', with_class: JumpExtension)
|
120
|
+
```
|
121
|
+
|
122
|
+
We've just declaratively extended our API to
|
123
|
+
```ruby
|
124
|
+
HumanoidAPI.base_functions.jump
|
125
|
+
HumanoidAPI.base_functions.weird.jumping.on_one_leg
|
126
|
+
```
|
127
|
+
|
128
|
+
## [WIP] Declare contract for input data by means of dry-validation (available in [input-validation branch](https://github.com/o-kurnenkov/interface-dsl/tree/input-validation))
|
129
|
+
```ruby
|
130
|
+
interface(:midnight_api) do
|
131
|
+
defpoint(:emergency_push) do
|
132
|
+
describe "Push the code, fall asleep"
|
133
|
+
handler MidnightCodingOperation
|
134
|
+
|
135
|
+
# contract \
|
136
|
+
contract Dry::Validation.Schema do
|
137
|
+
required(:path_to_bed).schema do
|
138
|
+
required(:room).filled
|
139
|
+
end
|
140
|
+
end
|
141
|
+
# contract /
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
|
79
147
|
## Development
|
80
148
|
|
81
149
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/interface-dsl.gemspec
CHANGED
@@ -16,10 +16,13 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
17
|
f.match(%r{^(test|spec|features)/})
|
18
18
|
end
|
19
|
+
|
19
20
|
spec.bindir = "exe"
|
20
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
22
|
spec.require_paths = ["lib"]
|
22
23
|
|
24
|
+
spec.add_runtime_dependency 'dry-validation'
|
25
|
+
|
23
26
|
spec.add_runtime_dependency 'hashie', '~> 3.0'
|
24
27
|
spec.add_runtime_dependency 'dry-configurable', '~> 0.3'
|
25
28
|
spec.add_runtime_dependency 'factorymethods', '~> 1.0'
|
data/lib/interface/dsl.rb
CHANGED
@@ -15,7 +15,7 @@ module Interface
|
|
15
15
|
end
|
16
16
|
|
17
17
|
interfaces.merge!(name => ::Interface::PortGroup.new(name, self).tap do |group|
|
18
|
-
|
18
|
+
group.instance_eval(&block)
|
19
19
|
end)
|
20
20
|
end
|
21
21
|
|
@@ -89,7 +89,7 @@ module Interface
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def define_entity(name, &block)
|
92
|
-
::Interface::PortEntity.new(name).tap { |port|
|
92
|
+
::Interface::PortEntity.new(name).tap { |port| port.instance_eval(&block) }
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
@@ -2,7 +2,8 @@ module Interface
|
|
2
2
|
# TODO
|
3
3
|
# => define Schemas with dry-validation
|
4
4
|
class PortEntity < Struct.new(:name)
|
5
|
-
WTFError
|
5
|
+
WTFError = Class.new(StandardError)
|
6
|
+
InvalidInputError = Class.new(StandardError)
|
6
7
|
|
7
8
|
N_A = 'N/A'.freeze
|
8
9
|
LIM = ('-' * 48).freeze
|
@@ -11,16 +12,27 @@ module Interface
|
|
11
12
|
@description = text
|
12
13
|
end
|
13
14
|
|
14
|
-
def call
|
15
|
-
if @
|
16
|
-
fail(WTFError.new("WAT A HECK U DOIN'! THERE'S NO
|
15
|
+
def call(*args, &block)
|
16
|
+
if @handler.nil?
|
17
|
+
fail(WTFError.new("WAT A HECK U DOIN'! THERE'S NO HANDLER TO CALL!"))
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
+
if !@contract.nil?
|
21
|
+
fail(InvalidInputError.new("Empty argument list doesn not comply with the Contract")) if args.empty?
|
22
|
+
|
23
|
+
errors = @contract.call(*args).errors
|
24
|
+
fail(InvalidInputError.new(errors)) if errors.any?
|
25
|
+
end
|
26
|
+
|
27
|
+
@handler.call(*args, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def handler(klass)
|
31
|
+
@handler = klass
|
20
32
|
end
|
21
33
|
|
22
|
-
def
|
23
|
-
@
|
34
|
+
def contract(validation_schema)
|
35
|
+
@contract = validation_schema
|
24
36
|
end
|
25
37
|
|
26
38
|
def returns(hash)
|
@@ -38,7 +50,7 @@ module Interface
|
|
38
50
|
#{LIM}
|
39
51
|
Name:\t#{ name }
|
40
52
|
Desc:\t#{ @description || N_A}
|
41
|
-
Responsible:\t#{ @
|
53
|
+
Responsible:\t#{ @handler || N_A}
|
42
54
|
Accepts:\t#{ @arguments || N_A}
|
43
55
|
Returns:
|
44
56
|
\tsuccess:\t#{ @returns && @returns.fetch(:success, N_A) || N_A }
|
data/lib/interface.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interface-dsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleksiy Kurnenkov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-validation
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: hashie
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|