Class: VersionedBlocks

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

Constant Summary collapse

VERSION =
"1.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_uriObject

Returns the value of attribute base_uri.



5
6
7
# File 'lib/versioned_blocks.rb', line 5

def base_uri
  @base_uri
end

.prepend_errorsObject

Returns the value of attribute prepend_errors.



5
6
7
# File 'lib/versioned_blocks.rb', line 5

def prepend_errors
  @prepend_errors
end

.versionsObject

Returns the value of attribute versions.



5
6
7
# File 'lib/versioned_blocks.rb', line 5

def versions
  @versions
end

Class Method Details

.base_uri_from_opts(opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/versioned_blocks.rb', line 14

def base_uri_from_opts(opts)
  # specify a base uri:
  #   base:'http://www.example.com/'
  # or set a default base like:
  #   VersionedRetry.base = 'http://www.example.com/'
  b = opts.has_key?(:base_uri) && (opts[:override]==true || self.base_uri=='') ? opts[:base_uri] : self.base_uri
  raise('Empty base URI! Set VersionedBlock.base_uri or pass a :base_uri as an option') if b.nil? || b==''
  b += '/' unless b[-1]=='/' # add a slash if it's missing
  b = b[0..-2] if b[-2..-1]=='//' # get rid of trailing double slashes
  b
end

.opts_specify_a_version?(opts) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/versioned_blocks.rb', line 26

def opts_specify_a_version?(opts)
  opts.has_key?(:to) || (opts.has_key?(:to) && opts.has_key?(:from)) || opts.has_key?(:only) || opts.has_key?(:these)
end

.resetObject



7
8
9
10
11
12
# File 'lib/versioned_blocks.rb', line 7

def reset
  # set defaults
  self.versions = {}
  self.base_uri = ''
  self.prepend_errors = false
end

.versions_from_opts(opts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/versioned_blocks.rb', line 30

def versions_from_opts(opts)
  # specifying versioning:
  #   pass {only:1} to test only v1
  #   pass {to:3} to test v1,v2,v3
  #   pass {from:2, to:3} to test v2,v3
  #   pass {these:[1,2,4]} to test v1,v2,v4 but NOT v3
  # you can set default versioning with:
  #   VersionedRetry.config = {}
  # you can then override that default for a specifig case by passing:
  #   override:true
  # or reset the default versioning with:
  #   VersionedRetry.reset
  opts = opts_specify_a_version?(opts) || (opts[:override]==true && opts_specify_a_version?(opts)) ? opts : self.versions 
  #opts = self.versions unless (opts[:override]==true || self.versions == {})
  if opts[:from].is_a?(Integer) && opts[:to].is_a?(Integer) # from vX to vY
    raise "Expected :from (#{opts[:from]}) to be less than :to (#{opts[:to]})" if opts[:from] > opts[:to]
    versions_to_test = (opts[:from]..opts[:to])
  else
    if opts[:to].is_a?(Integer) # up to vX
      versions_to_test = (1..opts[:to])
    elsif opts[:only].is_a?(Integer) # only vX
      versions_to_test = [opts[:only]]
    elsif opts[:these].is_a?(Array) # vX, vY, etc. for each of :these
      if opts[:these].all?{|n| n.is_a?(Integer)}
        versions_to_test = opts[:these]
      else
        raise "Each element in :these must be an integer"
      end
    else
      raise "Couldn't determine which versions to test!\nUse :only, :these, :to, or :from and :to"
    end
  end
  versions_to_test
end