dsl_compose 1.0.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +300 -3
- data/lib/dsl_compose/composer.rb +74 -0
- data/lib/dsl_compose/dsl/arguments/argument/equal_to_validation.rb +25 -0
- data/lib/dsl_compose/dsl/arguments/argument/format_validation.rb +25 -0
- data/lib/dsl_compose/dsl/arguments/argument/greater_than_or_equal_to_validation.rb +35 -0
- data/lib/dsl_compose/dsl/arguments/argument/greater_than_validation.rb +35 -0
- data/lib/dsl_compose/dsl/arguments/argument/in_validation.rb +35 -0
- data/lib/dsl_compose/dsl/arguments/argument/interpreter.rb +86 -0
- data/lib/dsl_compose/dsl/arguments/argument/length_validation.rb +42 -0
- data/lib/dsl_compose/dsl/arguments/argument/less_than_or_equal_to_validation.rb +35 -0
- data/lib/dsl_compose/dsl/arguments/argument/less_than_validation.rb +35 -0
- data/lib/dsl_compose/dsl/arguments/argument/not_in_validation.rb +35 -0
- data/lib/dsl_compose/dsl/arguments/argument.rb +299 -0
- data/lib/dsl_compose/dsl/arguments.rb +113 -0
- data/lib/dsl_compose/dsl/dsl_method/interpreter.rb +57 -0
- data/lib/dsl_compose/dsl/dsl_method.rb +143 -0
- data/lib/dsl_compose/dsl/interpreter.rb +72 -0
- data/lib/dsl_compose/dsl.rb +152 -0
- data/lib/dsl_compose/dsls.rb +80 -0
- data/lib/dsl_compose/interpreter/execution/arguments.rb +145 -0
- data/lib/dsl_compose/interpreter/execution/method_calls/method_call.rb +53 -0
- data/lib/dsl_compose/interpreter/execution/method_calls.rb +25 -0
- data/lib/dsl_compose/interpreter/execution.rb +64 -0
- data/lib/dsl_compose/interpreter.rb +50 -0
- data/lib/dsl_compose/version.rb +2 -2
- data/lib/dsl_compose.rb +35 -4
- metadata +26 -3
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DSLCompose
|
4
|
+
class Interpreter
|
5
|
+
class Execution
|
6
|
+
class MethodIsUniqueError < StandardError
|
7
|
+
def message
|
8
|
+
"This method is unique and can only be called once within this DSL"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class RequiredMethodNotCalledError < StandardError
|
13
|
+
def message
|
14
|
+
"This method is required, but was not called within this DSL"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :dsl
|
19
|
+
attr_reader :klass
|
20
|
+
attr_reader :method_calls
|
21
|
+
attr_reader :arguments
|
22
|
+
|
23
|
+
# execute/process a dynamically defined DSL
|
24
|
+
def initialize klass, dsl, *args, &block
|
25
|
+
@klass = klass
|
26
|
+
@dsl = dsl
|
27
|
+
@method_calls = MethodCalls.new
|
28
|
+
@arguments = Arguments.new(dsl.arguments, *args)
|
29
|
+
|
30
|
+
# dynamically process the DSL by calling the provided block
|
31
|
+
# all methods executions will be caught and processed by the method_missing method below
|
32
|
+
if block
|
33
|
+
instance_eval(&block)
|
34
|
+
end
|
35
|
+
|
36
|
+
# assert that all required methods have been called at least once
|
37
|
+
dsl.required_dsl_methods.each do |dsl_method|
|
38
|
+
unless @method_calls.method_called? dsl_method.name
|
39
|
+
raise RequiredMethodNotCalledError
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# catch and process any method calls within the DSL block
|
47
|
+
def method_missing method_name, *args, &block
|
48
|
+
# if the method does not exist, then this will raise a MethodDoesNotExistError
|
49
|
+
dsl_method = @dsl.dsl_method method_name
|
50
|
+
|
51
|
+
# if the method is unique, then it can only be called once per DSL
|
52
|
+
if dsl_method.unique? && @method_calls.method_called?(method_name)
|
53
|
+
raise MethodIsUniqueError
|
54
|
+
end
|
55
|
+
|
56
|
+
@method_calls.add_method_call dsl_method, *args, &block
|
57
|
+
end
|
58
|
+
|
59
|
+
def respond_to_missing?(method_name, *args)
|
60
|
+
@dsl.has_dsl_method? method_name
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DSLCompose
|
4
|
+
# The class is reponsible for parsing and executing a dynamic DSL (dynamic DSLs are
|
5
|
+
# created using the DSLCompose::DSL class).
|
6
|
+
class Interpreter
|
7
|
+
# A dynamic DSL can be used multiple times on the same class, each time the DSL is used
|
8
|
+
# a corresponding execution will be created. The execution contains the resulting configuration
|
9
|
+
# from that particular use of the DSL.
|
10
|
+
attr_reader :executions
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@executions = []
|
14
|
+
end
|
15
|
+
|
16
|
+
# Execute/process a dynamically defined DSL on a class.
|
17
|
+
# `klass` is the class in which the DSL is being used, not
|
18
|
+
# the class in which the DSL was defined.
|
19
|
+
def execute_dsl klass, dsl, *args, &block
|
20
|
+
execution = Execution.new(klass, dsl, *args, &block)
|
21
|
+
@executions << execution
|
22
|
+
execution
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns an array of all executions for a given class.
|
26
|
+
def class_executions klass
|
27
|
+
@executions.filter { |e| e.klass == klass }
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns an array of all executions for a given class.
|
31
|
+
def dsl_executions dsl_name
|
32
|
+
@executions.filter { |e| e.dsl.name == dsl_name }
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_h dsl_name
|
36
|
+
h = {}
|
37
|
+
dsl_executions(dsl_name).each do |execution|
|
38
|
+
h[execution.klass] ||= {
|
39
|
+
arguments: execution.arguments.to_h,
|
40
|
+
method_calls: {}
|
41
|
+
}
|
42
|
+
execution.method_calls.method_calls.each do |method_call|
|
43
|
+
h[execution.klass][:method_calls][method_call.method_name] ||= []
|
44
|
+
h[execution.klass][:method_calls][method_call.method_name] << method_call.to_h
|
45
|
+
end
|
46
|
+
end
|
47
|
+
h
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/dsl_compose/version.rb
CHANGED
data/lib/dsl_compose.rb
CHANGED
@@ -1,8 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "dsl_compose/version"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
require "dsl_compose/dsl/arguments/argument/equal_to_validation"
|
6
|
+
require "dsl_compose/dsl/arguments/argument/format_validation"
|
7
|
+
require "dsl_compose/dsl/arguments/argument/greater_than_or_equal_to_validation"
|
8
|
+
require "dsl_compose/dsl/arguments/argument/greater_than_validation"
|
9
|
+
require "dsl_compose/dsl/arguments/argument/in_validation"
|
10
|
+
require "dsl_compose/dsl/arguments/argument/length_validation"
|
11
|
+
require "dsl_compose/dsl/arguments/argument/less_than_or_equal_to_validation"
|
12
|
+
require "dsl_compose/dsl/arguments/argument/less_than_validation"
|
13
|
+
require "dsl_compose/dsl/arguments/argument/not_in_validation"
|
14
|
+
|
15
|
+
require "dsl_compose/dsl/arguments/argument/interpreter"
|
16
|
+
require "dsl_compose/dsl/arguments/argument"
|
17
|
+
require "dsl_compose/dsl/arguments"
|
18
|
+
|
19
|
+
require "dsl_compose/dsl/dsl_method/interpreter"
|
20
|
+
require "dsl_compose/dsl/dsl_method"
|
21
|
+
|
22
|
+
require "dsl_compose/dsl/interpreter"
|
23
|
+
|
24
|
+
require "dsl_compose/dsl"
|
25
|
+
|
26
|
+
require "dsl_compose/interpreter/execution/method_calls/method_call"
|
27
|
+
require "dsl_compose/interpreter/execution/method_calls"
|
28
|
+
require "dsl_compose/interpreter/execution/arguments"
|
29
|
+
require "dsl_compose/interpreter/execution"
|
30
|
+
require "dsl_compose/interpreter"
|
31
|
+
|
32
|
+
require "dsl_compose/composer"
|
33
|
+
|
34
|
+
require "dsl_compose/dsls"
|
35
|
+
|
36
|
+
module DSLCompose
|
37
|
+
class Error < StandardError
|
38
|
+
end
|
8
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dsl_compose
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Craig Ulliott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby gem to add dynamic DSLs to classes. DSLs are added to classes by
|
14
14
|
including the DSLCompose module, and then calling the add_dsl singleton method within
|
@@ -24,6 +24,29 @@ files:
|
|
24
24
|
- LICENSE.txt
|
25
25
|
- README.md
|
26
26
|
- lib/dsl_compose.rb
|
27
|
+
- lib/dsl_compose/composer.rb
|
28
|
+
- lib/dsl_compose/dsl.rb
|
29
|
+
- lib/dsl_compose/dsl/arguments.rb
|
30
|
+
- lib/dsl_compose/dsl/arguments/argument.rb
|
31
|
+
- lib/dsl_compose/dsl/arguments/argument/equal_to_validation.rb
|
32
|
+
- lib/dsl_compose/dsl/arguments/argument/format_validation.rb
|
33
|
+
- lib/dsl_compose/dsl/arguments/argument/greater_than_or_equal_to_validation.rb
|
34
|
+
- lib/dsl_compose/dsl/arguments/argument/greater_than_validation.rb
|
35
|
+
- lib/dsl_compose/dsl/arguments/argument/in_validation.rb
|
36
|
+
- lib/dsl_compose/dsl/arguments/argument/interpreter.rb
|
37
|
+
- lib/dsl_compose/dsl/arguments/argument/length_validation.rb
|
38
|
+
- lib/dsl_compose/dsl/arguments/argument/less_than_or_equal_to_validation.rb
|
39
|
+
- lib/dsl_compose/dsl/arguments/argument/less_than_validation.rb
|
40
|
+
- lib/dsl_compose/dsl/arguments/argument/not_in_validation.rb
|
41
|
+
- lib/dsl_compose/dsl/dsl_method.rb
|
42
|
+
- lib/dsl_compose/dsl/dsl_method/interpreter.rb
|
43
|
+
- lib/dsl_compose/dsl/interpreter.rb
|
44
|
+
- lib/dsl_compose/dsls.rb
|
45
|
+
- lib/dsl_compose/interpreter.rb
|
46
|
+
- lib/dsl_compose/interpreter/execution.rb
|
47
|
+
- lib/dsl_compose/interpreter/execution/arguments.rb
|
48
|
+
- lib/dsl_compose/interpreter/execution/method_calls.rb
|
49
|
+
- lib/dsl_compose/interpreter/execution/method_calls/method_call.rb
|
27
50
|
- lib/dsl_compose/version.rb
|
28
51
|
homepage:
|
29
52
|
licenses:
|
@@ -39,7 +62,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
62
|
requirements:
|
40
63
|
- - ">="
|
41
64
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
65
|
+
version: 3.0.0
|
43
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
67
|
requirements:
|
45
68
|
- - ">="
|