oare 0.1.3 → 0.1.4

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.
@@ -2,21 +2,106 @@ module Oare
2
2
  module Resource
3
3
 
4
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)
26
+ true
27
+ else
28
+ false
29
+ 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
+
5
64
  base.send(:include, InstanceMethods)
6
65
  base.extend(ClassMethods)
7
66
  base.set_default_values
8
67
  end
9
68
 
10
69
  module InstanceMethods
70
+ attr_accessor :nested_attributes_values
71
+
11
72
  def initialize(attributes = {})
73
+ @nested_attributes_values ||= {}
12
74
  self.class.instance_variable_get(:@nested_attributes).each do |key, value|
13
- collection = attributes.delete(key)
75
+ @nested_attributes_values[key] = collection = attributes.delete(key)
14
76
  next unless collection
15
- attributes[value] = collection.map {|i, a| a}
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)
83
+ end
16
84
  end
17
85
  super
18
86
  end
19
- end
87
+
88
+ def create_path(options = {}, query_options = {})
89
+ self.class.create_path(prefix_options.merge(options), query_options)
90
+ end
91
+
92
+ def update_path(options = {}, query_options = {})
93
+ self.class.update_path(to_param, prefix_options.merge(options), query_options)
94
+ end
95
+
96
+ def encode(options={})
97
+ if new?
98
+ keys = nested_attributes_values.keys
99
+ super(options.merge(:methods => keys))
100
+ else
101
+ super
102
+ end
103
+ end
104
+ end # Instance Methods
20
105
 
21
106
  module ClassMethods
22
107
  attr_accessor :access_token
@@ -25,7 +110,7 @@ module Oare
25
110
 
26
111
  def set_default_values
27
112
  self.nested_attributes ||= {}
28
- self.associations ||= []
113
+ self.associations ||= []
29
114
  end
30
115
 
31
116
  def has_many(association_id, options = {})
@@ -33,14 +118,19 @@ module Oare
33
118
  associations << class_name.constantize
34
119
 
35
120
  define_method(association_id) do |*args|
121
+ current_value = instance_variable_get("@#{association_id}".to_sym)
122
+ return current_value if current_value
123
+
36
124
  resource = find_or_create_resource_for_collection(class_name)
37
- if self.new_record? then [resource.new]
38
- else
125
+ value = if self.new_record? then [resource.new]
126
+ else
39
127
  # TODO:
40
128
  # Request for users of account
41
129
  # Use an instance variable version of the access token
42
130
  #
43
- end
131
+ end
132
+ instance_variable_set(
133
+ "@#{association_id}".to_sym, value)
44
134
  end
45
135
  end
46
136
 
@@ -49,6 +139,10 @@ module Oare
49
139
  define_method("#{association_id}_attributes=") do |*args|
50
140
  # TODO
51
141
  end
142
+
143
+ define_method("#{association_id}_attributes") do
144
+ nested_attributes_values["#{association_id}_attributes"]
145
+ end
52
146
  end
53
147
 
54
148
  def access_token=(token)
@@ -69,7 +163,22 @@ module Oare
69
163
  @connection.timeout = timeout if timeout
70
164
  @connection
71
165
  end
72
- end
166
+
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)}"
172
+ end
173
+
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)}"
179
+ end
180
+
181
+ end # Class Methods
73
182
  end # Oare Resource
74
183
 
75
184
  module Gateway
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.3'
8
+ s.version = '0.1.4'
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.3
5
+ version: 0.1.4
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-18 00:00:00 +08:00
14
+ date: 2011-05-19 00:00:00 +08:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency