riakrest 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,30 +24,20 @@ module RiakRest
24
24
  #
25
25
  # Since Jiak interaction is JSON, duplicate fields names within an array are
26
26
  # not meaningful, including a symbol that "equals" a string. Duplicates
27
- # raise an exception.
27
+ # fields are ignored.
28
28
  #
29
29
  # ===Usage
30
30
  # <pre>
31
- # schema = JiakSchema.new({:allowed_fields => [:foo,:bar,:baz],
32
- # :required_fields => [:foo,:bar],
33
- # :read_mask => [:foo,:baz],
34
- # :write_mask => [:foo,:bar] })
31
+ # schema = JiakSchema.new([:foo,:bar])
32
+ # schema.allowed_fields # => [:foo,:bar]
33
+ # schema.required_fields # => []
34
+ # schema.read_mask # => [:foo,:bar]
35
+ # schema.write_mask # => [:foo,:bar]
35
36
  #
36
- # schema.required_fields # => [:foo,:bar]
37
- #
38
- #
39
- # schema = JiakSchema.new({:allowed_fields => [:foo,:bar,:baz],
40
- # :required_fields => [:foo,:bar]})
41
- #
42
- # schema.read_mask # => [:foo,:bar,:baz]
43
- # schema.required_fields # => [:foo,:bar]
44
- # schema.required_fields = [:foo,:bar,:baz]
45
- # schema.required_fields # => [:foo,:bar,:baz]
46
- #
47
- #
48
- # schema = JiakSchema.new([:foo,:bar,:baz])
49
- #
50
- # schema.write_mask # => [:foo,:bar,:baz)
37
+ # schema.write :baz
38
+ # schema.allowed_fields # => [:foo,:bar,:baz]
39
+ # schema.read_mask # => [:foo,:bar]
40
+ # schema.write_mask # => [:foo,:bar,:baz]
51
41
  #
52
42
  # </pre>
53
43
  class JiakSchema
@@ -60,16 +50,13 @@ module RiakRest
60
50
  # New schema from either a hash or an single-element array.
61
51
  #
62
52
  # ====Hash structure
63
- # <em>required</em>
64
53
  # <code>allowed_fields</code>:: Fields that can be stored.
65
- # <em>optional</em>
66
54
  # <code>required_fields</code>:: Fields that must be provided on storage.
67
55
  # <code>read_mask</code>:: Fields returned on retrieval.
68
56
  # <code>write_mask</code>:: Fields that can be changed and stored.
69
- # The value for key must be an array.
70
57
  #
71
58
  # =====OR
72
- # <code>schema</code>: A hash whose value is the above hash structure.
59
+ # <code>schema</code>: A hash whose value is in the above hash structure.
73
60
  #
74
61
  # Notes
75
62
  # * Keys can either be symbols or strings.
@@ -88,7 +75,7 @@ module RiakRest
88
75
  # * the method argument is not either a hash or array
89
76
  # * The fields are not either symbols or strings
90
77
  # * The fields elements are not unique
91
- def initialize(arg)
78
+ def initialize(arg=nil)
92
79
  case arg
93
80
  when Hash
94
81
  # Jiak returns a JSON structure with a single key 'schema' whose value
@@ -97,51 +84,62 @@ module RiakRest
97
84
  opts = arg[:schema] || arg['schema'] || arg
98
85
 
99
86
  opts[:allowed_fields] ||= opts['allowed_fields']
100
- check_arr("allowed_fields",opts[:allowed_fields])
87
+ opts[:allowed_fields] = transform_fields("allowed_fields",
88
+ opts[:allowed_fields])
101
89
 
102
90
  # Use required if provided, otherwise set to empty array
103
91
  opts[:required_fields] ||= opts['required_fields'] || []
104
- check_arr("required_fields",opts[:required_fields])
92
+ opts[:required_fields] = transform_fields("required_fields",
93
+ opts[:required_fields])
105
94
 
106
95
  # Use masks if provided, otherwise set to allowed_fields
107
96
  [:read_mask,:write_mask].each do |key|
108
97
  opts[key] ||= opts[key.to_s] || opts[:allowed_fields]
109
- check_arr(key.to_s,opts[key])
98
+ opts[key] = transform_fields(key.to_s,opts[key])
110
99
  end
111
100
  when Array
112
101
  # An array arg must be a single-element array of the allowed
113
102
  # fields. Required fields is set to an empty array and the masks are
114
103
  # set to the allowed fields array.
115
- check_arr("allowed_fields",arg)
104
+ arg = transform_fields("allowed_fields",arg)
116
105
  opts = {
117
106
  :allowed_fields => arg,
118
107
  :required_fields => [],
119
108
  :read_mask => arg,
120
109
  :write_mask => arg
121
110
  }
