api_resource 0.6.18 → 0.6.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eecd2283b1df3b5e9cbbebdeded82ac62eadaf57
4
- data.tar.gz: b12734c1fb1b5dcabe606f704d9b7d6ab9628ba0
3
+ metadata.gz: b18f870fcfb29b4ffb83c8c580b12c55c81462e0
4
+ data.tar.gz: 2667fb9ea45c86ef67f5d10ddf4d8b850db3e83f
5
5
  SHA512:
6
- metadata.gz: 369173ff08a0384adc0b89891033668c84a66c3a1f2a92c822fb84b33e98eb968474239881cbb54f12f73b68dbafb0e9c00031a1bfe10a9b9820740a128143b2
7
- data.tar.gz: 0d454b872616d96c7aa3952cfbbccffdc96f0df84b97d2d03ef4e7fc5126d499de774bac08e2d2f8adfadf207744f2885fd47d04f9b06b4c26e33319b94af38a
6
+ metadata.gz: 4d100621cbaad1f11eef96fe05ae963f83ed5a51a392d1bc483d1adedf53a1a881d736f5cf1c1b73063e7e2f3d4a6b44be89b56a16cafa1ccb959fb80923802e
7
+ data.tar.gz: d9ebcf5a2777642ef95d308e1737ebd1700513cbf7ff8c522b2b7991127f7448e2e97183b3744566d044a1efe33f02b58bcd464cbbaab20d64276e9b210872c2
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api_resource (0.6.17)
4
+ api_resource (0.6.18)
5
+ activemodel
5
6
  colorize
6
7
  differ
8
+ httpclient
7
9
  json
8
10
  log4r
9
11
  rails
10
- rest-client
11
12
 
12
13
  GEM
13
14
  remote: https://rubygems.org/
@@ -83,6 +84,7 @@ GEM
83
84
  colorize
84
85
  i18n
85
86
  hike (1.2.3)
87
+ httpclient (2.3.4.1)
86
88
  i18n (0.6.1)
87
89
  journey (1.0.4)
88
90
  json (1.8.1)
@@ -130,8 +132,6 @@ GEM
130
132
  rb-fsevent (0.9.3)
131
133
  rdoc (3.12.2)
132
134
  json (~> 1.4)
133
- rest-client (1.6.7)
134
- mime-types (>= 1.16)
135
135
  rspec (2.12.0)
136
136
  rspec-core (~> 2.12.0)
137
137
  rspec-expectations (~> 2.12.0)
@@ -154,7 +154,6 @@ GEM
154
154
  multi_json (~> 1.0)
155
155
  rack (~> 1.0)
156
156
  tilt (~> 1.1, != 1.3.0)
157
- sqlite3 (1.3.6)
158
157
  thor (0.16.0)
159
158
  tilt (1.4.1)
160
159
  treetop (1.4.15)
@@ -182,8 +181,5 @@ DEPENDENCIES
182
181
  mocha (= 0.12.7)
183
182
  rake
184
183
  rb-fsevent
185
- rspec
186
184
  simplecov
187
- spork
188
- sqlite3
189
185
  yarjuf
data/Guardfile CHANGED
@@ -12,22 +12,10 @@ guard 'rspec', :version => 2, :cli => "--color --format nested --drb", :all_on_s
12
12
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
13
13
  watch(%r{^lib/api_resource/(.+)\.rb$}) {|m| "spec/lib/#{m[1]}_spec.rb"}
14
14
  watch('spec/spec_helper.rb') { "spec/" }
15
-
16
- # Rails example
17
- watch(%r{^spec/.+_spec\.rb$})
18
- watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
19
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
20
- watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
21
- watch(%r{^spec/support/(.+)\.rb$}) { "spec/" }
22
- watch('spec/spec_helper.rb') { "spec/" }
23
- watch('config/routes.rb') { "spec/routing" }
24
- watch('app/controllers/application_controller.rb') { "spec/controllers" }
25
- # Capybara request specs
26
- watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
27
15
  end
28
16
 
29
- guard 'spork' do
30
- watch('api_resource.gemspec')
31
- watch('Gemfile.lock')
32
- watch('spec/spec_helper.rb') { :rspec }
33
- end
17
+ #guard 'spork' do
18
+ # watch('api_resource.gemspec')
19
+ # watch('Gemfile.lock')
20
+ # watch('spec/spec_helper.rb') { :rspec }
21
+ #end
data/api_resource.gemspec CHANGED
@@ -17,9 +17,7 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  # Development Dependencies
19
19
  gem.add_development_dependency "rake"
20
- gem.add_development_dependency "rspec"
21
- gem.add_development_dependency "spork"
22
- gem.add_development_dependency "yarjuf"
20
+ gem.add_development_dependency "yarjuf"
23
21
  # this latest version of mocha is not compatible with the rails
