dredd 0.0.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: 1d570f3f29ad40acd6993c3c732be64c8dc1ccde
4
+ data.tar.gz: 437c818aeef70e7bd7388430c00e4b2285d962ae
5
+ SHA512:
6
+ metadata.gz: a77dd1859aa1111f34b29fe7f7be2491bee5f08d8f3fe6d9df3817696817c35e202efd878e8e4f5effcc9d595a05930ae034a8a649b4e4eccd647a547d237f0d
7
+ data.tar.gz: 8f4053a1ba11c29463c06ba1485101138c2f867686f188ad4565550e512d70d632daad9a88c2a07849bd38f0452c6291cd2758bb64a371fc4daff3708c7f5ace
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dredd.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,25 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+
5
+ guard :minitest do
6
+ # with Minitest::Spec
7
+ watch(%r{^spec/(.*)_spec\.rb})
8
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch(%r{^spec/spec_helper\.rb}) { 'spec' }
10
+
11
+ # Rails 4
12
+ # watch(%r{^app/(.+)\.rb}) { |m| "test/#{m[1]}_test.rb" }
13
+ # watch(%r{^app/controllers/application_controller\.rb}) { 'test/controllers' }
14
+ # watch(%r{^app/controllers/(.+)_controller\.rb}) { |m| "test/integration/#{m[1]}_test.rb" }
15
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
16
+ # watch(%r{^lib/(.+)\.rb}) { |m| "test/lib/#{m[1]}_test.rb" }
17
+ # watch(%r{^test/.+_test\.rb})
18
+ # watch(%r{^test/test_helper\.rb}) { 'test' }
19
+
20
+ # Rails < 4
21
+ # watch(%r{^app/controllers/(.*)\.rb}) { |m| "test/functional/#{m[1]}_test.rb" }
22
+ # watch(%r{^app/helpers/(.*)\.rb}) { |m| "test/helpers/#{m[1]}_test.rb" }
23
+ # watch(%r{^app/models/(.*)\.rb}) { |m| "test/unit/#{m[1]}_test.rb" }
24
+ end
25
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 jondot
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,29 @@
1
+ # Dredd
2
+
3
+ Rules over collections.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'dredd'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install dredd
19
+
20
+ ## Usage
21
+
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "spec"
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ end
8
+
data/dredd.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dredd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dredd"
8
+ spec.version = Dredd::VERSION
9
+ spec.authors = ["jondot"]
10
+ spec.email = ["jondotan@gmail.com"]
11
+ spec.description = %q{}
12
+ spec.summary = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency "hashie", "~> 2.0.4"
22
+ spec.add_dependency "statsample", "~> 1.3.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rr"
27
+ spec.add_development_dependency "guard-minitest"
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'hashie/mash'
2
+ require 'statsample'
3
+
4
+
5
+ module Dredd
6
+ module Dsl
7
+ class Aggregate
8
+ def execute(collection)
9
+ return collection unless @projection
10
+
11
+ arr = collection.map{|k| Hashie::Mash.new(k) }
12
+ .map{|k| k.instance_eval(&@projection) }
13
+
14
+ return arr unless @func
15
+
16
+ arr.to_scale.send(@func)
17
+ end
18
+
19
+
20
+ def method_missing(meth, *args, &block)
21
+ if [:sum, :mean, :min, :max, :size].include?(meth)
22
+ @func = meth
23
+ @projection = block
24
+ else
25
+ super
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,38 @@
1
+ require 'hashie'
2
+ require 'dredd/dsl/aggregate'
3
+
4
+ module Dredd
5
+ class Query
6
+ attr_reader :window
7
+
8
+ def initialize(text)
9
+ instance_eval(text)
10
+ end
11
+
12
+ def all(&block)
13
+ @window = [:to_a]
14
+ @filter = block
15
+ @aggregate = Dsl::Aggregate.new
16
+ end
17
+
18
+ def first(num, &block)
19
+ @window = [:first, num]
20
+ @filter = block
21
+ @aggregate = Dsl::Aggregate.new
22
+ end
23
+
24
+ def last(num, &block)
25
+ @window = [:last, num]
26
+ @filter = block
27
+ @aggregate = Dsl::Aggregate.new
28
+ end
29
+
30
+ def execute(collection)
31
+ slice = collection.send(*@window)
32
+ .map{|k| Hashie::Mash.new(k)}
33
+ .select{|k| @filter ? k.instance_eval(&@filter) : true }
34
+ .map{|k| k.to_hash }
35
+ @aggregate.execute(slice)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Dredd
2
+ VERSION = "0.0.1"
3
+ end
data/lib/dredd.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "dredd/version"
2
+ require 'dredd/query'
3
+
4
+ module Dredd
5
+ def self.detect(query, values)
6
+ Query.new(query).execute(values)
7
+ end
8
+ end
@@ -0,0 +1,49 @@
1
+ require 'dredd/query'
2
+
3
+ describe Dredd::Query do
4
+ it "should execute an all query" do
5
+ q = Dredd::Query.new("all{ foo > 50 }")
6
+ q.execute([{:foo => 0}, {:foo => 60}]).must_equal(["foo" => 60])
7
+ end
8
+
9
+ it "should execute a limiting query" do
10
+ q = Dredd::Query.new("first(2){ foo > 50 }")
11
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal([])
12
+
13
+ q = Dredd::Query.new("last(2){ foo > 50 }")
14
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal(["foo" => 60])
15
+ end
16
+
17
+ it "should run aggregations on all" do
18
+ q = Dredd::Query.new("all.sum{ foo }")
19
+ q.execute([{:foo => 1},{:foo => 2}, {:foo => 60}]).must_equal(63)
20
+
21
+ q = Dredd::Query.new("all.mean{ foo }")
22
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal(20)
23
+ end
24
+
25
+ it "should run aggregations on queries" do
26
+ q = Dredd::Query.new("all{ foo > 10 }.sum{ foo }")
27
+ q.execute([{:foo => 1},{:foo => 2}, {:foo => 60}]).must_equal(60)
28
+
29
+ q = Dredd::Query.new("all{ foo > 10 }.mean{ foo }")
30
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal(60)
31
+
32
+ q = Dredd::Query.new("all{ foo > 10 }.size{ foo }")
33
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal(1)
34
+
35
+ q = Dredd::Query.new("all{ foo > 10 }.min{ foo }")
36
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal(60)
37
+
38
+ q = Dredd::Query.new("all{ foo > 10 }.max{ foo }")
39
+ q.execute([{:foo => 0},{:foo => 0}, {:foo => 60}]).must_equal(60)
40
+ end
41
+
42
+ it "should run aggregations on empty queries" do
43
+ q = Dredd::Query.new("all{ foo > 100 }.sum{ foo }")
44
+ q.execute([{:foo => 1},{:foo => 2}, {:foo => 60}]).must_equal(0)
45
+
46
+ q = Dredd::Query.new("all{ foo > 100 }.mean{ foo }")
47
+ q.execute([{:foo => 1},{:foo => 2}, {:foo => 60}]).nan?.must_equal true
48
+ end
49
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'dredd'
3
+
4
+ BAD_VALS = [
5
+ { :rate => 0 },
6
+ { :rate => 0 },
7
+ { :rate => 0 },
8
+ { :rate => 40 },
9
+ { :rate => 0 }
10
+ ]
11
+
12
+ describe Dredd do
13
+ it "should detect bad values" do
14
+ query = "all{ rate > 0 }.max{ rate }"
15
+ result = Dredd.detect(query, BAD_VALS)
16
+ result.must_equal(40)
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'rr'
3
+
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dredd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jondot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashie
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: statsample
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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: rr
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-minitest
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
+ description: ''
98
+ email:
99
+ - jondotan@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - Guardfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - dredd.gemspec
111
+ - lib/dredd.rb
112
+ - lib/dredd/dsl/aggregate.rb
113
+ - lib/dredd/query.rb
114
+ - lib/dredd/version.rb
115
+ - spec/dredd/query_spec.rb
116
+ - spec/dredd_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: ''
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.2.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: ''
142
+ test_files:
143
+ - spec/dredd/query_spec.rb
144
+ - spec/dredd_spec.rb
145
+ - spec/spec_helper.rb