111
+ when nil
112
+ arr = []
113
+ opts = {
114
+ :allowed_fields => arr,
115
+ :required_fields => arr,
116
+ :read_mask => arr,
117
+ :write_mask => arr
118
+ }
122
119
  else
123
120
  raise JiakSchemaException, "Initialize arg must be either hash or array"
124
121
  end
125
122
 
126
- @allowed_fields = opts[:allowed_fields]
127
- @required_fields = opts[:required_fields]
128
- @read_mask = opts[:read_mask]
129
- @write_mask = opts[:write_mask]
123
+ @allowed_fields = opts[:allowed_fields].dup
124
+ @required_fields = opts[:required_fields].dup
125
+ @read_mask = opts[:read_mask].dup
126
+ @write_mask = opts[:write_mask].dup
130
127
  end
131
128
 
132
129
  # call-seq:
133
130
  # JiakSchema.from_json(json) -> JiakSchema
134
131
  #
135
- # Create a JiakSchema from parsed JSON returned by the Jiak server.
136
- def self.from_jiak(jiak)
132
+ # Called by the Core Client framework when marshalling from Jiak. This
133
+ # method does not need to be call explicitly.
134
+ def self.jiak_create(jiak)
137
135
  new(jiak)
138
136
  end
139
137
 
140
- # call-seq:
141
- # schema.to_jiak -> JSON
138
+ # :call-seq:
139
+ # to_jiak -> JSON
142
140
  #
143
- # Create a representation suitable for sending to a Jiak server. Called by
144
- # JiakClient when transporting a schema to Jiak.
141
+ # Called by the Core Client framework when marshalling to Jiak. This method
142
+ # does not need to be call explicitly.
145
143
  def to_jiak
146
144
  { :schema =>
147
145
  { :allowed_fields => @allowed_fields,
@@ -149,24 +147,43 @@ module RiakRest
149
147
  :read_mask => @read_mask,
150
148
  :write_mask => @write_mask
151
149
  }
152
- }.to_json
150
+ }
153
151
  end
154
152
 
153
+ # :call-seq:
154
+ # allowed_fields = array
155
+ #
156
+ # Set the allowed fields array. Overrides whatever fields were in the array.
157
+ # Use JiakSchema#allow to add fields to the array.
155
158
  def allowed_fields=(arr) # :nodoc:
156
- check_arr('allowed_fields',arr)
157
- @allowed_fields = arr
159
+ @allowed_fields = transform_fields('allowed_fields',arr)
158
160
  end
161
+
162
+ # :call-seq:
163
+ # required_fields = array
164
+ #
165
+ # Set the required fields array. Overrides whatever fields were in the
166
+ # array. Use JiakSchema#require to add fields to the array.
159
167
  def required_fields=(arr) # :nodoc:
160
- check_arr('required_fields',arr)
161
- @required_fields = arr
168
+ @required_fields = transform_fields('required_fields',arr)
162
169
  end
170
+
171
+ # :call-seq:
172
+ # read_mask = array
173
+ #
174
+ # Set the read mask array. Overrides whatever fields were in the array.
175
+ # Use JiakSchema#readable to add fields to the array.
163
176
  def read_mask=(arr) # :nodoc:
164
- check_arr('read_mask',arr)
165
- @read_mask = arr
177
+ @read_mask = transform_fields('read_mask',arr)
166
178
  end
179
+
180
+ # :call-seq:
181
+ # write_mask = array
182
+ #
183
+ # Set the write mask array. Overrides whatever fields were in the array.
184
+ # Use JiakSchema#writable to add fields to the array.
167
185
  def write_mask=(arr) # :nodoc:
168
- check_arr('write_mask',arr)
169
- @write_mask = arr
186
+ @write_mask = transform_fields('write_mask',arr)
170
187
  end
171
188
 
172
189
  # :call-seq:
@@ -174,9 +191,126 @@ module RiakRest
174
191
  #
175
192
  # Set the read and write masks for a JiakSchema.
176
193
  def readwrite=(arr) # :nodoc:
177
- check_arr('readwrite',arr)
178
- @read_mask = arr
179
- @write_mask = arr
194
+ mask = transform_fields('readwrite',arr)
195
+ @read_mask = mask
196
+ @write_mask = mask
197
+ end
198
+
199
+ # :call-seq:
200
+ # allow(:f1,...,:fn) -> array
201
+ # allow([:f1,...,:fn]) -> array
202
+ #
203
+ # Add to the allowed fields array.
204
+ #
205
+ # Returns fields added to allowed fields.
206
+ def allow(*fields)
207
+ add_fields(@allowed_fields,"allow",fields)
208
+ end
209
+
210
+ # :call-seq:
211
+ # require(:f1,...,:fn) -> array
212
+ # require([:f1,...,:fn]) -> array
213
+ #
214
+ # Add fields to the required fields array. Adds the fields to the allowed
215
+ # fields as well.
216
+ #
217
+ # Returns fields added to required_fields.
218
+ def require(*fields)
219
+ added_fields = add_fields(@required_fields,"require",fields)
220
+ readwrite(*fields)
221
+ added_fields
222
+ end
223
+
224
+ # :call-seq:
225
+ # readable(:f1,...,:fn) -> array
226
+ # readable([:f1,...,:fn]) -> array
227
+ #
228
+ # Add fields to the read mask array. Adds the fields to the allowed
229
+ # fields as well.
230
+ #
231
+ # Returns fields added to read_mask
232
+ def readable(*fields)
233
+ added_fields = add_fields(@read_mask,"readable",fields)
234
+ allow(*fields)
235
+ added_fields
236
+ end
237
+
238
+ # :call-seq:
239
+ # writable(:f1,...,:fn) -> array
240
+ # writable([:f1,...,:fn]) -> array
241
+ #
242
+ # Add fields to the write mask array. Adds the fields to the allowed
243
+ # fields as well.
244
+ #
245
+ # Returns fields added to write_mask
246
+ def writable(*fields)
247
+ added_fields = add_fields(@write_mask,"writable",fields)
248
+ allow(*fields)
249
+ added_fields
250
+ end
251
+
252
+ # :call-seq:
253
+ # readwrite(:f1,...,:fn) -> nil
254
+ # readwrite([:f1,...,:fn]) -> nil
255
+ #
256
+ # Add fields to the read and write mask arrays. Adds the fields to the
257
+ # allowed fields as well.
258
+ #
259
+ # Returns nil.
260
+ def readwrite(*fields)
261
+ readable(*fields)
262
+ writable(*fields)
263
+ allow(*fields)
264
+ nil
265
+ end
266
+
267
+ # Scrub the fields by changing strings to symbols and removing any dups,
268
+ # then add new fields to arr and return added fields.
269
+ def add_fields(arr,descr,fields)
270
+ fields = fields[0] if(fields.size == 1 && fields[0].is_a?(Array))
271
+ scrubbed = transform_fields(descr,fields)
272
+ (scrubbed - arr).each {|f| arr << f}
273
+ end
274
+ private :add_fields
275
+
276
+ # :call-seq:
277
+ # allowed_fields -> array
278
+ #
279
+ # Return an array of the allowed fields. This array is a copy and cannot
280
+ # be used to update the allowed_fields array itself. Use JiakSchema#allow
281
+ # to add fields or JiakSchema#allowed_fields= to set the array.
282
+ def allowed_fields
283
+ @allowed_fields.dup
284
+ end
285
+
286
+ # :call-seq:
287
+ # required_fields -> array
288
+ #
289
+ # Return an array of the allowed fields. This array is a copy and cannot
290
+ # be used to update the required_fields array itself. Use JiakSchema#require
291
+ # to add fields or JiakSchema#required_fields= to set the array.
292
+ def required_fields
293
+ @required_fields.dup
294
+ end
295
+
296
+ # :call-seq:
297
+ # read_mask -> array
298
+ #
299
+ # Returns an array of the allowed fields. This array is a copy and cannot
300
+ # be used to update the read_mask array itself. Use JiakSchema#readable
301
+ # to add fields or JiakSchema#read_mask= to set the array.
302
+ def read_mask
303
+ @read_mask.dup
304
+ end
305
+
306
+ # :call-seq:
307
+ # write_mask -> array
308
+ #
309
+ # Returns an array of the allowed fields. This array is a copy and cannot
310
+ # be used to update the write_mask array itself. Use JiakSchema#writable
311
+ # to add fields or JiakSchema#write_mask= to set the array.
312
+ def write_mask
313
+ @write_mask.dup
180
314
  end
181
315
 
182
316
  # call-seq:
@@ -217,8 +351,8 @@ module RiakRest
217
351
  '",write_mask="'+@write_mask.inspect+'"'
218
352
  end
219
353
 
220
- # Each option must be an array of symbol or string elements.
221
- def check_arr(desc,arr)
354
+ # Check for array of symbol or string elements.
355
+ def transform_fields(desc,arr)
222
356
  if(arr.eql?("*"))
223
357
  raise(JiakSchemaException,
224
358
  "RiakRest does not support wildcard schemas at this time.")
@@ -231,11 +365,12 @@ module RiakRest
231
365
  raise JiakSchemaException, "#{desc} must be strings or symbols"
232
366
  end
233
367
  end
234
- unless arr.map{|f| f.to_s}.uniq.size == arr.size
235
- raise JiakSchemaException, "#{desc} must have unique elements."
236
- end
368
+ # unless arr.map{|f| f.to_s}.uniq.size == arr.size
369
+ # raise JiakSchemaException, "#{desc} must have unique elements."
370
+ # end
371
+ arr.map{|f| f.to_sym}.uniq
237
372
  end
238
- private :check_arr
373
+ private :transform_fields
239
374
 
240
375
  end
241
376