ruby-ext-js 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 CrowdFlower
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,65 @@
1
+ # ruby-ext-js
2
+
3
+ Provides ultra-basic classes for translating Ext.js GET params to Postgres (via Datamapper) and MongoDB (via Mongood) query opts with sensible default limits.
4
+
5
+ Examples:
6
+
7
+ ## Postgres
8
+
9
+ module ExtJs
10
+ class PullRequests < Postgres
11
+ # Converts Ext.js' wacky params structure into a Postgres db query opts
12
+ # hash. Supports filtering by PullRequests.state only
13
+ def self.db_opts(params, opts = {})
14
+ params[:dir] ||= "DESC" # Default to descending order
15
+
16
+ db_opts = self.pagination_opts( params, {
17
+ :max_offset => 5000, # Optional, prevent Postgres from shitting its pants
18
+ :max_limit => 100
19
+ }.merge( opts ))
20
+
21
+ # Filters (the Ext filters hash is not pretty)
22
+ filters = {}
23
+ if params[:filter] && params[:filter]["0"] && params[:filter]["0"][:data]
24
+ filters = case params[:filter]["0"][:data][:value]
25
+ when "Rejected": { :rejected => true }
26
+ when "Approved": { :rejected => false }
27
+ when "Unreviewed": { :reviewed => nil }
28
+ end
29
+ end
30
+
31
+ db_opts.merge filters
32
+ end
33
+ end
34
+ end
35
+
36
+ ## MongoDB
37
+
38
+ Mongo's a lot easier to work with:
39
+
40
+ module ExtJs
41
+ class PullRequests < Mongo
42
+ def self.allowed_filters
43
+ ["state"]
44
+ end
45
+ end
46
+ end
47
+
48
+ # Specs
49
+
50
+ `ExtJs` was extracted from private code. Existing specs rely on private models, so there are no specs here yet. Specs will require DataMapper.
51
+
52
+ # Contributing
53
+
54
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
55
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
56
+ * Fork the project
57
+ * Start a feature/bugfix branch
58
+ * Commit and push until you are happy with your contribution
59
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
60
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
61
+
62
+ # Copyright
63
+
64
+ Copyright (c) 2011 CrowdFlower. See LICENSE.txt for
65
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
7
+ gem.name = "ruby-ext-js"
8
+ gem.homepage = "http://github.com/help/ruby-ext-js"
9
+ gem.license = "MIT"
10
+ gem.summary = %Q{Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.}
11
+ gem.description = %Q{Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.}
12
+ gem.email = "tyson@doloreslabs.com"
13
+ gem.authors = ["Tyson Tate"]
14
+ gem.add_development_dependency "rspec", "~> 1.3.1"
15
+ gem.add_development_dependency "jeweler", "~> 1.5.2"
16
+ end
17
+ Jeweler::RubygemsDotOrgTasks.new
18
+
19
+ require 'spec'
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.pattern = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ task :default => :spec
26
+
27
+ require 'rake/rdoctask'
28
+ Rake::RDocTask.new do |rdoc|
29
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
30
+
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = "ruby-ext-js #{version}"
33
+ rdoc.rdoc_files.include('README*')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,132 @@
1
+ module ExtJs
2
+ class Postgres
3
+ def self.db_opts(params, opts = {}); raise NotImplementedError; end
4
+
5
+ protected
6
+
7
+ # Converts Ext.js' wacky params structure into a Postgres db query
8
+ # opts hash for pagination.
9
+ def self.pagination_opts(params, opts = {})
10
+ opts = {
11
+ :max_offset => 10000,
12
+ :max_limit => 100
13
+ }.merge opts
14
+
15
+ # Pagination
16
+ offset = offset_param( params, opts[:max_offset] )
17
+ limit = limit_param( params, opts[:max_limit] )
18
+
19
+ # Sort order
20
+ order = order_param( params )
21
+
22
+ {
23
+ :order => [order],
24
+ :offset => offset,
25
+ :limit => limit
26
+ }
27
+ end
28
+
29
+ # Given Ext.js params hash, returns a Datamapper sort param
30
+ def self.order_param( params )
31
+ sort = case params["sort"]
32
+ when "created_at"
33
+ :id
34
+ else
35
+ params["sort"] ? params["sort"].to_sym : :id
36
+ end
37
+ params["dir"] =~ /desc/i ? sort.desc : sort.asc
38
+ end
39
+
40
+ # Given Ext.js params hash, returns a Datamapper offset param
41
+ def self.offset_param( params, max_offset )
42
+ [( params["start"] && params["start"].to_i ) || 0, max_offset].min
43
+ end
44
+
45
+ # Given Ext.js params hash, returns a Datamapper limit param
46
+ def self.limit_param( params, max_limit )
47
+ [( params["limit"] && params["limit"].to_i ) || 25, max_limit].min
48
+ end
49
+
50
+ # Filtering
51
+
52
+ def self.allowed_filters
53
+ []
54
+ end
55
+
56
+ def self.get_filter_data( params )
57
+ return unless params["filter"] && params["filter"]["0"]
58
+ if allowed_filters.include?( params["filter"]["0"]["field"] )
59
+ {
60
+ :field => params["filter"]["0"]["field"],
61
+ :values => Array( params["filter"]["0"]["data"]["value"] )
62
+ }
63
+ end
64
+ end
65
+ end
66
+
67
+ class Mongo
68
+ MAX_PER_PAGE = 1000
69
+ DEFAULT_PAGE = 1
70
+ DEFAULT_PER_PAGE = 30
71
+
72
+ def self.db_opts( params )
73
+ opts = {}
74
+
75
+ opts.merge! page_param( params )
76
+ opts.merge! per_page_param( params )
77
+ opts.merge! sort_param( params )
78
+ opts.merge! search_param( params )
79
+
80
+ opts
81
+ end
82
+
83
+ protected
84
+
85
+ def self.allowed_filters
86
+ []
87
+ end
88
+
89
+ def self.page_param( params )
90
+ return { :page => DEFAULT_PAGE } unless params.key?( "start" ) && params.key?( "limit" )
91
+
92
+ start = params["start"].to_i
93
+ limit = per_page_param( params )[:per_page]
94
+
95
+ { :page => ( start / limit ) + 1 }
96
+ end
97
+
98
+ def self.per_page_param( params )
99
+ unless params.key?( "limit" ) && params["limit"].to_i > 0
100
+ return { :per_page => DEFAULT_PER_PAGE }
101
+ end
102
+ { :per_page => [params["limit"].to_i, MAX_PER_PAGE].min }
103
+ end
104
+
105
+ def self.sort_param( params )
106
+ return {} unless params.key?( "sort" )
107
+
108
+ sort = params["sort"] ? params["sort"].to_sym : :id
109
+ sort = params["dir"] =~ /desc/i ? sort.desc : sort.asc
110
+
111
+ { :sort => sort }
112
+ end
113
+
114
+ def self.search_param( params )
115
+ if params["filter"] && params["filter"]["0"]
116
+ field = params["filter"]["0"]["field"].gsub(/[^\.\w\d_-]/, "").strip
117
+ values = Array( params["filter"]["0"]["data"]["value"] )
118
+
119
+ if values.size == 1
120
+ values = values[0]
121
+ else
122
+ values = { "$in" => values }
123
+ end
124
+
125
+ unless field.blank? || !allowed_filters.include?( field ) || values.blank?
126
+ return { field => values }
127
+ end
128
+ end
129
+ {}
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-ext-js}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tyson Tate"]
12
+ s.date = %q{2011-01-20}
13
+ s.description = %q{Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.}
14
+ s.email = %q{tyson@doloreslabs.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "LICENSE.txt",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/ruby-ext-js.rb",
27
+ "ruby-ext-js.gemspec",
28
+ "spec/ruby-ext-js_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/help/ruby-ext-js}
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.}
36
+ s.test_files = [
37
+ "spec/ruby-ext-js_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<rspec>, ["~> 1.3.1"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 1.3.1"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<rspec>, ["~> 1.3.1"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ExtJs" do
4
+ describe "Postgres" do
5
+ it "sorts on id when you ask it to sort on created_at" do
6
+ ExtJs::Postgres.pagination_opts({
7
+ :sort => "created_at",
8
+ :order => "desc"
9
+ })[:order].should == [:id.asc]
10
+ end
11
+
12
+ it "specs the rest of the class or it gets the hose" do
13
+ pending "The current spec suite is currently rigidly tied to private models. Someday we'll write a generic spec suite here."
14
+ end
15
+ end
16
+
17
+ describe "Mongo" do
18
+ it "specs the rest of the class or it gets the hose" do
19
+ pending "The current spec suite is currently rigidly tied to private models. Someday we'll write a generic spec suite here."
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'spec'
4
+ require 'ruby-ext-js'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-ext-js
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Tyson Tate
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-20 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 1
34
+ version: 1.3.1
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 1
48
+ - 5
49
+ - 2
50
+ version: 1.5.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.
54
+ email: tyson@doloreslabs.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - LICENSE.txt
61
+ - README.markdown
62
+ files:
63
+ - .document
64
+ - .rspec
65
+ - LICENSE.txt
66
+ - README.markdown
67
+ - Rakefile
68
+ - VERSION
69
+ - lib/ruby-ext-js.rb
70
+ - ruby-ext-js.gemspec
71
+ - spec/ruby-ext-js_spec.rb
72
+ - spec/spec_helper.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/help/ruby-ext-js
75
+ licenses:
76
+ - MIT
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.
107
+ test_files:
108
+ - spec/ruby-ext-js_spec.rb
109
+ - spec/spec_helper.rb