oare 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,213 +1,187 @@
1
- module Oare
2
- module Resource
3
-
4
- def self.included(base)
5
- base.instance_eval <<-RUBY
6
-
7
- alias_method :original_save, :save
8
- undef_method :save
9
- define_method :save do |options = {}, query_options = nil|
10
- begin
11
- perform_validation = case options
12
- when Hash
13
- options.delete(:validate) != false
14
- when NilClass
15
- true
16
- else
17
- options
18
- end
19
-
20
- # clear the remote validations so they don't interfere with the local
21
- # ones. Otherwise we get an endless loop and can never change the
22
- # fields so as to make the resource valid
23
- @remote_errors = nil
24
- if perform_validation && valid? || !perform_validation
25
- save_without_validation(options, query_options)
1
+ module Oare::Resource
2
+ def self.included(base)
3
+ base.instance_eval <<-RUBY
4
+
5
+ alias_method :original_save, :save
6
+ undef_method :save
7
+ define_method :save do |options = {}, query_options = nil|
8
+ begin
9
+ perform_validation = case options
10
+ when Hash
11
+ options.delete(:validate) != false
12
+ when NilClass
26
13
  true
27
14
  else
28
- false
15
+ options
29
16
  end
30
- rescue ActiveResource::ResourceInvalid => error
31
- # cache the remote errors because every call to <tt>valid?</tt> clears
32
- # all errors. We must keep a copy to add these back after local
33
- # validations
34
- @remote_errors = error
35
- load_remote_errors(@remote_errors, true)
36
- false
37
- end
38
- end
39
-
40
- alias_method :original_save_without_validation, :save_without_validation
41
- undef_method :save_without_validation
42
- define_method :save_without_validation do |path_options = {}, query_options = nil|
43
- new? ? create(path_options, query_options) : update(path_options, query_options)
44
- end
45
-
46
- alias_method :original_create, :create
47
- undef_method :create
48
- define_method :create do |path_options = {}, query_options = nil|
49
- connection.post(create_path(path_options, query_options), encode, self.class.headers).tap do |response|
50
- self.id = id_from_response(response)
51
- load_attributes_from_response(response)
52
- end
53
- end
54
-
55
- alias_method :original_update, :update
56
- undef_method :update
57
- define_method :update do |path_options = {}, query_options = nil|
58
- connection.put(update_path(path_options, query_options), encode, self.class.headers).tap do |response|
59
- load_attributes_from_response(response)
60
- end
61
- end
62
- RUBY
63
17
 
64
- base.send(:include, InstanceMethods)
65
- base.extend(ClassMethods)
66
- base.set_default_values
67
- end
68
-
69
- module InstanceMethods
70
- attr_accessor :nested_attributes_values
71
-
72
- def initialize(attributes = {})
73
- @nested_attributes_values ||= {}
74
- self.class.instance_variable_get(:@nested_attributes).each do |key, value|
75
- @nested_attributes_values[key] = collection = attributes.delete(key)
76
- next unless collection
77
-
78
- collection.each do |index, associate_attributes|
79
- nested_model = value.to_s
80
- collection = []
81
- collection[index.to_i] = nested_model.singularize.camelize.constantize.new(associate_attributes)
82
- instance_variable_set("@#{nested_model}", collection)
18
+ # clear the remote validations so they don't interfere with the local
19
+ # ones. Otherwise we get an endless loop and can never change the
20
+ # fields so as to make the resource valid
21
+ @remote_errors = nil
22
+ if perform_validation && valid? || !perform_validation
23
+ save_without_validation(options, query_options)
24
+ true
25
+ else
26
+ false
83
27
  end
28
+ rescue ActiveResource::ResourceInvalid => error
29
+ # cache the remote errors because every call to <tt>valid?</tt> clears
30
+ # all errors. We must keep a copy to add these back after local
31
+ # validations
32
+ @remote_errors = error
33
+ load_remote_errors(@remote_errors, true)
34
+ false
84
35
  end
85
- super
86
36
  end
87
37
 
