interface-dsl 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/LICENSE +21 -0
- data/README.md +88 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/interface-dsl.gemspec +31 -0
- data/lib/interface/adapter.rb +35 -0
- data/lib/interface/dsl/version.rb +5 -0
- data/lib/interface/dsl.rb +95 -0
- data/lib/interface/port_entity.rb +50 -0
- data/lib/interface/port_group.rb +14 -0
- data/lib/interface.rb +8 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e070b39851cc80259b202cd24b845d623387c7ac
|
4
|
+
data.tar.gz: 897432bcb7838f0fe391f4b2a2559fa6541c3871
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 538e69567615952afefb27d59a2ac9825685813d267eac13e4df1a8c39184b200b2c44bfb3f75ef548cbee69db8d6250db261561ef212b09572a5935000261f3
|
7
|
+
data.tar.gz: 490ab14c0b3fc4e8112b5a1a24044b8b86c7c08526fc288f7877b489c22d64435dcbb6be43689092c72c521a0cd6a4066fdf89c4351a2248da6fa6dc12676fbd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Oleksiy Kurnenkov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Interface::Dsl
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'interface-dsl'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install interface-dsl
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# Define an operation class
|
23
|
+
class MidnightCodingOperation
|
24
|
+
# Define :call factory method conveniently
|
25
|
+
extend FactoryMethods
|
26
|
+
|
27
|
+
deffactory :call
|
28
|
+
|
29
|
+
# Or do it manually
|
30
|
+
# def self.call(*args)
|
31
|
+
# new(*args).call
|
32
|
+
# end
|
33
|
+
|
34
|
+
# Implement some logic
|
35
|
+
def call
|
36
|
+
puts "Step 1: turn off the light o.o"
|
37
|
+
puts "Step 2: git add ."
|
38
|
+
puts "Step 3: git commit -m 'asta la vista... (c)'"
|
39
|
+
puts "Step 4: git push --force origin master"
|
40
|
+
puts "Step 5: fall asleep -.-"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Define a facade API class
|
45
|
+
class CodeMonkeyAPI
|
46
|
+
# Enable DSL
|
47
|
+
extend Interface::DSL
|
48
|
+
|
49
|
+
# Define some interface
|
50
|
+
interface(:midnight_api) do |api|
|
51
|
+
|
52
|
+
# Define some endpoint
|
53
|
+
api.defpoint(:emergency_push) do |op|
|
54
|
+
op.describe "Push the code, fall asleep"
|
55
|
+
op.implementation MidnightCodingOperation
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Activate your operation:
|
61
|
+
CodeMonkeyAPI.midnight_api.emergency_push.call
|
62
|
+
#
|
63
|
+
# => Step 1: turn off the light o.o
|
64
|
+
# => Step 2: git add .
|
65
|
+
# => Step 3: git commit -m 'asta la vista... (c)'
|
66
|
+
# => Step 4: git push --force origin master
|
67
|
+
# => Step 5: fall asleep -.-
|
68
|
+
#
|
69
|
+
# Easy :)
|
70
|
+
```
|
71
|
+
|
72
|
+
## Organize your APIs to honour the Law of Demeter
|
73
|
+
```ruby
|
74
|
+
CodeMonkeyAPI # top-level API
|
75
|
+
MidnightCoderAPI = CodeMonkeyAPI.midnight_api # specific API
|
76
|
+
MidnightCoderAPI.emergency_push.call # usage
|
77
|
+
```
|
78
|
+
|
79
|
+
## Development
|
80
|
+
|
81
|
+
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.
|
82
|
+
|
83
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
84
|
+
|
85
|
+
## Contributing
|
86
|
+
|
87
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/o-kurnenkov/interface-dsl.
|
88
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "interface/dsl"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'interface/dsl/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "interface-dsl"
|
8
|
+
spec.version = Interface::Dsl::VERSION
|
9
|
+
spec.authors = ["Oleksiy Kurnenkov"]
|
10
|
+
spec.email = ["oleksiy.kurnenkov@onapp.com"]
|
11
|
+
spec.licenses = ['MIT']
|
12
|
+
spec.summary = %q{Interface description DSL}
|
13
|
+
spec.description = %q{Make your Interfaces declarative!}
|
14
|
+
spec.homepage = "https://github.com/o-kurnenkov/interface-dsl"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'hashie', '~> 3.4'
|
24
|
+
spec.add_runtime_dependency 'dry-configurable', '~> 0.5'
|
25
|
+
spec.add_runtime_dependency 'factorymethods', '~> 1.0'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'factorymethods', '~> 1.0'
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Interface
|
2
|
+
class Adapter
|
3
|
+
CallingError = Class.new(StandardError)
|
4
|
+
InterfaceError = Class.new(StandardError)
|
5
|
+
|
6
|
+
extend FactoryMethods
|
7
|
+
|
8
|
+
def_factory :call
|
9
|
+
|
10
|
+
def initialize(callable)
|
11
|
+
@callable = callable
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
unless callable.respond_to?(:call)
|
16
|
+
fail(InterfaceError("#{callable.class} is not callable!"))
|
17
|
+
end
|
18
|
+
|
19
|
+
_call
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def _call
|
25
|
+
result = callable.call
|
26
|
+
if result.is_a?(Array)
|
27
|
+
status, _result = result
|
28
|
+
status == :ok || fail(ServiceError("Error while calling #{callable.class}: #{_result}"))
|
29
|
+
_result
|
30
|
+
else
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require "interface"
|
2
|
+
require "interface/dsl/version"
|
3
|
+
|
4
|
+
module Interface
|
5
|
+
module DSL
|
6
|
+
OrphanPort = Class.new(StandardError)
|
7
|
+
ImmutableInterface = Class.new(StandardError)
|
8
|
+
|
9
|
+
require "interface/port_group"
|
10
|
+
require "interface/port_entity"
|
11
|
+
|
12
|
+
def interface(name, &block)
|
13
|
+
if interfaces.key?(name)
|
14
|
+
fail(ImmutableInterface.new("Interface can't be redefined or reopened! Use .extend_api method"))
|
15
|
+
end
|
16
|
+
|
17
|
+
interfaces.merge!(name => ::Interface::PortGroup.new(name, self).tap do |group|
|
18
|
+
yield(group)
|
19
|
+
end)
|
20
|
+
end
|
21
|
+
|
22
|
+
def defpoint(name, &block)
|
23
|
+
check_top_level_enpoint_policy
|
24
|
+
|
25
|
+
points.merge!(name => define_entity(name, &block))
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(meth, *args, &block)
|
29
|
+
super unless respond_to?(:interfaces) || respond_to?(:points)
|
30
|
+
|
31
|
+
if interfaces.respond_to?(meth)
|
32
|
+
interfaces.send(meth)
|
33
|
+
elsif points.respond_to?(meth)
|
34
|
+
points.send(meth)
|
35
|
+
else
|
36
|
+
super
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# extend_api(as: 'northbound.operations.truck_load', with_class: TruckLoadAPI)
|
41
|
+
def extend_api(as: , with_class: )
|
42
|
+
# Maybe Define just a nested interface???
|
43
|
+
_merge_point = respond_to?(:points) ? points : self
|
44
|
+
|
45
|
+
_merge_point.merge!(as => with_class)
|
46
|
+
end
|
47
|
+
|
48
|
+
def doc
|
49
|
+
_name = self.respond_to?(:name) ? self.name : self.class.name
|
50
|
+
puts _name
|
51
|
+
doc_all_endpoints
|
52
|
+
interfaces.each_pair { |_, i| i.doc }
|
53
|
+
end
|
54
|
+
|
55
|
+
def interfaces
|
56
|
+
@interfaces ||= Hashie::Mash.new
|
57
|
+
end
|
58
|
+
|
59
|
+
def points
|
60
|
+
@points ||= Hashie::Mash.new
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def check_top_level_enpoint_policy
|
66
|
+
return if Interface.config.allow_top_level_api_endpoints? || !top_level?
|
67
|
+
|
68
|
+
fail(OrphanPort.new("Can not be defined as a top level Interface"))
|
69
|
+
end
|
70
|
+
|
71
|
+
def top_level?
|
72
|
+
if respond_to?(:parent)
|
73
|
+
parent.nil? && interfaces.empty?
|
74
|
+
else
|
75
|
+
interfaces.empty?
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def doc_all_endpoints
|
80
|
+
traverse_all(points, print_doc)
|
81
|
+
end
|
82
|
+
|
83
|
+
def traverse_all(collection, execute_block)
|
84
|
+
collection.each_pair { |name, i| execute_block.call(i) }
|
85
|
+
end
|
86
|
+
|
87
|
+
def print_doc
|
88
|
+
@print_doc ||= ->(i) { i.doc }
|
89
|
+
end
|
90
|
+
|
91
|
+
def define_entity(name, &block)
|
92
|
+
::Interface::PortEntity.new(name).tap { |port| yield(port) }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Interface
|
2
|
+
# TODO
|
3
|
+
# => define Schemas with dry-validation
|
4
|
+
class PortEntity < Struct.new(:name)
|
5
|
+
WTFError = Class.new(StandardError)
|
6
|
+
|
7
|
+
N_A = 'N/A'.freeze
|
8
|
+
LIM = ('-' * 48).freeze
|
9
|
+
|
10
|
+
def describe(text)
|
11
|
+
@description = text
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
if @implementation.nil?
|
16
|
+
fail(WTFError.new("WAT A HECK U DOIN'! THERE'S NO IMPLEMENTATION TO CALL!"))
|
17
|
+
end
|
18
|
+
|
19
|
+
@implementation.call
|
20
|
+
end
|
21
|
+
|
22
|
+
def implementation(klass)
|
23
|
+
@implementation = klass
|
24
|
+
end
|
25
|
+
|
26
|
+
def returns(hash)
|
27
|
+
{ success: [:ok, Object],
|
28
|
+
failure: [:error, String] }
|
29
|
+
end
|
30
|
+
|
31
|
+
#TODO
|
32
|
+
def before_call; end
|
33
|
+
def after_call; end
|
34
|
+
def wrap_call; end
|
35
|
+
|
36
|
+
def doc
|
37
|
+
puts <<-DOC
|
38
|
+
#{LIM}
|
39
|
+
Name:\t#{ name }
|
40
|
+
Desc:\t#{ @description || N_A}
|
41
|
+
Responsible:\t#{ @implementation || N_A}
|
42
|
+
Accepts:\t#{ @arguments || N_A}
|
43
|
+
Returns:
|
44
|
+
\tsuccess:\t#{ @returns && @returns.fetch(:success, N_A) || N_A }
|
45
|
+
\tfailure:\t#{ @returns && @returns.fetch(:failure, N_A) || N_A }
|
46
|
+
#{LIM}
|
47
|
+
DOC
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/interface.rb
ADDED
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: interface-dsl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oleksiy Kurnenkov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-configurable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: factorymethods
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factorymethods
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.13'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.13'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
description: Make your Interfaces declarative!
|
112
|
+
email:
|
113
|
+
- oleksiy.kurnenkov@onapp.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- interface-dsl.gemspec
|
128
|
+
- lib/interface.rb
|
129
|
+
- lib/interface/adapter.rb
|
130
|
+
- lib/interface/dsl.rb
|
131
|
+
- lib/interface/dsl/version.rb
|
132
|
+
- lib/interface/port_entity.rb
|
133
|
+
- lib/interface/port_group.rb
|
134
|
+
homepage: https://github.com/o-kurnenkov/interface-dsl
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.5.1
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Interface description DSL
|
158
|
+
test_files: []
|