dm-extjs 0.1.0

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/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Alex Gibbons
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/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "dm-extjs"
15
+ gem.homepage = "http://github.com/alexgb/dm-extjs"
16
+ gem.license = "MIT"
17
+ gem.summary = %Q{DataMapper plugin for serializing resources to work with ExtJs}
18
+ gem.description = %Q{A DataMapper plugin that will serialize and provide meta data descriptions for resources and collections. Compatible with ExtJS and Sencha Touch}
19
+ gem.email = "alex.gibbons [a] gmail [dot] com"
20
+ gem.authors = ["Alex Gibbons"]
21
+ gem.add_dependency 'dm-serializer'
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
data/Readme.md ADDED
@@ -0,0 +1,61 @@
1
+ # dm-extjs
2
+
3
+ A DataMapper plugin that will serialize and provide meta data descriptions for resources and collections. Compatible with ExtJS and Sencha Touch.
4
+
5
+ ## Install
6
+
7
+ Just vendor and require this library to use. Must also have dm-serializer installed.
8
+
9
+ ## Examples
10
+
11
+ Describe a collection of resources:
12
+
13
+ Person.all(:name.like => "%larry%").to_extjs
14
+
15
+ Describe a resource and specific related resources:
16
+
17
+ Person.get(10).to_extjs(
18
+ :methods => [:pets]
19
+ )
20
+
21
+ Describe the success of a save operation for a resource:
22
+
23
+ person = Person.new
24
+ person.attributes = {:name => "Larry", :phone => "238-2934-4444"}
25
+ person.to_extjs(:success => person.save)
26
+
27
+ ## Responses
28
+
29
+ The above examples will produce JSON which resembles the following. These responses should be automagically ingested by your Ext.data.JsonReader.
30
+
31
+ {
32
+ "metaData": {
33
+ "root": "results",
34
+ "successProperty": "success",
35
+ "messageProperty": "message",
36
+ "total": "total",
37
+ "start": "start",
38
+ "limit": "limit",
39
+ "fields": [{
40
+ "name": "id",
41
+ "type": "int"
42
+ },
43
+ {
44
+ "name": "name",
45
+ "type": "string"
46
+ },
47
+ {
48
+ "name": "phone",
49
+ "type": "string"
50
+ }],
51
+ "idProperty": "id"
52
+ },
53
+ "success": true,
54
+ "message": "",
55
+ "total": 1,
56
+ "results": [{
57
+ "id": 1,
58
+ "name": "Larry",
59
+ "phone": "238-2934-4444"
60
+ }]
61
+ }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/dm-extjs.gemspec ADDED
@@ -0,0 +1,69 @@
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{dm-extjs}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alex Gibbons"]
12
+ s.date = %q{2011-03-29}
13
+ s.description = %q{A DataMapper plugin that will serialize and provide meta data descriptions for resources and collections. Compatible with ExtJS and Sencha Touch}
14
+ s.email = %q{alex.gibbons [a] gmail [dot] com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "Rakefile",
24
+ "Readme.md",
25
+ "VERSION",
26
+ "dm-extjs.gemspec",
27
+ "lib/dm-extjs.rb",
28
+ "lib/dm-extjs/collection.rb",
29
+ "lib/dm-extjs/model.rb",
30
+ "lib/dm-extjs/resource.rb",
31
+ "test/helper.rb",
32
+ "test/test_dm-extjs.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/alexgb/dm-extjs}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{DataMapper plugin for serializing resources to work with ExtJs}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_dm-extjs.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
52
+ s.add_development_dependency(%q<rcov>, [">= 0"])
53
+ s.add_runtime_dependency(%q<dm-serializer>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_dependency(%q<rcov>, [">= 0"])
59
+ s.add_dependency(%q<dm-serializer>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ s.add_dependency(%q<dm-serializer>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,17 @@
1
+ module DataMapper
2
+ module ExtJS
3
+ module Collection
4
+
5
+ def to_extjs (options={})
6
+ {
7
+ :metaData => model.ext_meta(options[:methods]),
8
+ :success => options.has_key?(:success) ? options[:success] : true,
9
+ :message => options.has_key?(:message) ? options[:message] : '',
10
+ :total => count,
11
+ :results => self.to_json(options.merge(:to_json => false))
12
+ }.to_json
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,72 @@
1
+ module DataMapper
2
+ module ExtJS
3
+ module Model
4
+
5
+ # generates the meta description of a resource
6
+ # TODO: idProperty is set to the first primary key, in instances
7
+ # where you have multiple keys this would be wrong
8
+ #
9
+ # pass array of relationships (as in to_json(:methods => RELATIONSHIPS))
10
+ # so as to squash provide meta mapping for these related models, this will
11
+ # squash those properties on top of the base model
12
+
13
+ def ext_meta (relationships=nil)
14
+ relationships ||= []
15
+ ret = {
16
+ :fields => properties.collect {|p| property_description(p)},
17
+ :idProperty => properties.key.first.name
18
+ }
19
+
20
+ # map related properties to meta description
21
+ # TODO: cleanup needed
22
+ # because "relationships" are actually all declared :methods, this will
23
+ # try to build meta descriptions for both methods on a model, and
24
+ # relationships on a resource, here we attempt to treat the method as a
25
+ # relationship, then assume it's a model level method with a return type
26
+ # of string
27
+ relationships.each do |r|
28
+ begin
29
+ __send__(r.to_sym).model.properties.each do |p|
30
+ prop = self.property_description(p)
31
+ prop[:mapping] = "#{r}.#{prop[:name]}"
32
+ prop[:name] = "#{r}__#{prop[:name]}"
33
+ ret[:fields].push(prop)
34
+ end
35
+ rescue
36
+ ret[:fields].push({
37
+ :name => r,
38
+ :type => 'string'
39
+ })
40
+ end
41
+
42
+
43
+ end
44
+
45
+ EXT_META.merge(ret)
46
+ end
47
+
48
+ protected
49
+
50
+ # provides property descriptions needed by ext
51
+ def property_description (property)
52
+ ret = {:name => property.name}
53
+
54
+ if property.respond_to?(:custom?) && property.custom?
55
+ if property.respond_to? :flag_map
56
+ ret[:type] = EXT_TYPE_TRANSLATIONS["String"][:type]
57
+ else
58
+ ret[:type] = property.class.name.split('::').last.downcase
59
+ end
60
+ elsif type = EXT_TYPE_TRANSLATIONS[property.primitive.to_s][:type]
61
+ ret[:type] = type
62
+ end
63
+
64
+ if dateFormat = EXT_TYPE_TRANSLATIONS[property.primitive.to_s][:dateFormat]
65
+ ret[:dateFormat] = dateFormat
66
+ end
67
+ ret
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,20 @@
1
+ module DataMapper
2
+ module ExtJS
3
+ module Resource
4
+
5
+ def to_extjs (options={})
6
+ ret = {
7
+ :metaData => self.class.ext_meta(options[:methods]),
8
+ :success => options.has_key?(:success) ? options[:success] : (saved? && clean?),
9
+ :message => (ext_errors << (options[:message] || '')).join("\n"),
10
+ :results => self.to_json(options.merge(:to_json => false))
11
+ }.to_json
12
+ end
13
+
14
+ def ext_errors
15
+ errors.to_a unless !respond_to?(:errors)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
data/lib/dm-extjs.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'dm-serializer'
2
+
3
+ require File.join(File.dirname(__FILE__), 'dm-extjs', 'collection')
4
+ require File.join(File.dirname(__FILE__), 'dm-extjs', 'resource')
5
+ require File.join(File.dirname(__FILE__), 'dm-extjs', 'model')
6
+
7
+ module DataMapper
8
+ module ExtJS
9
+ EXT_SORT_PARAM = 'order'
10
+ EXT_LIMIT_PARAM = 'limit'
11
+ EXT_OFFSET_PARAM = 'start'
12
+ EXT_DIRECTION_PARAM = '_dir'
13
+
14
+ EXT_META = {
15
+ :root => 'results',
16
+ :successProperty => 'success',
17
+ :messageProperty => 'message',
18
+ :total => 'total',
19
+ :start => EXT_OFFSET_PARAM,
20
+ :limit => EXT_LIMIT_PARAM
21
+ }
22
+
23
+ DM_PARAM_TRANSLATIONS = {
24
+ EXT_SORT_PARAM => :order,
25
+ EXT_LIMIT_PARAM => :limit,
26
+ EXT_OFFSET_PARAM => :offset
27
+ }
28
+
29
+ EXT_TYPE_TRANSLATIONS = {
30
+ 'String' => {:type => 'string'},
31
+ 'Integer' => {:type => 'int'},
32
+ 'Fixnum' => {:type => 'int'},
33
+ 'Float' => {:type => 'float'},
34
+ 'TrueClass' => {:type => 'boolean'},
35
+ 'FalseClass' => {:type => 'boolean'},
36
+ 'Date' => {:type => 'date', :dateFormat => 'Y-m-d'},
37
+ 'DateTime' => {:type => 'date', :dateFormat => 'c'}
38
+ }
39
+ end
40
+ end
41
+
42
+ # include in DM
43
+ module DataMapper
44
+ Model.append_inclusions(ExtJS::Resource)
45
+ Model.append_extensions(ExtJS::Model)
46
+
47
+ class Collection
48
+ include ExtJS::Collection
49
+ end
50
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'dm-extjs'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestDmExtjs < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-extjs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Alex Gibbons
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-29 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ prerelease: false
24
+ name: shoulda
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ prerelease: false
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 2
65
+ version: 1.5.2
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: rcov
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ type: :runtime
83
+ prerelease: false
84
+ name: dm-serializer
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirement: *id005
95
+ description: A DataMapper plugin that will serialize and provide meta data descriptions for resources and collections. Compatible with ExtJS and Sencha Touch
96
+ email: alex.gibbons [a] gmail [dot] com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ files:
104
+ - .document
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - Rakefile
109
+ - Readme.md
110
+ - VERSION
111
+ - dm-extjs.gemspec
112
+ - lib/dm-extjs.rb
113
+ - lib/dm-extjs/collection.rb
114
+ - lib/dm-extjs/model.rb
115
+ - lib/dm-extjs/resource.rb
116
+ - test/helper.rb
117
+ - test/test_dm-extjs.rb
118
+ has_rdoc: true
119
+ homepage: http://github.com/alexgb/dm-extjs
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.3.7
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: DataMapper plugin for serializing resources to work with ExtJs
152
+ test_files:
153
+ - test/helper.rb
154
+ - test/test_dm-extjs.rb