Class: Viewmaster::TemplateVersion

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ TemplateVersion

Returns a new instance of TemplateVersion.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
13
# File 'lib/viewmaster/template_version.rb', line 7

def initialize(opts={}, &block)
  @template_path = opts[:template_path]
  @name          = opts[:name]
  @transitions   = []
  @filter        = opts[:filter]
  yield self if block_given?
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/viewmaster/template_version.rb', line 4

def name
  @name
end

#template_pathObject

Returns the value of attribute template_path.



3
4
5
# File 'lib/viewmaster/template_version.rb', line 3

def template_path
  @template_path
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



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

def transitions
  @transitions
end

Class Method Details

.find(name) ⇒ Object



24
25
26
# File 'lib/viewmaster/template_version.rb', line 24

def self.find(name)
  Config.versions.detect{|o| o.name == name}
end

Instance Method Details

#can_transition_to?(transition_name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/viewmaster/template_version.rb', line 28

def can_transition_to?(transition_name)

  #includes current transition name to the whitelist
  tt = ([transitions.map(&:to).uniq] + [name]).flatten

  transition = transitions.detect{ |t| t.to.include? transition_name}
  can_transition = tt.include?(transition_name)
  if (!transition.nil? && transition.filter.is_a?(Proc))
    return can_transition && transition.filter.call(transition_name)
  else
    return can_transition
  end
end

#change_version_to(transition_name) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/viewmaster/template_version.rb', line 42

def change_version_to(transition_name)
  unless can_transition_to?(transition_name)
    raise "Can't transition to #{transition_name} from #{self.name}"
  end
  # @block_transition_before_change
  transitions.each{ |o| o.run_callback }
  # @block_transition_after_change
end

#transition(opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/viewmaster/template_version.rb', line 15

def transition(opts={})
  @transitions << Transition.new do |t|
    t.from        = name
    t.to          = opts[:to]
    t.callback    = opts[:do]
    t.filter      = opts[:filter]
  end
end