arel-trigonometry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_script:
5
+ - mysql -e 'create database arel_trigonometry_test;'
6
+ - psql -c 'create database arel_trigonometry_test;' -U postgres
7
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jacob Swanner
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.
@@ -0,0 +1,37 @@
1
+ # Arel::Trigonometry [![Build Status](https://secure.travis-ci.org/jswanner/arel-trigonometry.png?branch=master)](https://travis-ci.org/jswanner/arel-trigonometry)
2
+
3
+ Provides trigonometry functions, implemented with Arel.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'arel-trigonometry'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install arel-trigonometry
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ # require 'arel/trigonometry/mysql' # for only MySQL support
23
+ # require 'arel/trigonometry/postgresql' # for only PostgreSQL support
24
+ require 'arel/trigonometry'
25
+
26
+ angles = Arel::Table.new(:angles)
27
+ angles.project(Arel::Nodes::Radians.new(angles[:a]))
28
+ # => SELECT RADIANS("angles"."a") FROM "angles";
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -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
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arel/trigonometry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'arel-trigonometry'
8
+ spec.version = Arel::Trigonometry::VERSION
9
+ spec.authors = ['Jacob Swanner']
10
+ spec.email = ['jacob@jacobswanner.com']
11
+ spec.description = %q{Provides trigonometry functions, implemented with Arel; which could be added to ActiveRecord scope, for instance.}
12
+ spec.summary = %q{Provides trigonometry functions, implemented with Arel.}
13
+ spec.homepage = 'https://github.com/jswanner/arel-trigonometry'
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 'arel'
22
+
23
+ spec.add_development_dependency 'activerecord', '>= 3'
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'mysql2'
26
+ spec.add_development_dependency 'pg'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec', '~> 2.11'
29
+ end
@@ -0,0 +1,2 @@
1
+ require 'arel/trigonometry/mysql'
2
+ require 'arel/trigonometry/postgresql'
@@ -0,0 +1,3 @@
1
+ require 'arel/trigonometry/version'
2
+ require 'arel/trigonometry/nodes/trigonometry_functions'
3
+ require 'arel/trigonometry/visitors/mysql'
@@ -0,0 +1,20 @@
1
+ require 'arel'
2
+
3
+ module Arel
4
+ module Nodes
5
+ %w[
6
+ Arcsine
7
+ Arctangent
8
+ Cosine
9
+ CubeRoot
10
+ Radians
11
+ Sine
12
+ SquareRoot
13
+ Tangent
14
+ ].each do |name|
15
+ const_set name, Class.new(Unary)
16
+ end
17
+ Arctangent2 = Class.new(Binary)
18
+ Pow = Class.new(Binary)
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ require 'arel/trigonometry/version'
2
+ require 'arel/trigonometry/nodes/trigonometry_functions'
3
+ require 'arel/trigonometry/visitors/postgresql'
@@ -0,0 +1,5 @@
1
+ module Arel
2
+ module Trigonometry
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ module Arel
2
+ module Visitors
3
+ module CommonTrigonometryVisitors
4
+ private
5
+
6
+ def visit_Arel_Nodes_Arcsine o
7
+ "ASIN(#{visit o.expr})"
8
+ end
9
+
10
+ def visit_Arel_Nodes_Arctangent o
11
+ "ATAN(#{visit o.expr})"
12
+ end
13
+
14
+ def visit_Arel_Nodes_Arctangent2 o
15
+ "ATAN2(#{visit o.left}, #{visit o.right})"
16
+ end
17
+
18
+ def visit_Arel_Nodes_Cosine o
19
+ "COS(#{visit o.expr})"
20
+ end
21
+
22
+ def visit_Arel_Nodes_CubeRoot o
23
+ "CBRT(#{visit o.expr})"
24
+ end
25
+
26
+ def visit_Arel_Nodes_Pow o
27
+ "POW(#{visit o.left}, #{visit o.right})"
28
+ end
29
+
30
+ def visit_Arel_Nodes_Radians o
31
+ "RADIANS(#{visit o.expr})"
32
+ end
33
+
34
+ def visit_Arel_Nodes_Sine o
35
+ "SIN(#{visit o.expr})"
36
+ end
37
+
38
+ def visit_Arel_Nodes_SquareRoot o
39
+ "SQRT(#{visit o.expr})"
40
+ end
41
+
42
+ def visit_Arel_Nodes_Tangent o
43
+ "TAN(#{visit o.expr})"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ require 'arel/trigonometry/visitors/common_trigonometry_visitors'
2
+
3
+ module Arel
4
+ module Visitors
5
+ class MySQL
6
+ include CommonTrigonometryVisitors
7
+
8
+ def visit_Arel_Nodes_CubeRoot o
9
+ "POW(#{visit o.expr}, 1/3)"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ require 'arel/trigonometry/visitors/common_trigonometry_visitors'
2
+
3
+ module Arel
4
+ module Visitors
5
+ class PostgreSQL
6
+ include CommonTrigonometryVisitors
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'mysql2'
2
+ require 'spec_helper'
3
+ require 'arel/trigonometry/mysql'
4
+
5
+ describe 'Trigonometry functions in MySQL', :mysql do
6
+ def parse_results results
7
+ results.first.first
8
+ end
9
+
10
+ include_examples 'trigonometry examples'
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'pg'
2
+ require 'spec_helper'
3
+ require 'arel/trigonometry/postgresql'
4
+
5
+ describe 'Trigonometry functions in PostgreSQL', :postgres do
6
+ def parse_results results
7
+ results.first.values.first
8
+ end
9
+
10
+ include_examples 'trigonometry examples'
11
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'bundler/setup'
3
+ require 'active_record'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.order = 'random'
8
+ end
9
+
10
+ Dir[File.expand_path('../support/*.rb', __FILE__)].each { |file| require file }
@@ -0,0 +1,17 @@
1
+ shared_context 'MySQL connection', :mysql do
2
+ before :all do
3
+ ActiveRecord::Base.establish_connection(
4
+ adapter: 'mysql2',
5
+ database: 'arel_trigonometry_test'
6
+ )
7
+ end
8
+ end
9
+
10
+ shared_context 'Postgres connection', :postgres do
11
+ before :all do
12
+ ActiveRecord::Base.establish_connection(
13
+ adapter: 'postgresql',
14
+ database: 'arel_trigonometry_test'
15
+ )
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ shared_examples 'trigonometry examples' do
2
+ let(:query) { project(function).to_sql }
3
+ let(:result) { parse_results(execute(query)) }
4
+
5
+ context 'arcsine' do
6
+ let(:function) { Arel::Nodes::Arcsine.new(1) }
7
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(1.570_796_326) }
8
+ end
9
+
10
+ context 'arctangent' do
11
+ let(:function) { Arel::Nodes::Arctangent.new(1) }
12
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(0.785_398_163) }
13
+ end
14
+
15
+ context 'arctangent2' do
16
+ let(:function) { Arel::Nodes::Arctangent2.new(1, 2) }
17
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(0.463_647_609) }
18
+ end
19
+
20
+ context 'cosine' do
21
+ let(:function) { Arel::Nodes::Cosine.new(1) }
22
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(0.540_302_305) }
23
+ end
24
+
25
+ context 'cube root' do
26
+ let(:function) { Arel::Nodes::CubeRoot.new(16) }
27
+ specify { expect(result.to_i).to eq 2 }
28
+ end
29
+
30
+ context 'pow' do
31
+ let(:function) { Arel::Nodes::Pow.new(4, 2) }
32
+ specify { expect(result.to_i).to eq 16 }
33
+ end
34
+
35
+ context 'radians' do
36
+ let(:function) { Arel::Nodes::Radians.new(180) }
37
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(3.141_592_653) }
38
+ end
39
+
40
+ context 'sine' do
41
+ let(:function) { Arel::Nodes::Sine.new(360) }
42
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(0.958_915_723) }
43
+ end
44
+
45
+ context 'square root' do
46
+ let(:function) { Arel::Nodes::SquareRoot.new(16) }
47
+ specify { expect(result.to_i).to eq 4 }
48
+ end
49
+
50
+ context 'tangent' do
51
+ let(:function) { Arel::Nodes::Tangent.new(1) }
52
+ specify { expect(result.to_f).to be_within(0.000_000_001).of(1.557_407_724) }
53
+ end
54
+ end
@@ -0,0 +1,7 @@
1
+ def execute query
2
+ ActiveRecord::Base.connection.execute query
3
+ end
4
+
5
+ def project clause
6
+ Arel::SelectManager.new(ActiveRecord::Base).project clause
7
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arel-trigonometry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jacob Swanner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: arel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mysql2
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pg
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '2.11'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '2.11'
126
+ description: Provides trigonometry functions, implemented with Arel; which could be
127
+ added to ActiveRecord scope, for instance.
128
+ email:
129
+ - jacob@jacobswanner.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - .rspec
136
+ - .travis.yml
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - arel-trigonometry.gemspec
142
+ - lib/arel/trigonometry.rb
143
+ - lib/arel/trigonometry/mysql.rb
144
+ - lib/arel/trigonometry/nodes/trigonometry_functions.rb
145
+ - lib/arel/trigonometry/postgresql.rb
146
+ - lib/arel/trigonometry/version.rb
147
+ - lib/arel/trigonometry/visitors/common_trigonometry_visitors.rb
148
+ - lib/arel/trigonometry/visitors/mysql.rb
149
+ - lib/arel/trigonometry/visitors/postgresql.rb
150
+ - spec/mysql_spec.rb
151
+ - spec/postgres_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/support/shared_contexts.rb
154
+ - spec/support/shared_examples.rb
155
+ - spec/support/sql_helpers.rb
156
+ homepage: https://github.com/jswanner/arel-trigonometry
157
+ licenses:
158
+ - MIT
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ none: false
165
+ requirements:
166
+ - - ! '>='
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 1.8.24
178
+ signing_key:
179
+ specification_version: 3
180
+ summary: Provides trigonometry functions, implemented with Arel.
181
+ test_files:
182
+ - spec/mysql_spec.rb
183
+ - spec/postgres_spec.rb
184
+ - spec/spec_helper.rb
185
+ - spec/support/shared_contexts.rb
186
+ - spec/support/shared_examples.rb
187
+ - spec/support/sql_helpers.rb