groovy_oneliner 1.0.0

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: b8dc13200f769f7b66bd2b61badef306f15d2a16
4
+ data.tar.gz: 5a80d432721ab5a87f406da1312c42c1765c7c4e
5
+ SHA512:
6
+ metadata.gz: b5317e989aa0498890c3d93d59b8e11503874c5d48a0594ff348d49be60802153e47bf93685306ca2527159ac01260a534e25ca50c6d73a105af91f94dccdf62
7
+ data.tar.gz: 2ba1bb414ae2d252e65c167286b918b8932203f0d687484e0b6cd973a3fd12d3c8ef58dbd14f4728965218928731c96deeabf9076cc7e4d95bd92a73c537ed6e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in groovy_oneliner.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
6
+ end
7
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pedro Cunha
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,75 @@
1
+ # GroovyOneliner
2
+
3
+ Convert any groovy script file to a single line script. It is recommended (and also in order for this gem to work) that all lines must be terminated with `;`.
4
+
5
+ ### What would this help with ?
6
+ In order to do scripting in [ElasticSearch](https://github.com/elasticsearch/elasticsearch) you can pick an handful of languages to inject code in your queries however ElasticSearch requires you that any script is written as a single line of code (not to mention the obvious properly formatted with `;`) at query time.
7
+
8
+ One of the languages supported is Groovy. Groovy is very similar to Java.
9
+
10
+ `GroovyOneliner` was extracted from one of the production apps at [HouseTrip](http://housetrip.com), since it allows to have a separate file for the Groovy script, which benefits: syntax highlighting, multilines, comments etc... and convert that script into one liner that you can use for queries. Nevertheless is important to say this is being used in production.
11
+
12
+ It is simple as it is, i.e, there are no external dependencies and while it's a very small task, it should allow to avoid repeating the same parsing code over and over.
13
+
14
+ This gem won't do for you:
15
+ - Check if there is any syntax errors
16
+ - Check if you hate semi-colons
17
+
18
+ What does it actually do:
19
+ - Removes all `//` & `\* ... */` comments
20
+ - Attempts to remove as much white space as possible such as empty lines, between `;`, etc..
21
+
22
+ Some file examples and the corresponding output when using this gem:
23
+
24
+ ### Example
25
+
26
+ ```groovy
27
+ // This line is amazing
28
+ a = 1;
29
+
30
+ /*
31
+ * Foo
32
+ */
33
+ b = 2;
34
+ ```
35
+
36
+ Converts to
37
+ ```groovy
38
+ a = 1; b = 2;
39
+ ```
40
+
41
+ ## Installation
42
+
43
+ Add this line to your application's Gemfile:
44
+
45
+ ```ruby
46
+ gem 'groovy_oneliner'
47
+ ```
48
+
49
+ And then execute:
50
+
51
+ $ bundle
52
+
53
+ Or install it yourself as:
54
+
55
+ $ gem install groovy_oneliner
56
+
57
+ ## Usage
58
+
59
+ ```
60
+ GroovyOneliner.compute(file: 'path/to/file')
61
+ ```
62
+
63
+ If you want to cache reading the file (recommended for production & staging environments)
64
+
65
+ ```
66
+ GroovyOneliner.compute(file: 'path/to/file', cache: true)
67
+ ```
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it ( https://github.com/pedrocunha/groovy_oneliner/fork )
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'groovy_oneliner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "groovy_oneliner"
8
+ spec.version = GroovyOneliner::VERSION
9
+ spec.authors = ["Pedro Cunha"]
10
+ spec.email = ["pkunha@gmail.com"]
11
+ spec.summary = %q{Convert groovy scripts into one line script}
12
+ spec.homepage = 'http://github.com/pedrocunha/groovy_oneliner'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency 'bundler', '~> 1.7'
19
+ spec.add_development_dependency 'rake', '~> 10.0'
20
+ spec.add_development_dependency 'rspec', '~> 3'
21
+ spec.add_development_dependency 'pry', '~> 0'
22
+ spec.add_development_dependency 'pry-nav', '~> 0'
23
+ spec.add_development_dependency 'guard', '~> 2.9'
24
+ spec.add_development_dependency 'guard-rspec', '~> 4'
25
+ end
@@ -0,0 +1,23 @@
1
+ require 'groovy_oneliner'
2
+
3
+ class GroovyOneliner
4
+ class Cache
5
+
6
+ def initialize
7
+ @cache = {}
8
+ end
9
+
10
+ def [](key)
11
+ @cache[key]
12
+ end
13
+
14
+ def []=(key, value)
15
+ @cache[key] = value
16
+ end
17
+
18
+ def reset!
19
+ @cache = {}
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ require 'groovy_oneliner'
2
+
3
+ class GroovyOneliner
4
+ class Converter
5
+
6
+ def initialize(content)
7
+ @content = content
8
+ end
9
+
10
+ def compute
11
+ @content
12
+ .gsub(/\/\/.*$/, '') # remove all comments //
13
+ .gsub("\n", '') # remove all line-breaks
14
+ .gsub("\"", "'") # substitute all double-quote to single-quote
15
+ .gsub(/^$/, '') # remove all empty lines
16
+ .gsub(/;\s*/, ';') # remove all whitespace after ;
17
+ .gsub(%r{/\*.*\*\/}, '') # remove all comments /* ... */
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ class GroovyOneliner
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ require 'groovy_oneliner/version'
2
+ require 'groovy_oneliner/cache'
3
+ require 'groovy_oneliner/converter'
4
+ require 'singleton'
5
+
6
+ class GroovyOneliner
7
+ include Singleton
8
+
9
+ def initialize
10
+ @cache = GroovyOneliner::Cache.new
11
+ end
12
+
13
+ def compute(options = {})
14
+ path = options.fetch(:path)
15
+ should_cache = options.fetch(:cache, false)
16
+
17
+ if should_cache
18
+ cached_content = @cache[path]
19
+ return cached_content if cached_content
20
+ end
21
+
22
+ content = File.read(path)
23
+ output = GroovyOneliner::Converter.new(content).compute
24
+
25
+ should_cache ? @cache[path] = output : output
26
+ end
27
+
28
+ def self.compute(options = {})
29
+ self.instance.compute(options)
30
+ end
31
+
32
+ end
33
+
@@ -0,0 +1,26 @@
1
+ require 'groovy_oneliner/cache'
2
+
3
+ RSpec.describe GroovyOneliner::Cache do
4
+ subject { described_class.new }
5
+
6
+ describe '#[]' do
7
+ it 'returns empty if nothing on cache' do
8
+ expect(subject['foo']).to be_nil
9
+ end
10
+ end
11
+
12
+ describe '#[]=' do
13
+ it 'stores the value in the key' do
14
+ expect(subject['foo'] = 'yadada').to eql('yadada')
15
+ end
16
+ end
17
+
18
+ describe '#reset' do
19
+ it 'resets the cache' do
20
+ subject['foo'] = 'bar'
21
+ subject.reset!
22
+
23
+ expect(subject['foo']).to be_nil
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,65 @@
1
+ require 'groovy_oneliner/converter'
2
+
3
+ RSpec.describe GroovyOneliner::Converter do
4
+
5
+ describe '#initialization' do
6
+ it 'raises an error on missing argument' do
7
+ expect { described_class.new }.to raise_error
8
+ end
9
+
10
+ it 'returns an instance when good argument' do
11
+ expect { described_class.new("foo") }.to_not raise_error
12
+ end
13
+ end
14
+
15
+ describe '#compute' do
16
+
17
+ it 'removes all // comments' do
18
+ subject = described_class.new <<-TEXT
19
+ // This is a comment and will be ignored
20
+ return 1;
21
+ TEXT
22
+
23
+ expect(subject.compute).to eql("return 1;")
24
+ end
25
+
26
+ it 'removes all line breaks' do
27
+ subject = described_class.new <<-TEXT
28
+ a = 3;
29
+ return 1;
30
+ TEXT
31
+
32
+ expect(subject.compute).to eql("a = 3;return 1;")
33
+ end
34
+
35
+ it 'removes all empty lines' do
36
+ subject = described_class.new <<-TEXT
37
+ a = 4;
38
+
39
+ b = 5;
40
+ TEXT
41
+
42
+ expect(subject.compute).to eql("a = 4;b = 5;")
43
+ end
44
+
45
+ it 'removes all whitespace after ;' do
46
+ subject = described_class.new <<-TEXT
47
+ a = 4; b = 5;
48
+ TEXT
49
+
50
+ expect(subject.compute).to eql("a = 4;b = 5;")
51
+ end
52
+
53
+ it 'removes all /* comment */ blocks' do
54
+ subject = described_class.new <<-TEXT
55
+ /*
56
+ * This is a comment and will be ignored
57
+ */
58
+ return 1;
59
+ TEXT
60
+
61
+ expect(subject.compute).to eql("return 1;")
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,42 @@
1
+ require 'groovy_oneliner'
2
+
3
+ RSpec.describe GroovyOneliner do
4
+
5
+ it 'does not allow to initialize an instance of this class' do
6
+ expect { described_class.new }.to raise_error
7
+ end
8
+
9
+ describe '.compute' do
10
+
11
+ let(:path) { 'path/to/file' }
12
+ let(:options) { { path: path } }
13
+
14
+ subject { described_class }
15
+
16
+ it 'allows to compute a file' do
17
+ expect(File).to receive(:read).once.with(path).and_return("foo = 1;")
18
+ subject.compute(options)
19
+ end
20
+
21
+ context 'when caching is off' do
22
+ it 'reads the file as many times as calls to compute' do
23
+ expect(File).to receive(:read).twice.with(path).and_return("foo = 1;")
24
+ subject.compute(options)
25
+ subject.compute(options)
26
+ end
27
+ end
28
+
29
+ context 'when caching is on' do
30
+ let(:options) { { path: path, cache: true } }
31
+
32
+ it 'reads the file only once' do
33
+ expect(File).to receive(:read).once.with(path).and_return("foo = 1;")
34
+ subject.compute(options)
35
+ subject.compute(options)
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+
42
+ end
@@ -0,0 +1,65 @@
1
+ RSpec.configure do |config|
2
+ # rspec-expectations config goes here. You can use an alternate
3
+ # assertion/expectation library such as wrong or the stdlib/minitest
4
+ # assertions if you prefer.
5
+ config.expect_with :rspec do |expectations|
6
+ # This option will default to `true` in RSpec 4. It makes the `description`
7
+ # and `failure_message` of custom matchers include text for helper methods
8
+ # defined using `chain`, e.g.:
9
+ # be_bigger_than(2).and_smaller_than(4).description
10
+ # # => "be bigger than 2 and smaller than 4"
11
+ # ...rather than:
12
+ # # => "be bigger than 2"
13
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
14
+ end
15
+
16
+ # rspec-mocks config goes here. You can use an alternate test double
17
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
18
+ config.mock_with :rspec do |mocks|
19
+ # Prevents you from mocking or stubbing a method that does not exist on
20
+ # a real object. This is generally recommended, and will default to
21
+ # `true` in RSpec 4.
22
+ mocks.verify_partial_doubles = true
23
+ end
24
+
25
+ # These two settings work together to allow you to limit a spec run
26
+ # to individual examples or groups you care about by tagging them with
27
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
28
+ # get run.
29
+ config.filter_run :focus
30
+ config.run_all_when_everything_filtered = true
31
+
32
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
33
+ # For more details, see:
34
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
35
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
36
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
37
+ config.disable_monkey_patching!
38
+
39
+ # Many RSpec users commonly either run the entire suite or an individual
40
+ # file, and it's useful to allow more verbose output when running an
41
+ # individual spec file.
42
+ if config.files_to_run.one?
43
+ # Use the documentation formatter for detailed output,
44
+ # unless a formatter has already been configured
45
+ # (e.g. via a command-line flag).
46
+ config.default_formatter = 'doc'
47
+ end
48
+
49
+ # Print the 10 slowest examples and example groups at the
50
+ # end of the spec run, to help surface which specs are running
51
+ # particularly slow.
52
+ config.profile_examples = 10
53
+
54
+ # Run specs in random order to surface order dependencies. If you find an
55
+ # order dependency and want to debug it, you can fix the order by providing
56
+ # the seed, which is printed after each run.
57
+ # --seed 1234
58
+ config.order = :random
59
+
60
+ # Seed global randomization in this process using the `--seed` CLI option.
61
+ # Setting this allows you to use `--seed` to deterministically reproduce
62
+ # test failures related to randomization by passing the same `--seed` value
63
+ # as the one that triggered the failure.
64
+ Kernel.srand config.seed
65
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: groovy_oneliner
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Cunha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-01 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: pry-nav
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: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4'
111
+ description:
112
+ email:
113
+ - pkunha@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - Gemfile
121
+ - Guardfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - groovy_oneliner.gemspec
126
+ - lib/groovy_oneliner.rb
127
+ - lib/groovy_oneliner/cache.rb
128
+ - lib/groovy_oneliner/converter.rb
129
+ - lib/groovy_oneliner/version.rb
130
+ - spec/groovy_oneliner/cache_spec.rb
131
+ - spec/groovy_oneliner/converter_spec.rb
132
+ - spec/groovy_oneliner_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: http://github.com/pedrocunha/groovy_oneliner
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.4.2
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Convert groovy scripts into one line script
158
+ test_files: []