easydsl 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6f2bb1a8dbc0ece711617165a48837362dbb071
4
+ data.tar.gz: d30bc490e726fc53603234e473806df65d0d3c73
5
+ SHA512:
6
+ metadata.gz: e99e1c93f4a53df6d15574757f72d769e1e44f0398326ec03de6c6818579d4fdc23ab0dffd375625b136b08f651207377674cb78edff21c5975a144ee5d6f61b
7
+ data.tar.gz: 923bfc0dc4af71e2d6dd61b6f8e8af75dbd6dd04896c9579671037377f0fd6964ae7c605fe26a08e095411e311f0e7e754674e356c6e6ed5c6b38813e58057f5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ Exclude:
4
+ - 'db/migrate/*'
5
+ - 'db/schema.rb'
6
+ - 'config/**/*'
7
+ - 'bin/*'
8
+ # Other common or autogenerated ignores
9
+ - 'lib/tasks/cucumber.rake'
10
+ # Project specific ignores goes here
11
+
12
+ Style/BarePercentLiterals:
13
+ EnforcedStyle: percent_q
14
+
15
+ Style/EmptyLineBetweenDefs:
16
+ AllowAdjacentOneLineDefs: true
17
+
18
+ Style/MultilineOperationIndentation:
19
+ EnforcedStyle: indented
20
+
21
+ Metrics/LineLength:
22
+ Max: 100
23
+
24
+ Lint/EndAlignment:
25
+ AlignWith: variable
26
+
27
+ Style/TrailingCommaInLiteral:
28
+ EnforcedStyleForMultiline: comma
29
+
30
+ Lint/AssignmentInCondition:
31
+ Enabled: false
32
+
33
+ Style/DoubleNegation:
34
+ Enabled: false
35
+
36
+ Style/SingleLineBlockParams:
37
+ Enabled: false
38
+
39
+ Documentation:
40
+ Enabled: false
41
+
42
+ Rails:
43
+ Enabled: true
44
+
45
+ Metrics/ClassLength:
46
+ Max: 150
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3.0
8
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in opendsl.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mirko Mignini
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ [![Build Status](https://travis-ci.org/MirkoMignini/easydsl.svg)](https://travis-ci.org/MirkoMignini/easydsl)
2
+ [![Coverage Status](https://coveralls.io/repos/github/MirkoMignini/easydsl/badge.svg?branch=master)](https://coveralls.io/github/MirkoMignini/easydsl?branch=master)
3
+ [![Code Climate](https://codeclimate.com/github/MirkoMignini/easydsl/badges/gpa.svg)](https://codeclimate.com/github/MirkoMignini/easydsl)
4
+ [![Gem Version](https://badge.fury.io/rb/easydsl.svg)](https://badge.fury.io/rb/easydsl)
5
+ [![security](https://hakiri.io/github/MirkoMignini/easydsl/master.svg)](https://hakiri.io/github/MirkoMignini/easydsl/master)
6
+
7
+ # EasyDSL
8
+
9
+ EasyDSL allows you to create your own ruby [dsl](https://en.wikipedia.org/wiki/Domain-specific_language) without writing a single line of code.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'easydsl'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install easydsl
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MirkoMignini/easydsl. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'easydsl'
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/easydsl.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easydsl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'easydsl'
8
+ spec.version = Easydsl::VERSION
9
+ spec.authors = ['Mirko Mignini']
10
+ spec.email = ['mirko.mignini@gmail.com']
11
+
12
+ spec.summary = 'EasyDSL allows you to create your own ruby dsl'\
13
+ ' without writing a single line of code.'
14
+ spec.homepage = 'https://github.com/MirkoMignini/easydsl'
15
+ spec.license = 'MIT'
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'coveralls'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'rubocop-rspec'
28
+
29
+ spec.add_dependency 'activesupport', '~> 4.2.6'
30
+ end
data/lib/easydsl.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'easydsl/version'
2
+ require 'easydsl/node_builder'
3
+ require 'easydsl/node'
4
+
5
+ module Easydsl
6
+ def self.define(*_args, &block)
7
+ raise(ArgumentError, 'A block is mandatory.') unless block_given?
8
+ tree = NodeBuilder.new('root')
9
+ tree.instance_exec(&block)
10
+ Node.new(tree.name, tree.args, 0, nil, tree.nodes)
11
+ end
12
+ end
@@ -0,0 +1,136 @@
1
+ require 'active_support/inflector'
2
+ require 'easydsl/node_array'
3
+
4
+ module Easydsl
5
+ class Node
6
+ attr_reader :name, :index, :parent, :singleton
7
+ attr_accessor :args
8
+
9
+ def initialize(name, args, index, parent, node_builders = [])
10
+ @name = name.to_s.chomp('!').to_sym
11
+ @singleton = name[-1] == '!'
12
+ @args = args
13
+ @index = index
14
+ @parent = parent
15
+ node_builders.each_with_index do |item, i|
16
+ add_child(item.name, item.args, i, self, item.nodes)
17
+ end
18
+ end
19
+
20
+ def nodes
21
+ @nodes ||= Hash.new { |h, k| h[k] = NodeArray.new }
22
+ end
23
+
24
+ def all_nodes
25
+ all = []
26
+ nodes.each { |k, _v| all += nodes[k] }
27
+ all.sort! { |a, b| a.index <=> b.index }
28
+ end
29
+
30
+ def max_index
31
+ all_nodes.map(&:index).max || 0
32
+ end
33
+
34
+ def define(&block)
35
+ add_block(&block)
36
+ end
37
+
38
+ protected
39
+
40
+ def add_hierarchy(to, tree, base_index)
41
+ tree.each_with_index do |item, index|
42
+ to.add_child(item.name, item.args, base_index + index, to, item.nodes)
43
+ end
44
+ end
45
+
46
+ def add_block(&block)
47
+ tree = NodeBuilder.new('tree')
48
+ tree.instance_exec(&block)
49
+ add_hierarchy(self, tree.nodes, max_index)
50
+ end
51
+
52
+ def add_child(name, args, index, parent, node_builders = [])
53
+ node = handle_singleton(name, args, node_builders)
54
+ return node unless node.nil?
55
+ node = Node.new(name, args, index, parent, node_builders)
56
+ nodes[node.name] << node
57
+ node
58
+ end
59
+
60
+ def handle_singleton(name, args, node_builders = [])
61
+ return nil unless nodes.key?(name)
62
+ node = nodes[name].first
63
+ return nil if node.nil? || node.singleton == false
64
+ node.args = args
65
+ add_hierarchy(node, node_builders, node.max_index)
66
+ nodes[name].first
67
+ end
68
+
69
+ def clean_method_symbol(method_symbol)
70
+ method_symbol.to_s.gsub(/[=?]/, '').to_sym
71
+ end
72
+
73
+ def singular_method_symbol(method_symbol)
74
+ method_symbol.to_s.singularize.to_sym
75
+ end
76
+
77
+ def handle_block(method_symbol, *args, &block)
78
+ collection = nodes[method_symbol]
79
+ child = if collection.count > 0
80
+ collection.first
81
+ else
82
+ add_child(method_symbol, args, max_index, self)
83
+ end
84
+ child.add_block(&block)
85
+ end
86
+
87
+ def handle_brackets(_method_symbol, *args)
88
+ @args.first[args.first]
89
+ end
90
+
91
+ def handle_operators(method_symbol, *args)
92
+ method_symbol_wo = clean_method_symbol(method_symbol)
93
+ case method_symbol[-1]
94
+ when ']' then handle_brackets(method_symbol_wo, *args)
95
+ when '=' then handle_assignment(method_symbol_wo, *args)
96
+ when '?' then handle_question(method_symbol_wo, *args)
97
+ end
98
+ end
99
+
100
+ def handle_assignment(method_symbol, *args)
101
+ collection = nodes[method_symbol]
102
+ if collection.count > 0
103
+ collection.first.args = args
104
+ else
105
+ add_child(method_symbol, args, max_index, self)
106
+ end
107
+ end
108
+
109
+ def handle_question(method_symbol, *_args)
110
+ if nodes[method_symbol].count > 0
111
+ true
112
+ else
113
+ singular = singular_method_symbol(method_symbol)
114
+ method_symbol != singular ? nodes[singular].count > 0 : false
115
+ end
116
+ end
117
+
118
+ def handle_node(method_symbol, *_args)
119
+ return nodes[method_symbol].first.value_or_self if nodes[method_symbol].count > 0
120
+ singular = singular_method_symbol(method_symbol)
121
+ return nodes[singular] if method_symbol != singular
122
+ nil
123
+ end
124
+
125
+ def value_or_self
126
+ return @args.first if nodes.count == 0 && @args.count == 1 && @args.first.class != Node
127
+ self
128
+ end
129
+
130
+ def method_missing(method_symbol, *args, &block)
131
+ return handle_block(method_symbol, *args, &block) if block_given?
132
+ return handle_operators(method_symbol, *args) if method_symbol.to_s.end_with?('=', '?', '[]')
133
+ handle_node(method_symbol, *args, &block)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,4 @@
1
+ module Easydsl
2
+ class NodeArray < Array
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module Easydsl
2
+ class NodeBuilder
3
+ attr_reader :name, :args
4
+
5
+ def nodes
6
+ @nodes ||= []
7
+ end
8
+
9
+ def initialize(name, *args)
10
+ @name = name
11
+ @args = args
12
+ end
13
+
14
+ def method_missing(method_symbol, *args, &block)
15
+ child = NodeBuilder.new(method_symbol, *args)
16
+ nodes.push(child)
17
+ child.instance_exec(&block) if block_given?
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Easydsl
2
+ VERSION = '0.1.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easydsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mirko Mignini
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-31 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 4.2.6
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 4.2.6
111
+ description:
112
+ email:
113
+ - mirko.mignini@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - easydsl.gemspec
130
+ - lib/easydsl.rb
131
+ - lib/easydsl/node.rb
132
+ - lib/easydsl/node_array.rb
133
+ - lib/easydsl/node_builder.rb
134
+ - lib/easydsl/version.rb
135
+ homepage: https://github.com/MirkoMignini/easydsl
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.4.5.1
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: EasyDSL allows you to create your own ruby dsl without writing a single line
159
+ of code.
160
+ test_files: []