simplemapper 0.0.5 → 0.0.6

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.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
  require 'rake/contrib/rubyforgepublisher'
7
7
 
8
8
  PKG_NAME = 'simplemapper'
9
- PKG_VERSION = "0.0.5"
9
+ PKG_VERSION = "0.0.6"
10
10
 
11
11
  PKG_FILES = FileList[
12
12
  "lib/**/*", "rspec/**/*", "[A-Z]*", "Rakefile", "doc/**/*"
data/lib/simple_mapper.rb CHANGED
@@ -13,4 +13,4 @@
13
13
  # "finder" options, like converting an options hash to a sql SELECT request, or mapping an options hash to an HTTP request.
14
14
  # It is purposed to make it as light and simple as possible to plug these in.
15
15
 
16
- require 'simple_mapper/base'
16
+ require File.expand_path(File.dirname(__FILE__) + '/simple_mapper/base')
@@ -87,7 +87,8 @@ module SimpleMapper
87
87
  request.body = body
88
88
  request.initialize_http_header headers.merge(options[:headers] || {})
89
89
  # - - - after_request_instantiate callback
90
- request = run_callback('initialize_request', request)
90
+ res = run_callback('initialize_request', request)
91
+ request = res if res.is_a?(request.class)
91
92
  # - - -
92
93
  request
93
94
  end
@@ -1,11 +1,9 @@
1
- require File.expand_path('simple_mapper/support')
2
- require File.expand_path('simple_mapper/plugin_support')
3
-
1
+ require File.expand_path(File.dirname(__FILE__) + '/persistence')
4
2
  module SimpleMapper
5
3
  class Base
6
4
  def self.inherited(klass)
7
- klass.send(:include, SimpleMapper::Persistence)
8
5
  SimpleMapper::Persistence::prepare_for_inheritance(klass)
6
+ klass.send(:include, SimpleMapper::Persistence)
9
7
  end
10
8
  end
11
9
  end
@@ -2,6 +2,8 @@ gem 'oauth'
2
2
  require 'oauth'
3
3
  require 'oauth/consumer'
4
4
  require 'oauth/client/net_http'
5
+
6
+ # First some fixes...
5
7
  module OAuth
6
8
  module Signature
7
9
  class Base
@@ -28,7 +30,7 @@ module OAuth
28
30
  end
29
31
 
30
32
  module SimpleMapper
31
- module HttpOAuthExtension
33
+ module Oauth
32
34
  def requires_oauth(consumer_key, consumer_secret, options={})
33
35
  @consumer_key = consumer_key
34
36
  @consumer_secret = consumer_secret
@@ -64,7 +66,7 @@ module SimpleMapper
64
66
  end
65
67
 
66
68
  class HttpAdapter
67
- include HttpOAuthExtension
69
+ include Oauth
68
70
  end
69
71
  end
70
72
 
@@ -43,7 +43,7 @@ module SimpleMapper
43
43
  # way you want. For example, you could set the data to some other object type, or to a Marshalled storage.
44
44
  # The type of data you receive will depend on the format and parser you use. Of course you could make up
45
45
  # your own spin-off of one of those, too.
46
- def data=(hash)
46
+ def data=(data)
47
47
  raise TypeError, "data must be a hash" unless data.is_a?(Hash)
48
48
  data.each {|k,v| instance_variable_set("@#{k}", v)}
49
49
  end
@@ -0,0 +1,50 @@
1
+ require 'simple_mapper/support/bliss_serializer'
2
+ require 'rubygems'
3
+ gem 'formattedstring'
4
+ require 'formatted_string'
5
+
6
+ module SimpleMapper
7
+ module JsonFormat
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ def self.mime_type_headers
13
+ {'Accept' => 'application/json', 'Content-type' => 'application/json'}
14
+ end
15
+
16
+ def to_json
17
+ json = Serialize.object_to_json(self, :key_name => 'self').to_s
18
+ # puts "POST JSON: #{json}"
19
+ json
20
+ end
21
+
22
+ module ClassMethods
23
+ # This assumes a standard json format:
24
+ # {'person':{'attribute':'','another_att':'value'}}
25
+ # And for a collection of objects:
26
+ # {'people':[{'person':{'attribute':'','another_att':'value 1'}},{'person':{'attribute':'','another_att':'value'}}]}
27
+ def from_json(json)
28
+ doc = Serialize.hash_from_json(json)
29
+ # doc could include a single 'model' element, or a 'models' wrapper around several.
30
+ puts "Top-level JSON key(s): #{doc.keys.inspect}" if @debug
31
+ if doc.is_a?(Hash)
32
+ key = doc.keys.first
33
+ if doc[key] && doc[key].keys.uniq == [key.singularize] && doc[key][key.singularize].is_a?(Array)
34
+ puts "Several objects returned under key '#{key}'/'#{key.singularize}':" if @debug
35
+ doc[key][key.singularize].collect do |e|
36
+ puts "Obj: #{e.inspect}" if @debug
37
+ Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(e)
38
+ end
39
+ elsif doc[key] # top-level must be single object
40
+ Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(doc[self.name.underscore])
41
+ else
42
+ nil
43
+ end
44
+ else # doc isn't a hash, probably nil
45
+ doc
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -37,17 +37,21 @@ module SimpleMapper
37
37
  doc = Serialize.hash_from_xml(xml)
38
38
  # doc could include a single 'model' element, or a 'models' wrapper around several.
39
39
  puts "Top-level XML key(s): #{doc.keys.inspect}" if @debug
40
- key = doc.keys.first
41
- if doc[key] && doc[key].keys.uniq == [key.singularize] && doc[key][key.singularize].is_a?(Array)
42
- puts "Several objects returned under key '#{key}'/'#{key.singularize}':" if @debug
43
- doc[key][key.singularize].collect do |e|
44
- puts "Obj: #{e.inspect}" if @debug
45
- Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(e)
40
+ if doc.is_a?(Hash)
41
+ key = doc.keys.first
42
+ if doc[key] && doc[key].keys.uniq == [key.singularize] && doc[key][key.singularize].is_a?(Array)
43
+ puts "Several objects returned under key '#{key}'/'#{key.singularize}':" if @debug
44
+ doc[key][key.singularize].collect do |e|
45
+ puts "Obj: #{e.inspect}" if @debug
46
+ Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(e)
47
+ end
48
+ elsif doc[key] # top-level must be single object
49
+ Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(doc[self.name.underscore])
50
+ else
51
+ nil
46
52
  end
47
- elsif doc[key] # top-level must be single object
48
- Object.module_eval("::#{key.singularize.camelize}", __FILE__, __LINE__).load(Serialize.hash_from_xml(xml)[self.name.underscore])
49
- else
50
- nil
53
+ else # doc isn't a hash, probably nil
54
+ doc
51
55
  end
52
56
  end
53
57
  end
@@ -1,10 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/support')
1
2
  module SimpleMapper
2
3
  module Persistence
3
- include PluginSupport
4
-
5
4
  # TODO: Write in inheritability for models. Should this instead be a place that plugins can plug in a callback?
6
5
  def self.prepare_for_inheritance(klass)
7
-
6
+ end
7
+
8
+ def self.included(klass)
9
+ klass.extend ClassMethods
10
+ require File.expand_path(File.dirname(__FILE__) + '/plugin_support')
11
+ klass.send(:include, SimpleMapper::PluginSupport)
8
12
  end
9
13
 
10
14
  module ClassMethods
@@ -83,7 +87,7 @@ module SimpleMapper
83
87
  end
84
88
 
85
89
  def extract_from(formatted_data)
86
- objs = send("from_#{format_name}".to_sym, formatted_data)
90
+ objs = send(:"from_#{format_name}", formatted_data)
87
91
  objs.is_a?(Array) ? objs.collect {|e| e.extended {@persisted = true}} : objs.extended {@persisted = true}
88
92
  end
89
93
 
@@ -98,8 +102,8 @@ module SimpleMapper
98
102
 
99
103
  def load(data=nil)
100
104
  obj = allocate
101
- obj.original_data = data
102
105
  obj.send(:initialize, data)
106
+ obj.original_data = data
103
107
  obj
104
108
  end
105
109
 
@@ -48,7 +48,7 @@ module Serialize
48
48
  if value.respond_to?(:to_xml)
49
49
  assoc_options = {}
50
50
  assoc_options = {:exclude => value.association.foreign_key_column.name} if value.respond_to?(:association)
51
- root_element.add_element(REXML::Document.new(value.to_xml(assoc_options.merge(:root => key)).to_s))
51
+ root_element.add_element(REXML::Document.new(value.to_xml(assoc_options.merge(:root => key.to_s)).to_s))
52
52
  else
53
53
  root_element.add_element(key.to_s) << REXML::Text.new(value.to_s.dup)
54
54
  end
@@ -57,16 +57,43 @@ module Serialize
57
57
 
58
58
  root ? doc.to_s : doc.children[0].children.to_s
59
59
  end
60
-
61
60
  def self.hash_from_xml(xml,options={})
62
- xml.formatted(:xml).to_hash
61
+ xml.to_s.formatted(:xml).to_hash
62
+ end
63
+
64
+ JSON_OPTIONS = {
65
+ :include_key => false, # Can be true or false
66
+ :key_name => :id, # Default key name
67
+ :instance_columns => :visible_properties,
68
+ :class_columns => :visible_properties,
69
+ }
70
+ def self.object_to_json(obj, options={})
71
+ # Automatically set the key_name for DataMapper objects
72
+ options.merge!(:key_name => obj.class.table.key.name) if obj.class.respond_to?(:table) && obj.class.respond_to?(:persistent?) && obj.class.persistent?
73
+ # Should do the above also for ActiveRecord...
74
+ options = options.reverse_merge!(obj.class.json_options) if obj.class.respond_to?(:json_options)
75
+ options = options.reverse_merge!(obj.json_options) if obj.respond_to?(:json_options)
76
+ to_json(obj.to_hash(options), {:root => Inflector.underscore(obj.class.name)}.merge(options))
77
+ end
78
+ def self.to_json(attributes={}, options={})
79
+ # max-results=25 || limit=25
80
+ # start-index=26 || offset=26
81
+ raise NotImplemented
82
+ options = JSON_OPTIONS.merge(options)
83
+ root = options[:root]
84
+ options[:exclude] = [options[:exclude]].flatten.compact.collect {|e| e.to_s}
85
+ attributes.reject! {|k,v| options[:exclude].include?(k)}
86
+ end
87
+ def self.hash_from_json(json,options={})
88
+ json.to_s.formatted(:json).to_hash
63
89
  end
64
90
  end
65
91
 
66
92
  class Hash
67
93
  def to_xml(options={})
68
- options[:root] = keys.length == 1 ? keys[0] : nil if !options.has_key?(:root)
69
- Serialize.to_xml(self.slashed, options.merge(:root => (options.has_key?(:root) ? options[:root] : 'xml')))
94
+ options[:root] = keys[0] if keys.length == 1 && (!options.has_key?(:root) || options[:root].to_s == '')
95
+ options[:root] = 'xml' if options[:root].to_s == ''
96
+ Serialize.to_xml(self.slashed, options)
70
97
  end
71
98
  end
72
99
 
@@ -76,6 +103,12 @@ class Array
76
103
  end
77
104
  end
78
105
 
106
+ class Time
107
+ def to_xml
108
+ xmlschema
109
+ end
110
+ end
111
+
79
112
  module Merb
80
113
  class Request
81
114
  def xml_params
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplemapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Parker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-15 00:00:00 -04:00
12
+ date: 2008-05-09 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -34,6 +34,7 @@ files:
34
34
  - lib/simple_mapper/default_plugins/options_to_query.rb
35
35
  - lib/simple_mapper/default_plugins/properties.rb
36
36
  - lib/simple_mapper/formats
37
+ - lib/simple_mapper/formats/json_format.rb
37
38
  - lib/simple_mapper/formats/xml_format.rb
38
39
  - lib/simple_mapper/persistence.rb
39
40
  - lib/simple_mapper/plugin_support.rb