Class: VersionCake::ExtractionStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/versioncake/strategies/extraction_strategy.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lookup(strategy) ⇒ 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
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 30

def self.lookup(strategy)
  case strategy
    when String, Symbol
      strategy_name = "#{strategy}_strategy".camelize
      begin
        VersionCake.const_get(strategy_name).new
      rescue
        raise Exception, "Unknown VersionCake extraction strategy #{strategy_name}"
      end
    when Proc
      if strategy.arity == 1
        VersionCake::CustomStrategy.new(strategy)
      else
        raise Exception, "Custom proc extraction strategy requires a single parameter"
      end
    when Object
      if !strategy.methods.include?(:execute)
        raise Exception, "Custom extraction strategy requires an execute method"
      elsif strategy.method(:execute).arity != 1
        raise Exception, "Custom extraction strategy requires an execute method with a single parameter"
      else
        VersionCake::CustomStrategy.new(strategy)
      end
    else
      raise Exception, "Invalid extration strategy"
  end
end

Instance Method Details

#execute(request) ⇒ Object

Execute should return a number or a numeric string if it successfully finds a version. If no version is found, nil should be returned. Any other results returned will raise an exception.

Raises:

  • (Exception)


26
27
28
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 26

def execute(request)
  raise Exception, "ExtractionStrategy requires execute to be implemented"
end

#extract(request) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 6

def extract(request)
  version = execute(request)
  if version.is_a?(Fixnum)
    version
  elsif version.is_a?(String) && /[0-9]+/.match(version)
    version.to_i
  elsif version.nil? # no version was found
    nil
  else
    raise Exception, "Invalid format for version number."
  end
end

#version_keyObject



19
20
21
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 19

def version_key
  VersionCake.config.version_key
end