Class: VersionCake::ExtractionStrategy

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

Defined Under Namespace

Classes: InvalidStrategyError, InvalidVersionError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.list(*strategies) ⇒ Object



38
39
40
41
42
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 38

def self.list(*strategies)
  strategies.map do |strategy|
    lookup(strategy)
  end
end

.lookup(strategy) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 44

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

  • (StandardError)


34
35
36
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 34

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

#extract(request) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 10

def extract(request)
  version = execute(request)
  if version.is_a?(Integer)
    version
  elsif version.is_a?(String) && /[0-9]+/.match(version)
    version.to_i
  elsif version_blank?(version)
    nil
  else
    raise InvalidVersionError, "Invalid format for version number."
  end
end

#version_blank?(version) ⇒ Boolean



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

def version_blank?(version)
  version.nil? || (version.is_a?(String) && version.length == 0)
end

#version_keyObject



23
24
25
# File 'lib/versioncake/strategies/extraction_strategy.rb', line 23

def version_key
  VersionCake.config.version_key
end