24
22
  # 3.2.9
25
23
  gem.add_development_dependency "mocha", ["=0.12.7"]
@@ -32,14 +30,13 @@ Gem::Specification.new do |gem|
32
30
  gem.add_development_dependency "flog"
33
31
  gem.add_development_dependency "hash_dealer"
34
32
  gem.add_development_dependency "rb-fsevent"
35
- gem.add_development_dependency "byebug"
36
33
  gem.add_development_dependency "simplecov"
37
- # stuff that seems like crap
38
- gem.add_development_dependency "sqlite3"
34
+ gem.add_development_dependency 'byebug'
39
35
 
36
+ gem.add_dependency "httpclient"
40
37
  gem.add_dependency "rails"
38
+ gem.add_dependency 'activemodel'
41
39
  gem.add_dependency "json"
42
- gem.add_dependency "rest-client"
43
40
  gem.add_dependency "log4r"
44
41
  gem.add_dependency "differ"
45
42
  gem.add_dependency "colorize"
data/lib/api_resource.rb CHANGED
@@ -14,9 +14,9 @@ require 'differ'
14
14
  require 'colorize'
15
15
 
16
16
  module ApiResource
17
-
17
+
18
18
  extend ActiveSupport::Autoload
19
-
19
+
20
20
  autoload :Associations
21
21
  autoload :AssociationActivation
22
22
  autoload :Attributes
@@ -34,53 +34,54 @@ module ApiResource
34
34
  autoload :ModelErrors
35
35
  autoload :Observing
36
36
  autoload :Scopes
37
+ autoload :Serializer
37
38
  autoload :Typecast
38
39
  autoload :Validations
39
-
40
+
40
41
  require 'api_resource/railtie'
41
-
42
+
42
43
  mattr_writer :logger
43
44
  mattr_accessor :raise_missing_definition_error; self.raise_missing_definition_error = false
44
45
 
45
46
  DEFAULT_TIMEOUT = 10 # seconds
46
-
47
+
47
48
  # Load a fix for inflections for words ending in ess
48
49
  ActiveSupport::Inflector.inflections do |inflect|
49
50
  inflect.singular(/ess$/i, 'ess')
50
51
  end
51
-
52
+
52
53
  def self.load_mocks_and_factories
53
54
  require 'hash_dealer'
54
55
  Mocks.clear_endpoints
55
56
  Mocks.init
