Module: Welo::Resource

Defined in:
lib/welo/base/resource.rb,
lib/welo/core/resource.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

A simple hook to extend the ClassMethods



14
15
16
# File 'lib/welo/core/resource.rb', line 14

def self.included(mod)
  mod.extend ClassMethods
end

Instance Method Details

#base_pathObject

Shorthand for class’ base_path



182
183
184
# File 'lib/welo/core/resource.rb', line 182

def base_path
  self.class.base_path
end

#epithet(name) ⇒ Object

Shorthand for class’ epithet



197
198
199
# File 'lib/welo/core/resource.rb', line 197

def epithet(name)
  self.class.epithet(name)
end

#epithet_path_to(resource, label) ⇒ Object



305
306
307
# File 'lib/welo/core/resource.rb', line 305

def epithet_path_to(resource, label)
  File.join label.to_s, epitheting_path_part(resource,label)
end

#epithet_resource_match_params?(resource, params, label, prefix = '') ⇒ Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/welo/core/resource.rb', line 314

def epithet_resource_match_params?(resource, params, label, prefix='')
  EpithetMatcher.new(params, prefix).match?(self, resource, label)
end

#epitheting_path_part(resource, label) ⇒ Object



301
302
303
# File 'lib/welo/core/resource.rb', line 301

def epitheting_path_part(resource, label)
  File.join epithet(label).map{|sym| send(sym, resource).to_s}
end

#epithets(label, prefix = '') ⇒ Object

Same as Resource.epithets



310
311
312
# File 'lib/welo/core/resource.rb', line 310

def epithets(label, prefix='')
  epithet(label).map{|sym| ":#{prefix}#{sym}"}
end

#identifiers(ident = :default, prefix = '') ⇒ Object

Same as Resource.identifiers



236
237
238
# File 'lib/welo/core/resource.rb', line 236

def identifiers(ident=:default, prefix='')
  identify(ident).map{|sym| ":#{prefix}#{sym}"}
end

#identify(ident = :default) ⇒ Object

Shorthand for class’ identify



192
193
194
# File 'lib/welo/core/resource.rb', line 192

def identify(ident=:default)
  self.class.identify(ident)
end

#identifying_path_part(ident = :default) ⇒ Object

Returns the resource’s path part mapping the various fields. The objects must respond to .to_s and return a value such that the URL part is valid (esp. no whitespace) This method does NOT check wether the return of .to_s builds a valid URL part



221
222
223
# File 'lib/welo/core/resource.rb', line 221

def identifying_path_part(ident=:default)
  File.join identify(ident).map{|sym| send(sym).to_s}
end


265
266
267
268
269
270
271
272
273
# File 'lib/welo/core/resource.rb', line 265

def link_for_rel(rel)
  if rel.one?
    single_link_for_rel(rel)
  elsif rel.many?
    links_enumerator_for_rel(rel)
  else
    raise ArgumentError, "unkown relationship kinds #{rel.kinds}"
  end #case rel.kind
end

returns a lazy link loader enumerator for the relationship it will yield a link



286
287
288
289
290
291
292
293
# File 'lib/welo/core/resource.rb', line 286

def links_enumerator_for_rel(rel)
  sym = rel.sym
  LinksEnumerator.new do |&blk|
    send(sym).each do |i|
      blk.call Link.new(self, i, :label => sym, :local => rel.alias?)
    end
  end
end

#match_params?(params, ident = :default, prefix = '') ⇒ Boolean

Returns:

  • (Boolean)


295
296
297
# File 'lib/welo/core/resource.rb', line 295

def match_params?(params, ident=:default, prefix='')
  IdentifyingMatcher.new(params, prefix).match?(self, ident)
end

#nesting(resource_sym) ⇒ Object

Shorthand for class’ nesting



212
213
214
# File 'lib/welo/core/resource.rb', line 212

def nesting(resource_sym)
  self.class.nesting(resource_sym)
end

#path(ident = :default) ⇒ Object

Returns the full URL of this object



226
227
228
229
230
231
232
233
# File 'lib/welo/core/resource.rb', line 226

def path(ident=:default)
  tail = identifying_path_part(ident)
  if tail.empty?
    base_path
  else
    File.join base_path, tail
  end
end

#path_model(ident = :default, prefix = '') ⇒ Object

Shorthand for class’ path_model



187
188
189
# File 'lib/welo/core/resource.rb', line 187

def path_model(ident=:default, prefix='')
  self.class.path_model(ident, prefix)
end

#perspective(sym) ⇒ Object

Shorthand for class’ perspective



202
203
204
# File 'lib/welo/core/resource.rb', line 202

def perspective(sym)
  self.class.perspective(sym)
end

#relationship(sym) ⇒ Object

Shorthand for class’ relationship



207
208
209
# File 'lib/welo/core/resource.rb', line 207

def relationship(sym)
  self.class.relationship(sym)
end

#serialized_pairs(persp) ⇒ Object

returns an array of pairs of fields name and fields values for the perspective.

the order of the pairs is the same as the fields in the perspective

Links to other resources are represented as urls by sending them the :to_s method. the name of this method comes from the fact that Links are serialized to strings



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/welo/base/resource.rb', line 32

def serialized_pairs(persp)
  ary = structure_pairs(persp).map do |sym, val|
    i1 = case val
         when Link
           val.to_s
         when LinksEnumerator
           val.map{|v| v.to_s}
         else
           val
         end
    [sym, i1]
  end
end

returns a lazy link loader for the relationship i.e., the evaluation of the sym method may be delayed



277
278
279
280
281
282
# File 'lib/welo/core/resource.rb', line 277

def single_link_for_rel(rel)
  sym = rel.sym
  Link.new(self, nil, :label => sym, :local => rel.alias?) do 
    send(sym)
  end
end

#structure(name) ⇒ Object

Shorthand for class’ structure



241
242
243
# File 'lib/welo/core/resource.rb', line 241

def structure(name)
  self.class.structure(name)
end

#structure_pairs(name) ⇒ Object

Returns the structure as an array of pairs which first object is the field name, the second object being the value. When the class’ structure mandates a Relationship, then the second object of the pair is a Link to the related resource, or an LinksEnumerator if it has many resources.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/welo/core/resource.rb', line 250

def structure_pairs(name) 
  structure(name).map do |rel|
    sym = nil
    ret = case rel
          when Symbol
            sym = rel
            send(sym)
          when Relationship
            sym = rel.sym
            link_for_rel(rel)
          end #case rel
    [sym, ret]
  end
end

#to_ext(ext, persp = :default) ⇒ Object

shortcut to call to_#ext



64
65
66
67
# File 'lib/welo/base/resource.rb', line 64

def to_ext(ext, persp=:default)
  meth = ext.sub(/^\./, 'to_')
  send(meth, persp)
end

#to_json(persp) ⇒ Object

returns a JSON representation of the resource, under the persp perspective



53
54
55
# File 'lib/welo/base/resource.rb', line 53

def to_json(persp)
  to_serialized_hash(persp).to_json
end

#to_serialized_hash(persp) ⇒ Object

similar to to_rb_hash, but with the urls serialized



47
48
49
# File 'lib/welo/base/resource.rb', line 47

def to_serialized_hash(persp)
  Hash[serialized_pairs(persp)]
end

#to_text(persp) ⇒ Object

returns a text representation of the resource, under the persp perspective, the format is not standardized, suitable for dev cycles



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/welo/base/resource.rb', line 9

def to_text(persp)
  ret = ''
  structure_pairs(persp).each do |k,val|
    ret << "##{k}\n"
    if val.respond_to? :map
      ret << val.map{|v| v.to_s}.join("\n")
    else
      ret << val.to_s
    end
    ret << "\n"
  end
  ret
end

#to_YAML(persp) ⇒ Object

returns a YAML representation of the resource, under the persp perspective



59
60
61
# File 'lib/welo/base/resource.rb', line 59

def to_YAML(persp)
  YAML.dump to_serialized_hash(persp)
end