storage_room 0.3.16 → 0.3.17

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,6 @@
1
+ == Version 0.3.17
2
+ * Fix ActiveSupport::Concern deprecation warnings for InstanceMethods
3
+
1
4
  == Version 0.3.16
2
5
  * Fix identity map problem on Windows (require 'set')
3
6
 
data/TODO CHANGED
@@ -1,3 +1,5 @@
1
+ - example with images
2
+ - add url to inspect for images
1
3
 
2
4
 
3
5
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.16
1
+ 0.3.17
@@ -9,172 +9,39 @@ module StorageRoom
9
9
  self.send(:define_model_callbacks, :initialize_from_response_data)
10
10
  end
11
11
 
12
- module InstanceMethods
13
- # Optionally pass attributes to set up the object
14
- def initialize(hash={})
15
- self.attributes = hash
16
- end
17
-
18
- # Shortcut to get an attribute.
19
- def [](name)
20
- self.response_data[name]
21
- end
22
-
23
- # Takes a response data hash, saves it in the record and initializes the class from the response data
24
- def set_from_response_data(hash)
25
- self.response_data = hash
26
- self.initialize_from_response_data
27
- self
28
- end
29
-
30
- # Return all of the objects attributes
31
- def response_data
32
- @_response_data ||= Hash.new.with_indifferent_access
33
- end
34
-
35
- # Set the objects attributes with a hash. Only attributes passed in the hash are changed, existing ones are not overridden.
36
- def response_data=(hash = {})
37
- response_data.merge!(hash)
38
- end
39
-
40
- # The attributes as they were defined with key, one, many
41
- def attributes
42
- @_attributes ||= Hash.new.with_indifferent_access
43
- end
44
-
45
- def attributes=(args = {})
46
- attributes.merge!(args)
47
- end
48
-
49
- def as_json(args = {}) # :nodoc:
50
- to_hash(args)
51
- end
52
-
53
- # ActiveSupport seemed to cause problems when just using as_json, so using to_hash
54
- def to_hash(args = {}) # :nodoc:
55
- args ||= {}
56
- hash = {}
57
-
58
- self.attributes.each do |name, value|
59
- hash[name] = if value.is_a?(::Array)
60
- value.map {|x| x.respond_to?(:to_hash) ? call_method_with_optional_parameters(x, :to_hash, :nested => true) : x}
61
- elsif value.respond_to?(:to_hash)
62
- call_method_with_optional_parameters(value, :to_hash, :nested => true)
63
- elsif value.respond_to?(:as_json)
64
- value.as_json(:nested => true)
65
- else
66
- value
67
- end
68
- end
69
-
70
- hash
71
- end
72
-
73
- def inspect # :nodoc:
74
- body = attributes.map{|k, v| "#{k}: #{attribute_for_inspect(v)}"}.join(', ')
75
- "#<#{self.class} #{body}>"
76
- end
77
-
78
- # Reset an object to its initial state with all attributes unset
79
- def reset!
80
- @_response_data = Hash.new.with_indifferent_access
81
- @_attributes = Hash.new.with_indifferent_access
82
- true
83
- end
84
-
85
- # Has a Resource been loaded from the API
86
- def loaded?
87
- self.response_data.present?
88
- end
89
-
90
- def proxy? # :nodoc:
91
- false
92
- end
93
-
94
- protected
95
- # Helper to not call to_hash with the wrong number of arguments
96
- def call_method_with_optional_parameters(object, method_name, parameters)
97
- if object.respond_to?(method_name)
98
- if object.method(method_name).arity == -1
99
- object.send(method_name, parameters)
100
- else
101
- object.send(method_name)
102
- end
103
- end
104
- end
105
-
106
- # Iterate over the response data and initialize the attributes
107
- def initialize_from_response_data # :nodoc:
108
- _run_initialize_from_response_data_callbacks do
109
- self.class.attribute_options_including_superclasses.each do |name, options|
110
- value = if options[:type] == :key
111
- self[name].blank? ? options[:default] : self[name]
112
- elsif options[:type] == :one
113
- hash = self[name.to_s]
114
- hash && hash['@type'] ? self.class.new_from_response_data(hash) : nil
115
- elsif options[:type] == :many
116
- response_data[name] && response_data[name].map{|hash| self.class.new_from_response_data(hash)} || []
117
- else
118
- raise "Invalid type: #{options[:type]}"
119
- end
120
-
121
- send("#{name}=", value)
122
- end
123
- end
124
- end
125
-
126
- def ensure_loaded # :nodoc:
127
- if loaded?
128
- yield if block_given?
129
- else
130
- raise StorageRoom::ResourceNotLoadedError
131
- end
132
- end
133
-
134
- def attribute_for_inspect(value) # :nodoc:
135
- if value.is_a?(String) && value.length > 50
136
- "#{value[0..50]}...".inspect
137
- elsif value.is_a?(Date) || value.is_a?(Time)
138
- %("#{value.iso8601}")
139
- else
140
- value.inspect
141
- end
142
- end
143
- end
144
-
145
12
  module ClassMethods
