dsl_companion 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 938143a3c05df642a927d979b7f75ab214c9629c
4
+ data.tar.gz: 773e18265ea09fdf70b8e37a58f83cfeef093717
5
+ SHA512:
6
+ metadata.gz: 9c29fd79c1318fa522c42e69f9eb99c354ce0e0fb7fea803fd2dff142b9eb92d0ff237f001cb3dc210b7c12b0c3584941292ae1af27317814e575ce343a57a3f
7
+ data.tar.gz: 58e03724c32052ccb9b28b345f91f3aab2a76b3e62ef81e5a2336e1c80318186877b63523ca180ec1570118a52d4f390a0add2c65fa7c729e9cb8fb9ffbcca80
data/.gitignore ADDED
@@ -0,0 +1,48 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ /pkg/
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ # Standard
20
+ *~
21
+ # Standard Rails project
22
+ /tmp/
23
+ /log/
24
+ /db/*.sqlite3
25
+ # SASS CSS generation
26
+ /public/stylesheets/.sass-cache/
27
+ # Netbeans
28
+ /nbproject/
29
+ # Sublime Text 2 project
30
+ *.sublime-project
31
+ *.sublime-workspace
32
+ *(copie)*
33
+ # RVM
34
+ .rvmrc
35
+ # VisualRuby
36
+ .vr_settings.yaml
37
+ # Emacs
38
+ *#
39
+ *\#
40
+ \#*
41
+ .#*
42
+ \#*\#
43
+ # Geany
44
+ *.geany
45
+ # RubyMine
46
+ .idea
47
+ #RedCar
48
+ .redcar
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script:
5
+ - bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dsl_companion.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Laurent B.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # DslHelper
2
+ # EasyAppHelper
3
+
4
+ [![Build Status](https://travis-ci.org/lbriais/dsl_companion.png?branch=master)](https://travis-ci.org/lbriais/dsl_companion)
5
+ [![Gem Version](https://badge.fury.io/rb/dsl_companion.png)](http://badge.fury.io/rb/dsl_companion)
6
+
7
+ TODO: Write a gem description
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'dsl_helper'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dsl_helper
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( http://github.com/<my-github-username>/dsl_helper/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dsl_companion/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dsl_companion"
8
+ spec.version = DSLCompanion::VERSION
9
+ spec.authors = ["Laurent B."]
10
+ spec.email = ["lbnetid+gh@gmail.com"]
11
+ spec.summary = %q{Provides a customizable interpreter to run your own internal DSLs.}
12
+ spec.description = %q{The goal of this gem is to provide a versatile DSL interpreter.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,8 @@
1
+ require 'dsl_companion/version'
2
+ require 'dsl_companion/meta_programming'
3
+ require 'dsl_companion/helpers/context_helper'
4
+ require 'dsl_companion/helpers/active_record'
5
+ require 'dsl_companion/interpreter'
6
+
7
+ module DSLCompanion
8
+ end
@@ -0,0 +1,30 @@
1
+ module DSLCompanion
2
+
3
+ module ActiveRecordAttributesExposer
4
+ def add_dsl_attributes(record)
5
+ extra_module = nil
6
+ record.class.columns.each do |col|
7
+ # Remove the columns we don't want to give direct access to.
8
+ next if col.primary
9
+ next if col.name =~ /_id$/i
10
+ next if col.name =~ /^(created|updated)_at$/i
11
+
12
+ # Creating a module to ease the DSL, exposing AR attributes as methods.
13
+ # When in a do block attributes can be directly modified without specifying "self".
14
+ extra_module ||= Module.new
15
+ extra_module.module_eval do
16
+ define_method(col.name.to_sym) do |arg=nil|
17
+ if arg.nil?
18
+ self[col.name]
19
+ else
20
+ self[col.name] = arg
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ # And then include the module into the instance if needed
27
+ record.extend extra_module unless extra_module.nil?
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,64 @@
1
+ module DSLCompanion
2
+
3
+ module ContextHelper
4
+
5
+ def execute_within_context context=@context, &block
6
+ # Execute the block if any
7
+ if block_given?
8
+ last_saved_context = @context
9
+ @context = context
10
+ begin
11
+ # puts ">>>> Switching to context: #{@context} (from #{last_saved_context})"
12
+ @context.send :instance_variable_set, '@interpreter', @interpreter
13
+ @context.instance_eval(&block)
14
+ ensure
15
+ @context = last_saved_context
16
+ # puts "<<<< Back to context: #{@context}"
17
+ end
18
+ end
19
+ end
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
+ end
63
+
64
+ end
@@ -0,0 +1,22 @@
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
+
@@ -0,0 +1,77 @@
1
+ module DSLCompanion
2
+ class Interpreter
3
+
4
+ attr_accessor :logger
5
+
6
+ DEFAULT_EXEC_MODE=:lazy
7
+
8
+ def initialize(exec_mode=DEFAULT_EXEC_MODE)
9
+ @interpreter = self
10
+ self.exec_mode = exec_mode
11
+ end
12
+
13
+
14
+ def run file=nil, &block
15
+ @context = self
16
+ if file.nil?
17
+ self.instance_eval &block
18
+ else
19
+ @source_code_file = file
20
+ code = File.read file
21
+ self.instance_eval code
22
+ end
23
+ ensure
24
+ @last_source_code_file = @source_code_file
25
+ @source_code_file = nil
26
+ end
27
+
28
+ def self.run file=nil, exec_mode=DEFAULT_EXEC_MODE, &block
29
+ new(exec_mode).run file, &block
30
+ end
31
+
32
+
33
+ def exec_mode=(mode)
34
+ if [:strict, true].include? mode
35
+ @exec_mode = :strict
36
+ return @exec_mode
37
+ end
38
+ if [:lazy, false].include? mode
39
+ @exec_mode = :lazy
40
+ return @exec_mode
41
+ end
42
+ raise "DSL Interpreter: Invalid execution mode !"
43
+ end
44
+ attr_reader :exec_mode
45
+
46
+
47
+ def exec_strict_mode?
48
+ @exec_mode == :strict
49
+ end
50
+
51
+ def add_feature(mod)
52
+ self.extend(mod)
53
+ end
54
+
55
+ def method_missing(symbolic_name, *args)
56
+ method_name ||= symbolic_name.to_s
57
+ message = "DSL Interpreter: method '#{method_name}'"
58
+ message += block_given? ? ' (with a code block passed).' : ' (with no code block passed)'
59
+ message += ' is unknown'
60
+ message += " within DSL file: '#{@source_code_file}'" unless @source_code_file.nil?
61
+ message += '.'
62
+ report message
63
+ raise message if exec_strict_mode?
64
+ end
65
+
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
+ end
77
+ end
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,3 @@
1
+ module DSLCompanion
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe DSLCompanion::Interpreter do
4
+
5
+ subject{DSLCompanion::Interpreter}
6
+
7
+ it 'should be used with a block of commands' do
8
+ the_class=nil
9
+ subject.run do
10
+ the_class = self.class
11
+ end
12
+ expect(the_class == DSLCompanion::Interpreter).to be_truthy
13
+ end
14
+
15
+ it 'should allow to add extra features through modules' do
16
+
17
+ module ExtraModule
18
+ def extra_dsl_command
19
+
20
+ end
21
+ end
22
+
23
+ interpreter = subject.new :strict
24
+ interpreter.add_feature ExtraModule
25
+
26
+ expect(interpreter.respond_to? :extra_dsl_command).to be_truthy
27
+ expect {interpreter.run {extra_dsl_command} }.not_to raise_error
28
+ expect {interpreter.run {unknown_command} }.to raise_error
29
+
30
+ end
31
+
32
+ context 'when in lazy mode' do
33
+
34
+ it 'should only report through logging errors in the DSL' do
35
+ interpreter = subject.new
36
+ expect {interpreter.run {unknown_command} }.not_to raise_error
37
+ end
38
+
39
+ end
40
+
41
+ context 'when in strict mode' do
42
+
43
+ it 'should raise exceptions if errors in the DSL'do
44
+ interpreter = subject.new :strict
45
+ expect {interpreter.run {unknown_command} }.to raise_error
46
+
47
+ end
48
+
49
+ end
50
+
51
+
52
+ end
@@ -0,0 +1,31 @@
1
+ #-------------------------------------------------------------------------------
2
+ #
3
+ #
4
+ # Copyright (c) 2014 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ #-------------------------------------------------------------------------------
7
+
8
+ require 'dsl_companion'
9
+
10
+ # Requires supporting ruby files with custom matchers and macros, etc,
11
+ # in spec/support/ and its subdirectories.
12
+ Dir[File.expand_path("../../spec/support/**/*.rb", __FILE__)].each { |f| require f }
13
+
14
+ RSpec.configure do |config|
15
+ # ## Mock Framework
16
+ #
17
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
18
+ #
19
+ # config.mock_with :mocha
20
+ # config.mock_with :flexmock
21
+ # config.mock_with :rr
22
+ config.mock_with :rspec
23
+
24
+ # Run specs in random order to surface order dependencies. If you find an
25
+ # order dependency and want to debug it, you can fix the order by providing
26
+ # the seed, which is printed after each run.
27
+ # --seed 1234
28
+ config.order = "random"
29
+
30
+ config.color = true
31
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dsl_companion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Laurent B.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: The goal of this gem is to provide a versatile DSL interpreter.
56
+ email:
57
+ - lbnetid+gh@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - dsl_companion.gemspec
69
+ - lib/dsl_companion.rb
70
+ - lib/dsl_companion/helpers/active_record.rb
71
+ - lib/dsl_companion/helpers/context_helper.rb
72
+ - lib/dsl_companion/injection.rb
73
+ - lib/dsl_companion/interpreter.rb
74
+ - lib/dsl_companion/meta_programming.rb
75
+ - lib/dsl_companion/version.rb
76
+ - spec/interpreter_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.0
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Provides a customizable interpreter to run your own internal DSLs.
102
+ test_files:
103
+ - spec/interpreter_spec.rb
104
+ - spec/spec_helper.rb