56
-
57
- Dir["#{File.dirname(__FILE__)}/../spec/support/requests/*.rb"].each {|f|
57
+
58
+ Dir["#{File.dirname(__FILE__)}/../spec/support/requests/*.rb"].each {|f|
58
59
  require f
59
60
  }
60
- Dir["#{File.dirname(__FILE__)}/../spec/support/**/*.rb"].each {|f|
61
+ Dir["#{File.dirname(__FILE__)}/../spec/support/**/*.rb"].each {|f|
61
62
  require f
62
63
  }
63
64
  end
64
65
 
65
66
  class << self
66
-
67
- delegate :site, :site=, :format, :format=,
68
- :token, :token=, :timeout,
69
- :open_timeout,
67
+
68
+ delegate :site, :site=, :format, :format=,
69
+ :token, :token=, :timeout,
70
+ :open_timeout,
70
71
  :reset_connection, :ttl, :ttl=,
71
72
  :to => "ApiResource::Base"
72
-
73
+
73
74
  end
74
75
 
75
76
  def self.cache(reset = false)
76
77
  @cache = nil if reset
77
78
  @cache ||= begin
78
79
  defined?(Rails) ? Rails.cache : ActiveSupport::Cache::MemoryStore.new
79
- rescue
80
+ rescue
80
81
  ActiveSupport::Cache::MemoryStore.new
81
82
  end
82
83
  end
83
-
84
+
84
85
  # set the timeout val and reset the connection
85
86
  def self.timeout=(val)
86
87
  ApiResource::Base.timeout = val
@@ -116,7 +117,7 @@ module ApiResource
116
117
  self.ttl = old_ttl
117
118
  end
118
119
  end
119
-
120
+
120
121
  # logger
121
122
  def self.logger
122
123
  return @logger if @logger
@@ -125,11 +126,11 @@ module ApiResource
125
126
  @logger.level = Log4r::INFO
126
127
  @logger
127
128
  end
128
-
129
+
129
130
  # Use this method to enable logging in the future
130
131
  # def self.logging(val = nil)
131
132
  # return (@@logging || false) unless val
132
133
  # return @@logging = val
133
134
  # end
134
-
135
+
135
136
  end
@@ -2,7 +2,7 @@ require 'active_support'
2
2
  require 'active_support/string_inquirer'
3
3
 
4
4
  module ApiResource
5
-
5
+
6
6
  module Associations
7
7
 
8
8
  #TODO: many of these methods should also force loading of the resource definition
@@ -20,10 +20,10 @@ module ApiResource
20
20
  autoload :SingleObjectProxy
21
21
 
22
22
  included do
23
-
23
+
24
24
  unless self.ancestors.include?(ApiResource::AssociationActivation)
25
25
  raise Exception.new(
26
- "Can't include Associations without AssociationActivation"
26
+ "Can't include Associations without AssociationActivation"
27
27
  )
28
28
  end
29
29
 
@@ -42,7 +42,7 @@ module ApiResource
42
42
  extend InheritedMethod
43
43
 
44
44
  self.define_association_methods
45
-
45
+
46
46
  end
47
47
 
48
48
  # module methods to include the proper associations in various libraries - this is usually loaded in Railties
@@ -50,8 +50,8 @@ module ApiResource
50
50
  ActiveRecord::Base.class_eval do
51
51
  include ApiResource::AssociationActivation
52
52
  self.activate_associations(
53
- :has_many_remote => :has_many_remote,
54
- :belongs_to_remote => :belongs_to_remote,
53
+ :has_many_remote => :has_many_remote,
54
+ :belongs_to_remote => :belongs_to_remote,
55
55
  :has_one_remote => :has_one_remote,
56
56
  )
57
57
  end
@@ -71,7 +71,7 @@ module ApiResource
71
71
  end
72
72
 
73
73
  module ClassMethods
74
-
74
+
75
75
  # Define the methods for creating and testing for associations, unfortunately
76
76
  # scopes are different enough to require different methods :(
77
77
  def define_association_methods
@@ -82,7 +82,7 @@ module ApiResource
82
82
  options = options.with_indifferent_access
83
83
  # Raise an error if we have multiple args and options
84
84
  raise "Invalid arguments to #{assoc}" unless options.blank? || args.length == 1
85
- args.each do |arg|
85
+ args.each do |arg|
86
86
  klass_name = (options[:class_name] ? options[:class_name].to_s.classify : arg.to_s.classify)
87
87
  # add this to any descendants - the other methods etc are handled by inheritance
88
88
  ([self] + self.descendants).each do |klass|
@@ -93,15 +93,15 @@ module ApiResource
93
93
  define_association_as_attribute(:#{assoc}, arg, options)
94
94
  end
95
95
  end
96
-
96
+
97
97
  def #{assoc}?(name)
98
98
  return self.related_objects[:#{assoc}][name.to_s.pluralize.to_sym].present? || self.related_objects[:#{assoc}][name.to_s.singularize.to_sym].present?
99
99
  end
100
-
100
+
101
101
  def #{assoc}_class_name(name)
102
102
  raise "No such" + :#{assoc}.to_s + " association on #{name}" unless self.#{assoc}?(name)
103
103
  return self.find_namespaced_class_name(self.related_objects[:#{assoc}][name.to_sym])
104
- end
104
+ end
105
105
 
106
106
  EOE
107
107
  # For convenience we will define the methods for testing for the existence of an association
@@ -117,14 +117,14 @@ module ApiResource
117
117
  EOE
118
118
  end
119
119
  end
120
-
120
+
121
121
  def association?(assoc)
122
122
  self.related_objects.any? do |key, value|
123
123
  next if key.to_s == "scopes"
124
124
  value.detect { |k,v| k.to_sym == assoc.to_sym }
125
125
  end
126
126
  end
127
-
127
+
128
128
  def association_names
129
129
  # structure is {:has_many => {"myname" => "ClassName"}}
130
130
  self.related_objects.clone.delete_if{|k,v| k.to_s == "scopes"}.collect{|k,v| v.keys.collect(&:to_sym)}.flatten
@@ -133,7 +133,7 @@ module ApiResource
133
133
  def association_class(assoc)
134
134
  self.association_class_name(assoc).constantize
135
135
  end
136
-
136
+
137
137
  def association_class_name(assoc)
138
138
  raise ArgumentError, "#{assoc} is not a valid association of #{self}" unless self.association?(assoc)
139
139
  result = self.related_objects.detect do |key,value|
@@ -144,7 +144,7 @@ module ApiResource
144
144
 
145
145
  # TODO: add a special foreign_key option to associations
146
146
  def association_foreign_key_field(assoc, type = nil)
147
-
147
+
148
148
  if type.nil? && has_many?(assoc)
149
149
  type = :has_many
150
150
  else
@@ -160,7 +160,7 @@ module ApiResource
160
160
 
161
161
  str.to_sym
162
162
  end
163
-
163
+
164
164
  protected
165
165
 
166
166
  def clear_related_objects
@@ -184,6 +184,22 @@ module ApiResource
184
184
  self.related_objects[:scopes] = self.related_objects[:scopes].clone
185
185
  end
186
186
 
187
+ #
188
+ # Wrapper method to define all associations from the
189
+ # resource definition
190
+ #
191
+ # @return [Boolean] true
192
+ def define_all_associations
193
+ if self.resource_definition["associations"]
194
+ self.resource_definition["associations"].each_pair do |key, hash|
195
+ hash.each_pair do |assoc_name, assoc_options|
196
+ self.send(key, assoc_name, assoc_options)
197
+ end
198
+ end
199
+ end
200
+ true
201
+ end
202
+
187
203
  def define_association_as_attribute(assoc_type, assoc_name, opts)
188
204
  opts.stringify_keys!
189
205
 
@@ -198,7 +214,7 @@ module ApiResource
198
214
  define_attribute_method(assoc_name)
199
215
  define_attribute_method(id_method_name)
200
216
  end
201
-
217
+
202
218
  # a module to contain our generated methods
203
219
  cattr_accessor :api_resource_generated_methods
204
220
  self.api_resource_generated_methods = Module.new
@@ -216,7 +232,7 @@ module ApiResource
216
232
  )
217
233
  klass.define_association_as_attribute(self, assoc_name, opts)
218
234
  end
219
-
235
+
220
236
  def find_namespaced_class_name(klass)
221
237
  # return the name if it is itself namespaced
222
238
  return klass if klass =~ /::/
@@ -233,13 +249,13 @@ module ApiResource
233
249
 
234
250
  return klass
235
251
  end
236
-
252
+
237
253
  end
238
-
254
+
239
255
  def association?(assoc)
240
256
  self.class.association?(assoc)
241
257
  end
242
-
258
+
243
259
  def association_class(assoc)
244
260
  self.class.association_class(assoc)
245
261
  end
@@ -247,12 +263,12 @@ module ApiResource
247
263
  def association_class_name(assoc)
248
264
  self.class.association_class_name(assoc)
249
265
  end
250
-
266
+
251
267
  # list of all association names
252
268
  def association_names
253
269
  self.class.association_names
254
270
  end
255
271
 
256
272
  end
257
-
273
+
258
274
  end
@@ -1,22 +1,22 @@
1
1
  module ApiResource
2
-
2
+
3
3
  module Associations
4
-
4
+
5
5
  class AssociationProxy
6
6
 
7
7
 
8
8
  class_attribute :remote_path_element
9
- self.remote_path_element = :service_uri
9
+ self.remote_path_element = :service_uri
10
10
 
11
11
  attr_accessor :remote_path
12
12
  attr_reader :owner, :klass, :finder_opts
13
13
 
14
14
  # TODO: add the other load forcing methods here for collections
15
- delegate :[], :[]=, :<<, :first, :second, :last, :blank?, :nil?,
16
- :include?, :push, :pop, :+, :concat, :flatten, :flatten!, :compact,
17
- :compact!, :empty?, :fetch, :map, :reject, :reject!, :reverse,
18
- :select, :select!, :size, :sort, :sort!, :uniq, :uniq!, :to_a,
19
- :sample, :slice, :slice!, :count, :present?,
15
+ delegate :[], :[]=, :<<, :first, :second, :last, :blank?, :nil?,
16
+ :include?, :push, :pop, :+, :concat, :flatten, :flatten!, :compact,
17
+ :compact!, :empty?, :fetch, :map, :reject, :reject!, :reverse,
18
+ :select, :select!, :size, :sort, :sort!, :uniq, :uniq!, :to_a,
19
+ :sample, :slice, :slice!, :count, :present?,
20
20
  :to => :internal_object
21
21
 
22
22
  # define association methods on the class
@@ -27,7 +27,7 @@ module ApiResource
27
27
 
28
28
  # pass this along
29
29
  opts[:name] = assoc_name
30
-
30
+
31
31
  klass.api_resource_generated_methods.module_eval <<-EOE, __FILE__, __LINE__ + 1
32
32
  def #{assoc_name}
33
33
  @attributes_cache[:#{assoc_name}] ||= begin
@@ -60,7 +60,8 @@ module ApiResource
60
60
  #{id_method_name}_will_change!
61
61
  end
62
62
  @attributes_cache[:#{id_method_name}] = val
63
- write_attribute(:#{id_method_name}, val)
63
+ # write_attribute(:#{id_method_name}, val)
64
+ @attributes[:#{id_method_name}] = val
64
65
  end
65
66
 
66
67
  EOE
@@ -76,7 +77,7 @@ module ApiResource
76
77
  public
77
78
 
78
79
  def initialize(klass, owner, options = {})
79
-
80
+
80
81
  # the base class for our scope, e.g. ApiResource::SomeClass
81
82
  @klass = klass.is_a?(String) ? klass.constantize : klass
82
83
 
@@ -150,7 +151,7 @@ module ApiResource
150
151
  end
151
152
 
152
153
  end
153
-
154
+
154
155
  end
155
-
156
+
156
157
  end