146
13
  # Is the passed hash of response data a real object or only an association with a URL but no actual data
147
14
  def response_data_is_association?(hash)
148
15
  hash.size == 2 && hash.include?('@type') && hash.include?('url')
149
16
  end
150
-
17
+
151
18
  # Load an object with the specified URL from the API
152
19
  def load(url, parameters = {})
153
20
  return nil if url.blank?
154
-
21
+
155
22
  StorageRoom.log("Loading #{url}")
156
23
  httparty = get(url, parameters)
157
-
24
+
158
25
  handle_critical_response_errors(httparty)
159
26
  new_from_response_data(httparty.parsed_response.first[1])
160
27
  end
161
-
28
+
162
29
  # Build an object out of a local file
163
30
  def new_from_json_file(path)
164
31
  json = ::File.read(path)
165
32
  new_from_json_string(json)
166
33
  end
167
-
34
+
168
35
  # Build an object out of the POST body
169
36
  def new_from_json_string(json_string)
170
37
  hash = JSON.parse(json_string)
171
38
  new_from_response_data(hash.first[1])
172
39
  end
173
-
40
+
174
41
  # Creates a new object of the correct class and initializes it from the response data
175
42
  def new_from_response_data(response_data)
176
43
  object = StorageRoom.class_for_name(response_data['@type']).new.set_from_response_data(response_data)
177
-
44
+
178
45
  if object.is_a?(Entry) && !object.loaded? && !object.proxy?
179
46
  StorageRoom.log("Return #{response_data['url']} proxied")
180
47
  Proxy.new(object)
@@ -182,13 +49,13 @@ module StorageRoom
182
49
  object
183
50
  end
184
51
  end
185
-
52
+
186
53
  # Defines a basic key for a Resource
187
54
  def key(name, options = {})
188
55
  options.merge!(:type => :key, :default => nil)
189
56
  define_attribute_methods(name, options)
190
57
  end
191
-
58
+
192
59
  # Defines a to-one association for a Resource (embedded or association)
193
60
  def one(name, options = {})
194
61
  options.merge!(:type => :one, :default => nil)
@@ -200,32 +67,168 @@ module StorageRoom
200
67
  options.merge!(:type => :many, :default => [])
201
68
  define_attribute_methods(name, options)
202
69
  end
203
-
70
+
204
71
  # Creates getter and setter for an attribute
205
72
  def define_attribute_methods(name, options) # :nodoc:
206
73
  define_method name do
207
74
  attributes[name] || options[:default]
208
75
  end
209
-
76
+
210
77
  define_method "#{name}=" do |object|
211
78
  attributes[name] = object
212
79
  end
213
-
80
+
214
81
  self.attribute_options[name] = options
215
82
  end
216
-
83
+
217
84
  def attribute_options
218
85
  @attribute_options ||= {}
219
86
  end
220
-
87
+
221
88
  def attribute_options_including_superclasses
222
89
  hash = attribute_options.dup
223
90
  hash.merge!(superclass.attribute_options_including_superclasses) if superclass.respond_to?(:attribute_options_including_superclasses)
224
-
91
+
225
92
  hash
226
93
  end
