Class: ResourceTemplate::ResourceTemplates

Inherits:
Array
  • Object
show all
Defined in:
lib/resource_template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection = [], parent = nil) ⇒ ResourceTemplates

Initialize Resources (i.e. a new collection of ResourceTemplate objects) from given collection of ResourceTemplates or hashes



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/resource_template.rb', line 218

def initialize(collection=[], parent=nil)
  if collection
    raise ArgumentError.new("#{collection.inspect} is not a collection") unless collection.kind_of?(Enumerable)

    collection.each do |r|
      if r.kind_of?(ResourceTemplate)
        push(r)
      elsif r.kind_of?(Hash)
        push(ResourceTemplate.new(r, parent))
      else
        raise ArgumentError.new("#{r.inspect} is neither a ResourceTemplate nor a Hash")
      end
    end
  end
end

Class Method Details

.parse_xmlObject

Create Resources from an XML string

Raises:

  • (NotImplementedError)


235
236
237
# File 'lib/resource_template.rb', line 235

def self.parse_xml
  raise NotImplementedError.new
end

Instance Method Details

#all_by_name(h = {}) ⇒ Object

Get a hash of all named ResourceTemplate objects contained in the supplied collection, keyed by name



267
268
269
270
271
272
273
274
# File 'lib/resource_template.rb', line 267

def all_by_name(h = {})
  inject(h) do |hash, resource_template|
    hash[resource_template.name] = resource_template if resource_template.name
    resource_template.resource_templates.all_by_name(hash)
    hash
  end
  h
end

#all_postorderObject

All members and their descendants, descendants first



284
285
286
287
288
# File 'lib/resource_template.rb', line 284

def all_postorder
  inject([]) do |a, resource_template|
    a += resource_template.all_postorder
  end
end

#all_preorderObject

All members and their descendants, descendants last



277
278
279
280
281
# File 'lib/resource_template.rb', line 277

def all_preorder
  inject([]) do |a, resource_template|
    a += resource_template.all_preorder
  end
end

Return a new resource template with the path_template or uri_template expanded; expands also any child resource templates that don’t require additional parameters.



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/resource_template.rb', line 333

def expand_links(actual_params)
  ResourceTemplates.new(
    select{|rt| (rt.positional_params(rt.parent) - rt.optional_params).empty?}.map {|rt|
      {
        "name"               => rt.name,
        "rel"                => rt.rel,
        "uri_template"       => rt.partial_expand_uri_template(rt.uri_template, actual_params),
        "path_template"      => rt.partial_expand_uri_template(rt.path_template, actual_params),
        "params"             => rt.params - actual_params.keys,
        "optional_params"    => rt.optional_params - actual_params.keys,
        "options"            => rt.options
      }
    })
end

#partial_expand(actual_params) ⇒ Object

Partially expand the path_template or uri_template of the given resource templates with the given params, returning new resource templates



327
328
329
# File 'lib/resource_template.rb', line 327

def partial_expand(actual_params)
  ResourceTemplates.new(map{|resource_template| resource_template.partial_expand(actual_params)})
end

#to_jsonObject

Convert an array of ResourceTemplate objects to JSON



245
246
247
# File 'lib/resource_template.rb', line 245

def to_json
  to_parsed.to_json
end

#to_parsedObject

Convert member ResourceTemplate objects to array of hashes equivalent to their JSON or YAML representations



240
241
242
# File 'lib/resource_template.rb', line 240

def to_parsed
  map {|resource_template| resource_template.to_hash}
end

#to_table(parent_template = nil, t = [], indent = '') ⇒ Object

for #to_text



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/resource_template.rb', line 291

def to_table(parent_template = nil, t = [], indent = '')
  inject(t) do |table, resource_template|
    if parent_template
      link = (resource_template.rel || '')
      new_params = resource_template.params - parent_template.params
    else
      link = resource_template.name
      new_params = resource_template.params
    end
    link += new_params.map{|p| "{#{p}}"}.join(', ')
    table << [
      indent + link,
      resource_template.name || '',
      resource_template.options.join(', '),
      resource_template.uri_template || resource_template.path_template
    ] 
    resource_template.resource_templates.to_table(resource_template, t, indent + '  ')
  end
end

#to_textObject

text report



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/resource_template.rb', line 312

def to_text
  table = self.to_table

  0.upto(2) do |i|
    width = table.map{|row| row[i].length}.max
    table.each do |row|
      row[i] = row[i].ljust(width)
    end
  end

  table.map{|row| row.join(' ')}.join("\n") + "\n"
end

#to_xml(xm) ⇒ Object

Produces the XML format, given an XML builder object and an array of ResourceTemplate objects



257
258
259
260
261
262
263
264
# File 'lib/resource_template.rb', line 257

def to_xml(xm)
  xm.ResourceTemplates do |xm|
    each do |resource_template|
      resource_template.to_xml(xm)
    end
  end
  xm
end

#to_yamlObject

Convert an array of ResourceTemplate objects to YAML



250
251
252
# File 'lib/resource_template.rb', line 250

def to_yaml
  to_parsed.to_yaml
end