dsl_companion 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 938143a3c05df642a927d979b7f75ab214c9629c
4
- data.tar.gz: 773e18265ea09fdf70b8e37a58f83cfeef093717
3
+ metadata.gz: 3fd835850cec2614e0af45d5ef719662d26b7939
4
+ data.tar.gz: a1fc45e56860006570ffce9ff1feda47ed3db132
5
5
  SHA512:
6
- metadata.gz: 9c29fd79c1318fa522c42e69f9eb99c354ce0e0fb7fea803fd2dff142b9eb92d0ff237f001cb3dc210b7c12b0c3584941292ae1af27317814e575ce343a57a3f
7
- data.tar.gz: 58e03724c32052ccb9b28b345f91f3aab2a76b3e62ef81e5a2336e1c80318186877b63523ca180ec1570118a52d4f390a0add2c65fa7c729e9cb8fb9ffbcca80
6
+ metadata.gz: f37505af4d96812117db3e69c250e0fbebbf3c8e2bcc0d5fd1d9481579e6e2bd07bf8f20cecea0f80b7225416ba8ca4db0f6829ea5df3c4fa70dc8dba6f3e682
7
+ data.tar.gz: 4664de8232f5e4801c1cdf792964a0ef5927fddc125c49dc47ca71f135f7573ba0cc8fda2b6dcf91c04e3d86985e4afb6ce90bd71803e380044af04f201e3bfb
data/lib/dsl_companion.rb CHANGED
@@ -1,7 +1,12 @@
1
1
  require 'dsl_companion/version'
2
- require 'dsl_companion/meta_programming'
2
+ require 'dsl_companion/meta_helper'
3
3
  require 'dsl_companion/helpers/context_helper'
4
4
  require 'dsl_companion/helpers/active_record'
5
+
6
+ # Preloaded features
7
+ require 'dsl_companion/features/basic'
8
+
9
+ # The interpreter
5
10
  require 'dsl_companion/interpreter'
6
11
 
7
12
  module DSLCompanion
@@ -0,0 +1,43 @@
1
+ module DSLCompanion
2
+ module Features
3
+
4
+ module Basic
5
+
6
+ include MetaHelper
7
+
8
+ # If any method named define_<something>(*args) is in the DSL, then it provides an alternate
9
+ # generic syntax of define(:something, *args)
10
+ # @param [Object[]] args
11
+ # @param [Proc] block
12
+ # @return [Anything returned by th emethod]
13
+ def define *args, &block
14
+ extra = args.shift
15
+ method_name = "define_#{extra}"
16
+ if respond_to? method_name.to_sym
17
+ block_given? ? self.send(method_name, *args, &block) : self.send(method_name, *args)
18
+ else
19
+ block_given? ? method_missing(method_name.to_sym, *args, & block) : method_missing(method_name.to_sym, *args)
20
+ end
21
+ end
22
+
23
+ def interpreter?
24
+ self.is_a? DSLHelper::Interpreter
25
+ end
26
+
27
+ def interpreter
28
+ self
29
+ end
30
+
31
+ def logger(msg, level=:info)
32
+ if @logger.nil?
33
+ STDERR.puts "#{level.to_s.upcase}: #{msg}"
34
+ else
35
+ @logger.send level, msg
36
+ end
37
+ end
38
+
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -2,7 +2,7 @@ module DSLCompanion
2
2
 
3
3
  module ContextHelper
4
4
 
5
- def execute_within_context context=@context, &block
5
+ def execute_within_context(context=@context, &block)
6
6
  # Execute the block if any
7
7
  if block_given?
8
8
  last_saved_context = @context
@@ -18,47 +18,6 @@ module DSLCompanion
18
18
  end
19
19
  end
20
20
 
21
-
22
- # def with_dictionary &block
23
- # @within_dictionary = true
24
- #
25
- # raise "'with_dictionary' cannot be processed in this context !" unless interpreter?
26
- # execute_within_context @context, &block
27
- # ensure
28
- # @within_dictionary = false
29
- # end
30
-
31
- def define *args, &block
32
- extra = args.shift
33
- method_name = "define_#{extra}"
34
- if respond_to? method_name.to_sym
35
- block_given? ? self.send(method_name, *args, &block) : self.send(method_name, *args)
36
- else
37
- block_given? ? method_missing(method_name.to_sym, *args, & block) : method_missing(method_name.to_sym, *args)
38
- end
39
- end
40
-
41
- # def within_dictionary?
42
- # if interpreter?
43
- # @within_dictionary
44
- # else
45
- # @interpreter.within_dictionary?
46
- # end
47
- # end
48
-
49
- def interpreter?
50
- self.is_a? DSLHelper::Interpreter
51
- end
52
-
53
- def interpreter_exec_mode mode=nil
54
- if mode.nil?
55
- @interpreter.exec_mode
56
- else
57
- @interpreter.exec_mode = mode
58
- end
59
- end
60
-
61
-
62
21
  end
63
22
 
64
23
  end
@@ -1,7 +1,10 @@
1
1
  module DSLCompanion
2
+
2
3
  class Interpreter
3
4
 
4
- attr_accessor :logger
5
+ include DSLCompanion::Features::Basic
6
+
7
+ attr_writer :logger
5
8
 
6
9
  DEFAULT_EXEC_MODE=:lazy
7
10
 
@@ -59,19 +62,10 @@ module DSLCompanion
59
62
  message += ' is unknown'
60
63
  message += " within DSL file: '#{@source_code_file}'" unless @source_code_file.nil?
61
64
  message += '.'
62
- report message
65
+ logger message, :error unless exec_strict_mode?
63
66
  raise message if exec_strict_mode?
64
67
  end
65
68
 
66
- private
67
-
68
- def report(msg)
69
- if logger.nil?
70
- puts msg
71
- else
72
- logger.error msg
73
- end
74
- end
75
-
76
69
  end
70
+
77
71
  end
@@ -0,0 +1,38 @@
1
+ module MetaHelper
2
+
3
+ def metaclass
4
+ class << self
5
+ self
6
+ end
7
+ end
8
+
9
+ def meta_eval &block
10
+ metaclass.instance_eval &block
11
+ end
12
+
13
+ # Adds method to metaclass
14
+ def meta_def( name, &block )
15
+ meta_eval { define_method name, &block }
16
+ end
17
+
18
+ # Defines an instance method within a class
19
+ def class_def( name, &block )
20
+ class_eval { define_method name, &block }
21
+ end
22
+
23
+ def inject_variable(name, value)
24
+ # Inject instance variable in the current context
25
+ injected_accessor = name.to_s.to_sym
26
+ injected_instance_variable = "@#{injected_accessor}"
27
+ already_defined = self.instance_variable_defined? injected_instance_variable
28
+ logger("DSL Interpreter overriding existing variable '#{injected_instance_variable}'", :warn) if already_defined
29
+ self.instance_variable_set injected_instance_variable, value
30
+
31
+ # Defines the method that returns the instance variable and inject into the interpreter's context
32
+ meta_def "#{injected_accessor}" do
33
+ self.instance_variable_get injected_instance_variable
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -1,3 +1,3 @@
1
1
  module DSLCompanion
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -13,22 +13,49 @@ describe DSLCompanion::Interpreter do
13
13
  end
14
14
 
15
15
  it 'should allow to add extra features through modules' do
16
-
17
- module ExtraModule
16
+ module ExtraFeatureModule
18
17
  def extra_dsl_command
19
18
 
20
19
  end
21
20
  end
22
21
 
23
22
  interpreter = subject.new :strict
24
- interpreter.add_feature ExtraModule
23
+ interpreter.add_feature ExtraFeatureModule
25
24
 
26
25
  expect(interpreter.respond_to? :extra_dsl_command).to be_truthy