94
+ end# ==========
95
+ # = Banner =
96
+ # ==========
97
+
98
+
99
+ # Optionally pass attributes to set up the object
100
+ def initialize(hash={})
101
+ self.attributes = hash
102
+ end
103
+
104
+ # Shortcut to get an attribute.
105
+ def [](name)
106
+ self.response_data[name]
107
+ end
108
+
109
+ # Takes a response data hash, saves it in the record and initializes the class from the response data
110
+ def set_from_response_data(hash)
111
+ self.response_data = hash
112
+ self.initialize_from_response_data
113
+ self
114
+ end
115
+
116
+ # Return all of the objects attributes
117
+ def response_data
118
+ @_response_data ||= Hash.new.with_indifferent_access
119
+ end
120
+
121
+ # Set the objects attributes with a hash. Only attributes passed in the hash are changed, existing ones are not overridden.
122
+ def response_data=(hash = {})
123
+ response_data.merge!(hash)
124
+ end
125
+
126
+ # The attributes as they were defined with key, one, many
127
+ def attributes
128
+ @_attributes ||= Hash.new.with_indifferent_access
129
+ end
130
+
131
+ def attributes=(args = {})
132
+ attributes.merge!(args)
133
+ end
134
+
135
+ def as_json(args = {}) # :nodoc:
136
+ to_hash(args)
137
+ end
138
+
139
+ # ActiveSupport seemed to cause problems when just using as_json, so using to_hash
140
+ def to_hash(args = {}) # :nodoc:
141
+ args ||= {}
142
+ hash = {}
143
+
144
+ self.attributes.each do |name, value|
145
+ hash[name] = if value.is_a?(::Array)
146
+ value.map {|x| x.respond_to?(:to_hash) ? call_method_with_optional_parameters(x, :to_hash, :nested => true) : x}
147
+ elsif value.respond_to?(:to_hash)
148
+ call_method_with_optional_parameters(value, :to_hash, :nested => true)
149
+ elsif value.respond_to?(:as_json)
150
+ value.as_json(:nested => true)
151
+ else
152
+ value
153
+ end
154
+ end
155
+
156
+ hash
227
157
  end
158
+
159
+ def inspect # :nodoc:
160
+ body = attributes.map{|k, v| "#{k}: #{attribute_for_inspect(v)}"}.join(', ')
161
+ "#<#{self.class} #{body}>"
162
+ end
163
+
164
+ # Reset an object to its initial state with all attributes unset
165
+ def reset!
166
+ @_response_data = Hash.new.with_indifferent_access
167
+ @_attributes = Hash.new.with_indifferent_access
168
+ true
169
+ end
170
+
171
+ # Has a Resource been loaded from the API
172
+ def loaded?
173
+ self.response_data.present?
174
+ end
175
+
176
+ def proxy? # :nodoc:
177
+ false
178
+ end
179
+
180
+ protected
181
+ # Helper to not call to_hash with the wrong number of arguments
182
+ def call_method_with_optional_parameters(object, method_name, parameters)
183
+ if object.respond_to?(method_name)
184
+ if object.method(method_name).arity == -1
185
+ object.send(method_name, parameters)
186
+ else
187
+ object.send(method_name)
188
+ end
189
+ end
190
+ end
228
191
 
192
+ # Iterate over the response data and initialize the attributes
193
+ def initialize_from_response_data # :nodoc:
194
+ _run_initialize_from_response_data_callbacks do
195
+ self.class.attribute_options_including_superclasses.each do |name, options|
196
+ value = if options[:type] == :key
197
+ self[name].blank? ? options[:default] : self[name]
198
+ elsif options[:type] == :one
199
+ hash = self[name.to_s]
200
+ hash && hash['@type'] ? self.class.new_from_response_data(hash) : nil
201
+ elsif options[:type] == :many
202
+ response_data[name] && response_data[name].map{|hash| self.class.new_from_response_data(hash)} || []
203
+ else
204
+ raise "Invalid type: #{options[:type]}"
205
+ end
206
+
207
+ send("#{name}=", value)
208
+ end
209
+ end
210
+ end
211
+
212
+ def ensure_loaded # :nodoc:
213
+ if loaded?
214
+ yield if block_given?
215
+ else
216
+ raise StorageRoom::ResourceNotLoadedError
217
+ end
218
+ end
219
+
220
+ def attribute_for_inspect(value) # :nodoc:
221
+ if value.is_a?(String) && value.length > 50
222
+ "#{value[0..50]}...".inspect
223
+ elsif value.is_a?(Date) || value.is_a?(Time)
224
+ %("#{value.iso8601}")
225
+ else
226
+ value.inspect
227
+ end
228
+ end
229
229
  end
230
230
 
231
+
232
+
233
+
231
234
  end
@@ -93,29 +93,29 @@ module StorageRoom
93
93
 
94
94
  end
95
95
 
96
- module InstanceMethods
97
- def identity_map
98
- self.class.identity_map
99
- end
100
-
101
- def in_identity_map?
102
- return false if self[:@url].blank?
103
- identity_map.include?(self[:@url])
104
- end
105
-
106
- def create(*args)
107
- if result = super
108
- identity_map[self[:@url]] = self if self.class.identity_map_on?
109
- end
110
- result
96
+
97
+ def identity_map
98
+ self.class.identity_map
99
+ end
100
+
101
+ def in_identity_map?
102
+ return false if self[:@url].blank?
103
+ identity_map.include?(self[:@url])
104
+ end
105
+
106
+ def create(*args)
107
+ if result = super
108
+ identity_map[self[:@url]] = self if self.class.identity_map_on?
111
109
  end
