api_resource 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +37 -0
- data/Gemfile.lock +190 -0
- data/Guardfile +27 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/api_resource.gemspec +111 -0
- data/coverage/assets/0.5.3/app.js +88 -0
- data/coverage/assets/0.5.3/fancybox/blank.gif +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_close.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_loading.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_nav_left.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_nav_right.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_e.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_n.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_ne.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_nw.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_s.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_se.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_sw.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_w.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_left.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_main.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_over.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_right.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox-x.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox-y.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox.png +0 -0
- data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.css +363 -0
- data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.pack.js +44 -0
- data/coverage/assets/0.5.3/favicon_green.png +0 -0
- data/coverage/assets/0.5.3/favicon_red.png +0 -0
- data/coverage/assets/0.5.3/favicon_yellow.png +0 -0
- data/coverage/assets/0.5.3/highlight.css +129 -0
- data/coverage/assets/0.5.3/highlight.pack.js +1 -0
- data/coverage/assets/0.5.3/jquery-1.6.2.min.js +18 -0
- data/coverage/assets/0.5.3/jquery.dataTables.min.js +152 -0
- data/coverage/assets/0.5.3/jquery.timeago.js +141 -0
- data/coverage/assets/0.5.3/jquery.url.js +174 -0
- data/coverage/assets/0.5.3/loading.gif +0 -0
- data/coverage/assets/0.5.3/magnify.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/jquery-ui-1.8.4.custom.css +295 -0
- data/coverage/assets/0.5.3/stylesheet.css +383 -0
- data/coverage/index.html +3573 -0
- data/lib/api_resource.rb +130 -0
- data/lib/api_resource/association_activation.rb +19 -0
- data/lib/api_resource/associations.rb +218 -0
- data/lib/api_resource/associations/association_proxy.rb +116 -0
- data/lib/api_resource/associations/belongs_to_remote_object_proxy.rb +16 -0
- data/lib/api_resource/associations/dynamic_resource_scope.rb +23 -0
- data/lib/api_resource/associations/generic_scope.rb +68 -0
- data/lib/api_resource/associations/has_many_remote_object_proxy.rb +16 -0
- data/lib/api_resource/associations/has_many_through_remote_object_proxy.rb +13 -0
- data/lib/api_resource/associations/has_one_remote_object_proxy.rb +24 -0
- data/lib/api_resource/associations/multi_argument_resource_scope.rb +15 -0
- data/lib/api_resource/associations/multi_object_proxy.rb +84 -0
- data/lib/api_resource/associations/related_object_hash.rb +12 -0
- data/lib/api_resource/associations/relation_scope.rb +25 -0
- data/lib/api_resource/associations/resource_scope.rb +32 -0
- data/lib/api_resource/associations/scope.rb +132 -0
- data/lib/api_resource/associations/single_object_proxy.rb +82 -0
- data/lib/api_resource/attributes.rb +243 -0
- data/lib/api_resource/base.rb +717 -0
- data/lib/api_resource/callbacks.rb +45 -0
- data/lib/api_resource/connection.rb +195 -0
- data/lib/api_resource/core_extensions.rb +7 -0
- data/lib/api_resource/custom_methods.rb +117 -0
- data/lib/api_resource/decorators.rb +6 -0
- data/lib/api_resource/decorators/caching_decorator.rb +20 -0
- data/lib/api_resource/exceptions.rb +99 -0
- data/lib/api_resource/formats.rb +22 -0
- data/lib/api_resource/formats/json_format.rb +25 -0
- data/lib/api_resource/formats/xml_format.rb +36 -0
- data/lib/api_resource/local.rb +12 -0
- data/lib/api_resource/log_subscriber.rb +15 -0
- data/lib/api_resource/mocks.rb +285 -0
- data/lib/api_resource/model_errors.rb +82 -0
- data/lib/api_resource/observing.rb +27 -0
- data/lib/api_resource/railtie.rb +24 -0
- data/lib/api_resource/scopes.rb +48 -0
- data/nohup.out +63 -0
- data/spec/lib/api_resource_spec.rb +43 -0
- data/spec/lib/associations_spec.rb +751 -0
- data/spec/lib/attributes_spec.rb +191 -0
- data/spec/lib/base_spec.rb +655 -0
- data/spec/lib/callbacks_spec.rb +68 -0
- data/spec/lib/connection_spec.rb +137 -0
- data/spec/lib/local_spec.rb +20 -0
- data/spec/lib/mocks_spec.rb +74 -0
- data/spec/lib/model_errors_spec.rb +29 -0
- data/spec/lib/prefixes_spec.rb +107 -0
- data/spec/spec_helper.rb +82 -0
- data/spec/support/mocks/association_mocks.rb +63 -0
- data/spec/support/mocks/error_resource_mocks.rb +21 -0
- data/spec/support/mocks/prefix_model_mocks.rb +5 -0
- data/spec/support/mocks/test_resource_mocks.rb +44 -0
- data/spec/support/requests/association_requests.rb +31 -0
- data/spec/support/requests/error_resource_requests.rb +25 -0
- data/spec/support/requests/prefix_model_requests.rb +7 -0
- data/spec/support/requests/test_resource_requests.rb +38 -0
- data/spec/support/test_resource.rb +72 -0
- data/spec/tmp/DIR +0 -0
- data/spec/tmp/api_resource_test_db.sqlite +0 -0
- metadata +119 -3
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'api_resource/associations/single_object_proxy'
|
2
|
+
module ApiResource
|
3
|
+
module Associations
|
4
|
+
class BelongsToRemoteObjectProxy < SingleObjectProxy
|
5
|
+
def initialize(klass_name, contents, owner)
|
6
|
+
super
|
7
|
+
return if self.internal_object
|
8
|
+
# now if we have an owner and a foreign key, we set the data up to load
|
9
|
+
if owner && key = owner.send(self.klass.to_s.foreign_key)
|
10
|
+
self.load({"service_uri" => self.klass.element_path(key), "scopes_only" => true}.merge(self.klass.scopes))
|
11
|
+
end
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'api_resource/associations/resource_scope'
|
2
|
+
|
3
|
+
module ApiResource
|
4
|
+
module Associations
|
5
|
+
class DynamicResourceScope < ResourceScope
|
6
|
+
|
7
|
+
attr_accessor :dynamic_value
|
8
|
+
# initializer - set up the dynamic value
|
9
|
+
def initialize(klass, current_scope, dynamic_value, opts = {})
|
10
|
+
self.dynamic_value = dynamic_value
|
11
|
+
super(klass, current_scope, opts)
|
12
|
+
end
|
13
|
+
# get the to_query value for this resource scope
|
14
|
+
def to_hash
|
15
|
+
hsh = self.scopes[self.current_scope].clone
|
16
|
+
hsh.each_pair do |k,v|
|
17
|
+
hsh[k] = self.dynamic_value
|
18
|
+
end
|
19
|
+
self.parent_hash.merge(hsh)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'api_resource/associations/resource_scope'
|
2
|
+
|
3
|
+
module ApiResource
|
4
|
+
module Associations
|
5
|
+
|
6
|
+
class GenericScope < ResourceScope
|
7
|
+
attr_reader :name
|
8
|
+
attr_reader :params
|
9
|
+
attr_reader :types
|
10
|
+
attr_reader :values
|
11
|
+
|
12
|
+
# Gets called when a scope is called. Stores everything in
|
13
|
+
# this class. Sorry, couldn't be bothered to figure out the
|
14
|
+
# class hierarchy.
|
15
|
+
#
|
16
|
+
# klass - ApiResourceBase class
|
17
|
+
# current_scope - sym for the scope
|
18
|
+
# *args - arguments being passed to the scope
|
19
|
+
def initialize(klass, current_scope, *args)
|
20
|
+
|
21
|
+
@name = current_scope # contains sym with scope ie :for_provider
|
22
|
+
@params = klass.related_objects[:scopes][current_scope].keys
|
23
|
+
@types = klass.related_objects[:scopes][current_scope].values
|
24
|
+
|
25
|
+
# Bail if we have crap
|
26
|
+
if @params == nil
|
27
|
+
raise "Scope #{@name} does not exist #{klass.name}. Scopes: #{klass.related_objects[:scopes]}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# extract parent scope stuff
|
31
|
+
opts = {}
|
32
|
+
last_arg = args[args.count - 1]
|
33
|
+
if last_arg != nil && last_arg.is_a?(Hash) && last_arg[:parent] != nil
|
34
|
+
args = args.slice(0, args.count - 1)
|
35
|
+
opts = last_arg
|
36
|
+
end
|
37
|
+
|
38
|
+
# walk through parameters and types and assign values from *args to parameters
|
39
|
+
@values = []
|
40
|
+
@params.count.times do |i|
|
41
|
+
if @types[i] == :rest
|
42
|
+
@values << args.slice(i, args.count)
|
43
|
+
else
|
44
|
+
@values << args[i]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Let the parent class do its magic.
|
49
|
+
super(klass, current_scope, opts)
|
50
|
+
end
|
51
|
+
|
52
|
+
# get the to_query value for this resource scope
|
53
|
+
def to_hash
|
54
|
+
# debugger
|
55
|
+
if @params.count == 0
|
56
|
+
scope_arguments = true
|
57
|
+
else
|
58
|
+
scope_arguments = {}
|
59
|
+
@params.count.times do |i|
|
60
|
+
scope_arguments[@params[i]] = @values[i] if @values[i] != nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
self.parent_hash.merge({@name => scope_arguments})
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'api_resource/associations/multi_object_proxy'
|
2
|
+
module ApiResource
|
3
|
+
module Associations
|
4
|
+
class HasManyRemoteObjectProxy < MultiObjectProxy
|
5
|
+
def initialize(klass_name, contents, owner)
|
6
|
+
super
|
7
|
+
return if self.internal_object.present? || self.remote_path
|
8
|
+
# now if we have an owner and a foreign key, we set the data up to load
|
9
|
+
if owner
|
10
|
+
self.load({"service_uri" => self.klass.collection_path(self.owner.class.to_s.foreign_key => self.owner.id)}.merge(self.klass.scopes))
|
11
|
+
end
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ApiResource
|
2
|
+
module Associations
|
3
|
+
module HasManyThroughRemoteObjectProxy
|
4
|
+
def has_many_through_remote(association, options)
|
5
|
+
self.instance_eval do
|
6
|
+
send(:define_method, association) do
|
7
|
+
send(options[:through]).collect{ |t| t.send(association.to_s.singularize) }.flatten
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'api_resource/associations/single_object_proxy'
|
2
|
+
module ApiResource
|
3
|
+
module Associations
|
4
|
+
class HasOneRemoteObjectProxy < SingleObjectProxy
|
5
|
+
def initialize(klass_name, contents, owner)
|
6
|
+
super
|
7
|
+
return if self.internal_object
|
8
|
+
# now if we have an owner and a foreign key, we set the data up to load
|
9
|
+
if owner
|
10
|
+
self.load({"service_uri" => self.klass.collection_path(self.owner.class.to_s.foreign_key => self.owner.id)}.merge(self.klass.scopes))
|
11
|
+
end
|
12
|
+
true
|
13
|
+
end
|
14
|
+
protected
|
15
|
+
# load data from the remote server
|
16
|
+
# In a has_one, we can get back an Array, so we use the first element
|
17
|
+
def load_from_remote(options)
|
18
|
+
data = super(options)
|
19
|
+
data = data.first if data.is_a?(Array)
|
20
|
+
data
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'api_resource/associations/dynamic_resource_scope'
|
2
|
+
|
3
|
+
module ApiResource
|
4
|
+
module Associations
|
5
|
+
class MultiArgumentResourceScope < DynamicResourceScope
|
6
|
+
# initialize with a variable number of dynamic arguments
|
7
|
+
def initialize(klass, current_scope, *dynamic_value)
|
8
|
+
# pull off opts
|
9
|
+
opts = dynamic_value.extract_options!
|
10
|
+
# we always dynamic value to be an Array, so we don't use the splat here
|
11
|
+
super(klass, current_scope, dynamic_value.flatten, opts)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'api_resource/associations/association_proxy'
|
2
|
+
|
3
|
+
module ApiResource
|
4
|
+
|
5
|
+
module Associations
|
6
|
+
|
7
|
+
class MultiObjectProxy < AssociationProxy
|
8
|
+
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
def all
|
12
|
+
self.internal_object
|
13
|
+
end
|
14
|
+
|
15
|
+
def each(*args, &block)
|
16
|
+
self.internal_object.each(*args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(other)
|
20
|
+
return false if self.class != other.class
|
21
|
+
if self.internal_object.is_a?(Array)
|
22
|
+
self.internal_object.sort.each_with_index do |elem, i|
|
23
|
+
return false if other.internal_object.sort[i].attributes != elem.attributes
|
24
|
+
end
|
25
|
+
end
|
26
|
+
return true
|
27
|
+
end
|
28
|
+
|
29
|
+
def serializable_hash(options)
|
30
|
+
self.internal_object.collect{|obj| obj.serializable_hash(options) }
|
31
|
+
end
|
32
|
+
|
33
|
+
# force a load when calling this method
|
34
|
+
def internal_object
|
35
|
+
@internal_object ||= self.load_scope_with_options(:all, {})
|
36
|
+
end
|
37
|
+
|
38
|
+
def internal_object=(contents)
|
39
|
+
return @internal_object = contents if contents.all?{|o| o.is_a?(self.klass)}
|
40
|
+
return load(contents)
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
def load_scope_with_options(scope, options)
|
45
|
+
scope = self.loaded_hash_key(scope.to_s, options)
|
46
|
+
return [] if self.remote_path.blank?
|
47
|
+
|
48
|
+
self.loaded[scope] ||= begin
|
49
|
+
self.times_loaded += 1
|
50
|
+
self.load_from_remote(options).collect{|item| self.klass.new(item)}
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def load(contents)
|
56
|
+
@internal_object = [] and return nil if contents.blank?
|
57
|
+
if contents.is_a?(Array) && contents.first.is_a?(Hash) && contents.first[self.class.remote_path_element]
|
58
|
+
settings = contents.slice!(0).with_indifferent_access
|
59
|
+
end
|
60
|
+
|
61
|
+
settings = contents.with_indifferent_access if contents.is_a?(Hash)
|
62
|
+
settings ||= {}.with_indifferent_access
|
63
|
+
|
64
|
+
raise "Invalid response for multi object relationship: #{contents}" unless settings[self.class.remote_path_element] || contents.is_a?(Array)
|
65
|
+
self.remote_path = settings.delete(self.class.remote_path_element)
|
66
|
+
|
67
|
+
settings.each do |key, value|
|
68
|
+
raise "Expected the scope #{key} to point to a hash, to #{value}" unless value.is_a?(Hash)
|
69
|
+
self.instance_eval <<-EOE, __FILE__, __LINE__ + 1
|
70
|
+
def #{key}(opts = {})
|
71
|
+
@#{key} ||= ApiResource::Associations::RelationScope.new(self, :#{key}, opts)
|
72
|
+
end
|
73
|
+
EOE
|
74
|
+
self.scopes[key.to_s] = value
|
75
|
+
end
|
76
|
+
|
77
|
+
# Create the internal object
|
78
|
+
@internal_object = contents.is_a?(Array) ? contents.collect{|item| self.klass.new(item)} : nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ApiResource
|
2
|
+
module Associations
|
3
|
+
# RelatedObjectHash, re-defines dup to be recursive
|
4
|
+
class RelatedObjectHash < HashWithIndifferentAccess
|
5
|
+
def dup
|
6
|
+
Marshal.load(Marshal.dump(self))
|
7
|
+
end
|
8
|
+
# use this behavior for clone too
|
9
|
+
alias_method :clone, :dup
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'api_resource/associations/scope'
|
2
|
+
|
3
|
+
module ApiResource
|
4
|
+
|
5
|
+
module Associations
|
6
|
+
|
7
|
+
class RelationScope < Scope
|
8
|
+
|
9
|
+
# Use this method to access the internal data, this guarantees that loading only occurs once per object
|
10
|
+
def internal_object
|
11
|
+
ApiResource.with_ttl(ttl) do
|
12
|
+
@internal_object ||= self.klass.send(:load_scope_with_options, self.current_scope, self.to_hash)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
#
|
16
|
+
# class factory
|
17
|
+
def self.class_factory(hsh)
|
18
|
+
ApiResource::Associations::RelationScope
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'api_resource/associations/scope'
|
2
|
+
|
3
|
+
module ApiResource
|
4
|
+
|
5
|
+
module Associations
|
6
|
+
|
7
|
+
class ResourceScope < Scope
|
8
|
+
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
def internal_object
|
12
|
+
ApiResource.with_ttl(ttl) do
|
13
|
+
@internal_object ||= self.klass.send(:find, :all, :params => self.to_hash)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :all, :internal_object
|
18
|
+
|
19
|
+
def each(*args, &block)
|
20
|
+
self.internal_object.each(*args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Used by ApiResource::Scopes to create methods with the same name
|
24
|
+
# as the scope
|
25
|
+
#
|
26
|
+
# Weird place to have a factory... could have been on Scope or a separate class...
|
27
|
+
def self.class_factory(hsh)
|
28
|
+
return ApiResource::Associations::GenericScope
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module ApiResource
|
2
|
+
|
3
|
+
module Associations
|
4
|
+
|
5
|
+
class Scope
|
6
|
+
|
7
|
+
attr_accessor :klass, :current_scope, :internal_object
|
8
|
+
|
9
|
+
attr_reader :scopes
|
10
|
+
|
11
|
+
def initialize(klass, current_scope, opts = {})
|
12
|
+
# Holds onto the association proxy this RelationScope is bound to
|
13
|
+
@klass = klass
|
14
|
+
@parent = opts.delete(:parent)
|
15
|
+
@ttl = opts.delete(:expires_in)
|
16
|
+
# splits on _and_ and sorts to get a consistent scope key order
|
17
|
+
@current_scope = (self.parent_scope + Array.wrap(current_scope.to_s)).sort
|
18
|
+
# define methods for the scopes of the object
|
19
|
+
|
20
|
+
klass.scopes.each do |key, val|
|
21
|
+
self.instance_eval <<-EOE, __FILE__, __LINE__ + 1
|
22
|
+
# This class always has at least one scope, adding a new one should clone this object
|
23
|
+
def #{key}(*args)
|
24
|
+
obj = self.clone
|
25
|
+
# Call reload to make it go back to the webserver the next time it loads
|
26
|
+
obj.reload
|
27
|
+
return obj.enhance_current_scope(:#{key}, *args)
|
28
|
+
end
|
29
|
+
EOE
|
30
|
+
self.scopes[key.to_s] = val
|
31
|
+
end
|
32
|
+
# Use the method current scope because it gives a string
|
33
|
+
# This expression substitutes the options from opts into the default attributes of the scope, it will only copy keys that exist in the original
|
34
|
+
self.scopes[self.current_scope] = opts.inject(self.scopes[current_scope]){|accum,(k,v)| accum.key?(k.to_s) ? accum.merge(k.to_s => v) : accum}
|
35
|
+
end
|
36
|
+
|
37
|
+
def ttl
|
38
|
+
@ttl || 0
|
39
|
+
end
|
40
|
+
|
41
|
+
# Use this method to access the internal data, this guarantees that loading only occurs once per object
|
42
|
+
def internal_object
|
43
|
+
raise "Not Implemented: This method must be implemented in a subclass"
|
44
|
+
end
|
45
|
+
|
46
|
+
def scopes
|
47
|
+
@scopes ||= {}.with_indifferent_access
|
48
|
+
end
|
49
|
+
|
50
|
+
def scope?(scp)
|
51
|
+
self.scopes.key?(scp.to_s)
|
52
|
+
end
|
53
|
+
|
54
|
+
def current_scope
|
55
|
+
ActiveSupport::StringInquirer.new(@current_scope.join("_and_").concat("_scope"))
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_hash
|
59
|
+
self.parent_hash.merge(self.scopes[self.current_scope])
|
60
|
+
end
|
61
|
+
|
62
|
+
# takes empty hashes and replaces them with true so that to_query doesn't strip them out
|
63
|
+
def to_query_safe_hash(hash)
|
64
|
+
hash.each_pair do |k, v|
|
65
|
+
hash[k] = to_query_safe_hash(v) if v.is_a?(Hash)
|
66
|
+
hash[k] = true if v == {}
|
67
|
+
end
|
68
|
+
return hash
|
69
|
+
end
|
70
|
+
|
71
|
+
# gets the current hash and calls to_query on it
|
72
|
+
def to_query
|
73
|
+
#We need to add the unescape because to_query breaks on nested arrays
|
74
|
+
CGI.unescape(to_query_safe_hash(self.to_hash).to_query)
|
75
|
+
end
|
76
|
+
|
77
|
+
def method_missing(method, *args, &block)
|
78
|
+
self.internal_object.send(method, *args, &block)
|
79
|
+
end
|
80
|
+
|
81
|
+
def reload
|
82
|
+
remove_instance_variable(:@internal_object) if instance_variable_defined?(:@internal_object)
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_s
|
87
|
+
self.internal_object.to_s
|
88
|
+
end
|
89
|
+
|
90
|
+
def inspect
|
91
|
+
self.internal_object.inspect
|
92
|
+
end
|
93
|
+
|
94
|
+
def blank?
|
95
|
+
self.internal_object.blank?
|
96
|
+
end
|
97
|
+
alias_method :empty?, :blank?
|
98
|
+
|
99
|
+
def present?
|
100
|
+
self.internal_object.present?
|
101
|
+
end
|
102
|
+
|
103
|
+
def expires_in(ttl)
|
104
|
+
ApiResource::Decorators::CachingDecorator.new(self, ttl)
|
105
|
+
end
|
106
|
+
|
107
|
+
protected
|
108
|
+
# scope from the parent
|
109
|
+
def parent_scope
|
110
|
+
ret = @parent ? Array.wrap(@parent.current_scope).collect{|el| el.gsub(/_scope$/,'')} : []
|
111
|
+
ret.collect{|el| el.split(/_and_/)}.flatten
|
112
|
+
end
|
113
|
+
# querystring hash from parent
|
114
|
+
def parent_hash
|
115
|
+
@parent ? @parent.to_hash : {}
|
116
|
+
end
|
117
|
+
def enhance_current_scope(scp, *args)
|
118
|
+
opts = args.extract_options!
|
119
|
+
check_scope(scp)
|
120
|
+
cache_key = "a#{Digest::MD5.hexdigest((args.sort + [scp]).to_s)}"
|
121
|
+
return instance_variable_get("@#{cache_key}") if instance_variable_defined?("@#{cache_key}")
|
122
|
+
return instance_variable_set("@#{cache_key}", self.class.class_factory(self.scopes[scp]).new(self.klass, scp, *args, opts.merge(:parent => self)))
|
123
|
+
end
|
124
|
+
# make sure we have a valid scope
|
125
|
+
def check_scope(scp)
|
126
|
+
raise ArgumentError, "Unknown scope #{scp}" unless self.scope?(scp.to_s)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|