27
26
  expect {interpreter.run {extra_dsl_command} }.not_to raise_error
28
27
  expect {interpreter.run {unknown_command} }.to raise_error
28
+ end
29
+
30
+ it 'should give access to some convenience methods' do
31
+ interpreter = subject.new
32
+ expect(interpreter.respond_to? :define).to be_truthy
33
+ expect(interpreter.respond_to? :interpreter).to be_truthy
34
+ expect(interpreter.respond_to? :interpreter?).to be_truthy
35
+ end
36
+
37
+ it 'should be able to inject new variables in interpreter' do
38
+
39
+ module ExtraFeatureModule
40
+ def define_stuff stuff_name, value
41
+ interpreter.inject_variable stuff_name, value
42
+ end
43
+ end
44
+
45
+ interpreter = subject.new :strict
46
+ interpreter.add_feature ExtraFeatureModule
47
+
48
+ interpreter.run do
49
+ define_stuff :pipo, 'Hello world'
50
+ end
51
+ expect {interpreter.run {typo == 'Hello world'} }.to raise_error
52
+ expect {interpreter.run {pipo == 'Hello world'} }.not_to raise_error
29
53
 
30
54
  end
31
55
 
56
+
57
+
58
+
32
59
  context 'when in lazy mode' do
33
60
 
34
61
  it 'should only report through logging errors in the DSL' do
@@ -43,7 +70,6 @@ describe DSLCompanion::Interpreter do
43
70
  it 'should raise exceptions if errors in the DSL'do
44
71
  interpreter = subject.new :strict
45
72
  expect {interpreter.run {unknown_command} }.to raise_error
46
-
47
73
  end
48
74
 
49
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsl_companion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurent B.
@@ -67,11 +67,11 @@ files:
67
67
  - Rakefile
68
68
  - dsl_companion.gemspec
69
69
  - lib/dsl_companion.rb
70
+ - lib/dsl_companion/features/basic.rb
70
71
  - lib/dsl_companion/helpers/active_record.rb
71
72
  - lib/dsl_companion/helpers/context_helper.rb
72
- - lib/dsl_companion/injection.rb
73
73
  - lib/dsl_companion/interpreter.rb
74
- - lib/dsl_companion/meta_programming.rb
74
+ - lib/dsl_companion/meta_helper.rb
75
75
  - lib/dsl_companion/version.rb
76
76
  - spec/interpreter_spec.rb
77
77
  - spec/spec_helper.rb
@@ -1,22 +0,0 @@
1
- module DSLCompanion
2
-
3
- module Injection
4
-
5
- def inject(var_name, target)
6
- # Inject instance variable in the interpreter context
7
- injected_accessor = name.to_s.gsub(/^\s+/, '').tr(' ', '_').underscore.to_sym
8
- injected_instance_variable = "@#{injected_accessor}"
9
- already_defined = self.instance_variable_defined? injected_instance_variable
10
- Rails.logger.warn("DSL Interpreter: Overriding existing variable '#{injected_instance_variable}'") if already_defined
11
- self.instance_variable_set injected_instance_variable, target
12
-
13
- # Defines the method that returns the instance variable and inject into the interpreter's context
14
- meta_def "#{injected_accessor}" do
15
- self.instance_variable_get injected_instance_variable
16
- end
17
- end
18
-
19
- end
20
-
21
- end
22
-
@@ -1,22 +0,0 @@
1
- class Object
2
-
3
- def metaclass
4
- class << self
5
- self
6
- end
7
- end
8
-
9
- def meta_eval &block
10
- metaclass.instance_eval &block
11
- end
12
-
13
- # Adds method to metaclass
14
- def meta_def( name, &block )
15
- meta_eval { define_method name, &block }
16
- end
17
-
18
- # Defines an instance method within a class
19
- def class_def( name, &block )
20
- class_eval { define_method name, &block }
21
- end
22
- end