88
- def create_path(options = {}, query_options = {})
89
- self.class.create_path(prefix_options.merge(options), query_options)
38
+ alias_method :original_save_without_validation, :save_without_validation
39
+ undef_method :save_without_validation
40
+ define_method :save_without_validation do |path_options = {}, query_options = nil|
41
+ new? ? create(path_options, query_options) : update(path_options, query_options)
90
42
  end
91
43
 
92
- def update_path(options = {}, query_options = {})
93
- self.class.update_path(to_param, prefix_options.merge(options), query_options)
44
+ alias_method :original_create, :create
45
+ undef_method :create
46
+ define_method :create do |path_options = {}, query_options = nil|
47
+ connection.post(create_path(path_options, query_options), encode, self.class.headers).tap do |response|
48
+ self.id = id_from_response(response)
49
+ load_attributes_from_response(response)
50
+ end
94
51
  end
95
52
 
96
- def encode(options={})
97
- if new?
98
- keys = nested_attributes_values.keys
99
- super(options.merge(:methods => keys))
100
- else
101
- super
53
+ alias_method :original_update, :update
54
+ undef_method :update
55
+ define_method :update do |path_options = {}, query_options = nil|
56
+ connection.put(update_path(path_options, query_options), encode, self.class.headers).tap do |response|
57
+ load_attributes_from_response(response)
102
58
  end
103
59
  end
104
- end # Instance Methods
105
60
 
106
- module ClassMethods
107
- attr_accessor :access_token
108
- attr_accessor :associations
109
- attr_accessor :nested_attributes
61
+ RUBY
110
62
 
111
- def set_default_values
112
- self.nested_attributes ||= {}
113
- self.associations ||= []
114
- end
63
+ base.send(:include, InstanceMethods)
64
+ base.extend(ClassMethods)
65
+ base.set_default_values
66
+ end
115
67
 
116
- def has_many(association_id, options = {})
117
- class_name = options[:class_name] || association_id.to_s.singularize.camelize
118
- associations << class_name.constantize
68
+ module InstanceMethods
69
+ attr_accessor :nested_attributes_values
119
70
 
120
- define_method(association_id) do |*args|
121
- current_value = instance_variable_get("@#{association_id}".to_sym)
122
- return current_value if current_value
71
+ def initialize(attributes = {})
72
+ @nested_attributes_values ||= {}
73
+ self.class.instance_variable_get(:@nested_attributes).each do |key, value|
74
+ @nested_attributes_values[key] = collection = attributes.delete(key)
75
+ next unless collection
123
76
 
124
- resource = find_or_create_resource_for_collection(class_name)
125
- value = if self.new_record? then [resource.new]
126
- else
127
- # TODO:
128
- # Request for users of account
129
- # Use an instance variable version of the access token
130
- #
131
- end
132
- instance_variable_set(
133
- "@#{association_id}".to_sym, value)
77
+ collection.each do |index, associate_attributes|
78
+ nested_model = value.to_s
79
+ collection = []
80
+ collection[index.to_i] = nested_model.singularize.camelize.constantize.new(associate_attributes)
81
+ instance_variable_set("@#{nested_model}", collection)
134
82
  end
135
83
  end
84
+ super
85
+ end
136
86
 
137
- def accepts_nested_attributes_for(association_id, options = {})
138
- nested_attributes["#{association_id}_attributes"] = association_id
139
- define_method("#{association_id}_attributes=") do |*args|
140
- # TODO
141
- end
87
+ def create_path(options = {}, query_options = {})
88
+ self.class.create_path(prefix_options.merge(options), query_options)
89
+ end
142
90
 
143
- define_method("#{association_id}_attributes") do
144
- nested_attributes_values["#{association_id}_attributes"]
145
- end
146
- end
91
+ def update_path(options = {}, query_options = {})
92
+ self.class.update_path(to_param, prefix_options.merge(options), query_options)
93
+ end
147
94
 
148
- def access_token=(token)
149
- @access_token = token
150
- self.site = token.consumer.site
95
+ def encode(options={})
96
+ if new?
97
+ keys = nested_attributes_values.keys
98
+ super(options.merge(:methods => keys))
99
+ else
100
+ super
151
101
  end
102
+ end
152
103
 
153
- def connection(refresh = true)
154
- self.access_token ? self.oauth_connection : super
155
- end
104
+ def errors
105
+ @errors ||= Oare::Errors.new(self)
106
+ end
107
+ end # Instance Methods
108
+
109
+ module ClassMethods
110
+ attr_accessor :access_token
111
+ attr_accessor :associations
112
+ attr_accessor :nested_attributes
156
113
 
157
- def oauth_connection(refresh = true)
158
- associations.each do |model_constant|
159
- model_constant.access_token = self.access_token
160
- end if associations
114
+ def set_default_values
115
+ self.nested_attributes ||= {}
116
+ self.associations ||= []
117
+ end
161
118
 
162
- @connection = Oare::Connection.new(self.access_token) if @connection.nil? || refresh
163
- @connection.timeout = timeout if timeout
164
- @connection
119
+ def has_many(association_id, options = {})
120
+ class_name = options[:class_name] || association_id.to_s.singularize.camelize
121
+ associations << class_name.constantize
122
+
123
+ define_method(association_id) do |*args|
124
+ current_value = instance_variable_get("@#{association_id}".to_sym)
125
+ return current_value if current_value
126
+
127
+ resource = find_or_create_resource_for_collection(class_name)
128
+ value = if self.new_record? then [resource.new]
129
+ else
130
+ # TODO:
131
+ # Request for users of account
132
+ # Use an instance variable version of the access token
133
+ #
134
+ end
135
+ instance_variable_set(
136
+ "@#{association_id}".to_sym, value)
165
137
  end
138
+ end
166
139
 
167
- def create_path(path_options = {}, query_options = nil)
168
- path = path_options.delete(:path)
169
- prefix_options = path_options
170
- return collection_path(prefix_options, query_options) unless path
171
- "/#{path}.#{format.extension}#{query_string(query_options)}"
140
+ def accepts_nested_attributes_for(association_id, options = {})
141
+ nested_attributes["#{association_id}_attributes"] = association_id
142
+ define_method("#{association_id}_attributes=") do |*args|
143
+ # TODO
172
144
  end
173
145
 
174
- def update_path(id, path_options = {}, query_options = nil)
175
- path = path_options.delete(:path)
176
- prefix_options = path_options
177
- return collection_path(id, prefix_options, query_options) unless path
178
- "/#{path}/#{URI.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
146
+ define_method("#{association_id}_attributes") do
147
+ nested_attributes_values["#{association_id}_attributes"]
179
148
  end
149
+ end
180
150
 
181
- end # Class Methods
182
- end # Oare Resource
151
+ def access_token=(token)
152
+ @access_token = token
153
+ self.site = token.consumer.site
154
+ end
183
155
 
184
- module Gateway
156
+ def connection(refresh = true)
157
+ self.access_token ? self.oauth_connection : super
158
+ end
185
159
 
186
- def method_missing(method, *args, &block)
187
- collection = ::ActiveResource::Base.oauth_enabled_classes
188
- if collection.keys.include? method
189
- constant = collection[method].join('::').constantize
190
- constant.access_token = self.access_token
191
- #Rails.logger.info constant.access_token.inspect
192
- #Rails.logger.info ::ActiveResource::Base.access_token.inspect
193
- #sleep 20
194
- constant
195
- else
196
- super(method, *args, &block)
197
- end
160
+ def oauth_connection(refresh = true)
161
+ associations.each do |model_constant|
162
+ model_constant.access_token = self.access_token
163
+ end if associations
164
+
165
+ @connection = Oare::Connection.new(self.access_token) if @connection.nil? || refresh
166
+ @connection.timeout = timeout if timeout
167
+ @connection
198
168
  end
199
169
 
200
- def access_token
201
- ## This method assumes that the Gateway Model has the fields/methods for:
202
- # oauth_token : string,
203
- # oauth_token_secret : string,
204
- # consumer : consumer token
205
- #
170
+ def create_path(path_options = {}, query_options = nil)
171
+ path = path_options.delete(:path)
172
+ prefix_options = path_options
173
+ return collection_path(prefix_options, query_options) unless path
174
+ "/#{path}.#{format.extension}#{query_string(query_options)}"
175
+ end
206
176
 
