Class: Statesman::Adapters::Mongoid

Inherits:
Object
  • Object
show all
Defined in:
lib/statesman/adapters/mongoid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transition_class, parent_model, observer, _opts = {}) ⇒ Mongoid

Returns a new instance of Mongoid.



10
11
12
13
14
15
16
17
# File 'lib/statesman/adapters/mongoid.rb', line 10

def initialize(transition_class, parent_model, observer, _opts = {})
  @transition_class = transition_class
  @parent_model = parent_model
  @observer = observer
  unless transition_class_hash_fields.include?("statesman_metadata")
    raise UnserializedMetadataError, 
  end
end

Instance Attribute Details

#parent_modelObject (readonly)

Returns the value of attribute parent_model.



8
9
10
# File 'lib/statesman/adapters/mongoid.rb', line 8

def parent_model
  @parent_model
end

#transition_classObject (readonly)

Returns the value of attribute transition_class.



7
8
9
# File 'lib/statesman/adapters/mongoid.rb', line 7

def transition_class
  @transition_class
end

Instance Method Details

#create(from, to, metadata = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/statesman/adapters/mongoid.rb', line 19

def create(from, to,  = {})
  from = from.to_s
  to = to.to_s
  transition = transitions_for_parent.build(to_state: to,
                                            sort_key: next_sort_key,
                                            statesman_metadata: )

  @observer.execute(:before, from, to, transition)
  transition.save!
  @last_transition = transition
  @observer.execute(:after, from, to, transition)
  @observer.execute(:after_commit, from, to, transition)
  transition
ensure
  @last_transition = nil
end

#history(force_reload: false) ⇒ Object



36
37
38
39
# File 'lib/statesman/adapters/mongoid.rb', line 36

def history(force_reload: false)
  reset if force_reload
  transitions_for_parent.asc(:sort_key)
end

#last(force_reload: false) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/statesman/adapters/mongoid.rb', line 41

def last(force_reload: false)
  if force_reload
    @last_transition = history(force_reload: true).last
  else
    @last_transition ||= history.last
  end
end

#resetObject



49
50
51
52
# File 'lib/statesman/adapters/mongoid.rb', line 49

def reset
  # Aggressive, but the query cache can't be targeted at a more granular level
  ::Mongoid::QueryCache.clear_cache
end