Class: Riak::WalkSpec

Inherits:
Object show all
Includes:
Util::Escape, Util::Translation
Defined in:
lib/riak/walk_spec.rb

Overview

The specification of how to follow links from one object to another in Riak, when using the link-walker resource. Example link-walking operation:

GET /riak/artists/REM/albums,_,_/tracks,_,1

This operation would have two WalkSpecs:

Riak::WalkSpec.new({:bucket => 'albums'})
Riak::WalkSpec.new({:bucket => 'tracks', :result => true})

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Escape

#escape, #maybe_escape, #maybe_unescape, #unescape

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(hash) ⇒ WalkSpec #initialize(bucket, tag, keep) ⇒ WalkSpec

Creates a walk-spec for use in finding other objects in Riak.

Overloads:

  • #initialize(hash) ⇒ WalkSpec

    Creates a walk-spec from a hash.

    Parameters:

    • hash (Hash)

      options for the walk-spec

    Options Hash (hash):

    • :bucket (String) — default: "_"

      the bucket the links should point to (default ‘_’ is all)

    • :tag (String) — default: "_"

      the tag to filter links by (default ‘_’ is all)

    • :keep (Boolean) — default: false

      whether to return results from following this link specification

  • #initialize(bucket, tag, keep) ⇒ WalkSpec

    Creates a walk-spec from a bucket-tag-result triple.

    Parameters:

    • bucket (String)

      the bucket the links should point to (default ‘_’ is all)

    • tag (String)

      the tag to filter links by (default ‘_’ is all)

    • keep (Boolean)

      whether to return results from following this link specification

See Also:

  • Riak::WalkSpec.{Riak{Riak::RObject{Riak::RObject#walk}


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/riak/walk_spec.rb', line 61

def initialize(*args)
  args.flatten!
  case args.size
  when 1
    hash = args.first
    raise ArgumentError, t("hash_type", :hash => hash.inspect) unless Hash === hash
    assign(hash[:bucket], hash[:tag], hash[:keep])
  when 3
    assign(*args)
  else
    raise ArgumentError, t("wrong_argument_count_walk_spec")
  end
end

Instance Attribute Details

#bucketString

Returns The bucket followed links should be restricted to. “_” represents all buckets.

Returns:

  • (String)

    The bucket followed links should be restricted to. “_” represents all buckets.



18
19
20
# File 'lib/riak/walk_spec.rb', line 18

def bucket
  @bucket
end

#keepBoolean

Returns Whether objects should be returned from this phase of link walking. Default is false.

Returns:

  • (Boolean)

    Whether objects should be returned from this phase of link walking. Default is false.



24
25
26
# File 'lib/riak/walk_spec.rb', line 24

def keep
  @keep
end

#tagString

Returns The “riaktag” or “rel” that followed links should be restricted to. “_” represents all tags.

Returns:

  • (String)

    The “riaktag” or “rel” that followed links should be restricted to. “_” represents all tags.



21
22
23
# File 'lib/riak/walk_spec.rb', line 21

def tag
  @tag
end

Class Method Details

.normalize(*params) ⇒ Object

Normalize a list of walk specs into WalkSpec objects.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/riak/walk_spec.rb', line 27

def self.normalize(*params)
  params.flatten!
  specs = []
  while params.length > 0
    param = params.shift
    case param
    when Hash
      specs << new(param)
    when WalkSpec
      specs << param
    else
      if params.length >= 2
        specs << new(param, params.shift, params.shift)
      else
        raise ArgumentError, t("too_few_arguments", :params => params.inspect)
      end
    end
  end
  specs
end

Instance Method Details

#==(other) ⇒ Object



82
83
84
# File 'lib/riak/walk_spec.rb', line 82

def ==(other)
  other.is_a?(WalkSpec) && other.bucket == bucket && other.tag == tag && other.keep == keep
end

#===(other) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/riak/walk_spec.rb', line 86

def ===(other)
  self == other || case other
                   when WalkSpec
                     other.keep == keep &&
                       (bucket == "_" || bucket == other.bucket) &&
                       (tag == "_" || tag == other.tag)
                   when Link
                     (bucket == "_" || bucket == other.url.split("/")[2]) &&
                       (tag == "_" || tag == other.rel)
                   end
end

#to_sObject

Converts the walk-spec into the form required by the link-walker resource URL



76
77
78
79
80
# File 'lib/riak/walk_spec.rb', line 76

def to_s
  b = @bucket && escape(@bucket) || '_'
  t = @tag && escape(@tag) || '_'
  "#{b},#{t},#{@keep ? '1' : '_'}"
end