207
- OAuth::AccessToken.new(consumer, oauth_token, oauth_token_secret)
177
+ def update_path(id, path_options = {}, query_options = nil)
178
+ path = path_options.delete(:path)
179
+ prefix_options = path_options
180
+ return collection_path(id, prefix_options, query_options) unless path
181
+ "/#{path}/#{URI.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
208
182
  end
209
183
 
210
- end
184
+ end # Class Methods
211
185
  end
212
186
 
213
187
  # Monkey Patch Active Resource and Action Controller
@@ -0,0 +1,26 @@
1
+ class Oare::Errors < ActiveResource::Errors
2
+ def from_array(messages, save_cache = false)
3
+ clear unless save_cache
4
+ humanized_nested_attributes = @base.nested_attributes_values.map do |model, v|
5
+ v.map do |i, v2|
6
+ v2.keys.map do |k|
7
+ attribute_name = "#{model.gsub('_attributes','')}_#{k}".underscore
8
+ [attribute_name.humanize, attribute_name]
9
+ end
10
+ end
11
+ end.flatten
12
+ humanized_nested_attributes = Hash[*humanized_nested_attributes]
13
+
14
+ humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
15
+ humanized_attributes.merge!(humanized_nested_attributes)
16
+ messages.each do |message|
17
+ attr_message = humanized_attributes.keys.detect do |attr_name|
18
+ if message[0, attr_name.size + 1] == "#{attr_name} "
19
+ add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
20
+ end
21
+ end
22
+
23
+ self[:base] << message if attr_message.nil?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module Oare
2
+ module Gateway
3
+ def method_missing(method, *args, &block)
4
+ collection = ::ActiveResource::Base.oauth_enabled_classes
5
+ if collection.keys.include? method
6
+ constant = collection[method].join('::').constantize
7
+ constant.access_token = self.access_token
8
+ #Rails.logger.info constant.access_token.inspect
9
+ #Rails.logger.info ::ActiveResource::Base.access_token.inspect
10
+ #sleep 20
11
+ constant
12
+ else
13
+ super(method, *args, &block)
14
+ end
15
+ end
16
+
17
+ def access_token
18
+ ## This method assumes that the Gateway Model has the fields/methods for:
19
+ # oauth_token : string,
20
+ # oauth_token_secret : string,
21
+ # consumer : consumer token
22
+ #
23
+
24
+ OAuth::AccessToken.new(consumer, oauth_token, oauth_token_secret)
25
+ end
26
+ end
27
+ end
data/lib/oare.rb CHANGED
@@ -2,6 +2,12 @@ module Oare
2
2
  end
3
3
 
4
4
  if defined?(Rails) && Rails::VERSION::MAJOR == 3
5
+ require 'active_resource'
6
+ require 'active_resource/base'
7
+ require 'active_resource/validations'
8
+
5
9
  require File.expand_path('active_resource_override/connection', File.dirname(__FILE__))
10
+ require File.expand_path('active_resource_override/errors' , File.dirname(__FILE__))
6
11
  require File.expand_path('active_resource_override/base' , File.dirname(__FILE__))
12
+ require File.expand_path('extensions/gateway' , File.dirname(__FILE__))
7
13
  end
data/oare.gemspec CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
5
5
  ## Basic Information
6
6
  #
7
7
  s.name = 'oare'
8
- s.version = '0.1.4'
8
+ s.version = '0.1.6'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.required_ruby_version = '~> 1.9'
11
11
  s.required_rubygems_version = '>= 1.3.6'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: oare
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.4
5
+ version: 0.1.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nelvin Driz
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-05-19 00:00:00 +08:00
14
+ date: 2011-05-20 00:00:00 +08:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,8 @@ files:
45
45
  - init.rb
46
46
  - lib/active_resource_override/base.rb
47
47
  - lib/active_resource_override/connection.rb
48
+ - lib/active_resource_override/errors.rb
49
+ - lib/extensions/gateway.rb
48
50
  - lib/oare.rb
49
51
  - oare.gemspec
50
52
  has_rdoc: true