112
-
113
- def destroy
114
- identity_map.delete(self[:@url]) if self.class.identity_map_on?
115
- super
116
- end
117
-
110
+ result
118
111
  end
112
+
113
+ def destroy
114
+ identity_map.delete(self[:@url]) if self.class.identity_map_on?
115
+ super
116
+ end
117
+
118
+
119
119
  end
120
120
  end
121
121
 
data/storage_room.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "storage_room"
8
- s.version = "0.3.16"
8
+ s.version = "0.3.17"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sascha Konietzke"]
12
- s.date = "2011-11-21"
12
+ s.date = "2012-01-25"
13
13
  s.description = "StorageRoom is a CMS system for Mobile Applications (iPhone, Android, BlackBerry, ...). This library gives you an ActiveModel-like interface to your data."
14
14
  s.email = "sascha@thriventures.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,94 +1,92 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: storage_room
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.17
4
5
  prerelease:
5
- version: 0.3.16
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Sascha Konietzke
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-11-21 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70350538804900 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
23
21
  version: 1.2.9
24
22
  type: :development
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: webmock
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70350538804900
25
+ - !ruby/object:Gem::Dependency
26
+ name: webmock
27
+ requirement: &70350538804400 !ruby/object:Gem::Requirement
30
28
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
35
33
  type: :development
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: httparty
39
34
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70350538804400
36
+ - !ruby/object:Gem::Dependency
37
+ name: httparty
38
+ requirement: &70350538803920 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
45
43
  version: 0.6.1
46
44
  type: :runtime
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: activesupport
50
45
  prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *70350538803920
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: &70350538803400 !ruby/object:Gem::Requirement
52
50
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
56
54
  version: 3.0.0
57
55
  type: :runtime
58
- version_requirements: *id004
59
- - !ruby/object:Gem::Dependency
60
- name: activemodel
61
56
  prerelease: false
62
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *70350538803400
58
+ - !ruby/object:Gem::Dependency
59
+ name: activemodel
60
+ requirement: &70350538802880 !ruby/object:Gem::Requirement
63
61
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
67
65
  version: 3.0.0
68
66
  type: :runtime
69
- version_requirements: *id005
70
- - !ruby/object:Gem::Dependency
71
- name: mime-types
72
67
  prerelease: false
73
- requirement: &id006 !ruby/object:Gem::Requirement
68
+ version_requirements: *70350538802880
69
+ - !ruby/object:Gem::Dependency
70
+ name: mime-types
71
+ requirement: &70350538802400 !ruby/object:Gem::Requirement
74
72
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
79
77
  type: :runtime
80
- version_requirements: *id006
81
- description: StorageRoom is a CMS system for Mobile Applications (iPhone, Android, BlackBerry, ...). This library gives you an ActiveModel-like interface to your data.
78
+ prerelease: false
79
+ version_requirements: *70350538802400
80
+ description: StorageRoom is a CMS system for Mobile Applications (iPhone, Android,
81
+ BlackBerry, ...). This library gives you an ActiveModel-like interface to your data.
82
82
  email: sascha@thriventures.com
83
83
  executables: []
84
-
85
84
  extensions: []
86
-
87
- extra_rdoc_files:
85
+ extra_rdoc_files:
88
86
  - LICENSE
89
87
  - README.rdoc
90
88
  - TODO
91
- files:
89
+ files:
92
90
  - History.txt
93
91
  - LICENSE
94
92
  - README.rdoc
@@ -195,30 +193,26 @@ files:
195
193
  - tasks/storage_room.rake
196
194
  homepage: http://github.com/thriventures/storage_room_gem
197
195
  licenses: []
198
-
199
196
  post_install_message:
200
197
  rdoc_options: []
201
-
202
- require_paths:
198
+ require_paths:
203
199
  - lib
204
- required_ruby_version: !ruby/object:Gem::Requirement
200
+ required_ruby_version: !ruby/object:Gem::Requirement
205
201
  none: false
206
- requirements:
207
- - - ">="
208
- - !ruby/object:Gem::Version
209
- version: "0"
210
- required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
207
  none: false
212
- requirements:
213
- - - ">="
214
- - !ruby/object:Gem::Version
215
- version: "0"
208
+ requirements:
209
+ - - ! '>='
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
216
212
  requirements: []
217
-
218
213
  rubyforge_project:
219
214
  rubygems_version: 1.8.10
220
215
  signing_key:
221
216
  specification_version: 3
222
217
  summary: StorageRoom API Wrapper (ActiveModel style)
223
218
  test_files: []
224
-