Permalink
Browse files

Cleanup

* Use Ruby 1.9 hash syntax
* Remove usage of explicit self in cases that apply
  • Loading branch information...
1 parent 2e57d22 commit 20339fd3c207377fcc496526a0fedbd66fe620bf Travis Anderson committed with Hashrocket Workstation Jan 16, 2013
@@ -30,10 +30,10 @@ module Transitions
validates_presence_of :state
validate :state_inclusion
end
-
+
# The optional options argument is passed to find when reloading so you may
- # do e.g. record.reload(:lock => true) to reload the same record with an
- # exclusive row lock.
+ # do e.g. record.reload(lock: true) to reload the same record with an
+ # exclusive row lock.
def reload(options = nil)
super.tap do
sm = self.class.get_state_machine
@@ -51,24 +51,24 @@ def write_state(state)
write_state_without_persistence(prev_state)
raise
end
-
+
def write_state_without_persistence(state)
ivar = self.class.get_state_machine.current_state_variable
instance_variable_set(ivar, state)
self.state = state.to_s
end
def read_state
- self.state && self.state.to_sym
+ state && state.to_sym
end
def set_initial_state
- self.state ||= self.class.get_state_machine.initial_state.to_s if self.has_attribute?(:state)
+ self.state ||= self.class.get_state_machine.initial_state.to_s if has_attribute?(:state)
end
def state_inclusion
- unless self.class.get_state_machine.states.map{|s| s.name.to_s }.include?(self.state.to_s)
- self.errors.add(:state, :inclusion, :value => self.state)
+ unless self.class.get_state_machine.states.detect{|s| s.name.to_s == state.to_s }
+ self.errors.add(:state, :inclusion, value: state)
end
end
end
View
@@ -44,7 +44,7 @@ def initialize(machine, name, options = {}, &block)
def fire(obj, to_state = nil, *args)
transitions = @transitions.select { |t| t.from == obj.current_state }
- raise InvalidTransition, error_message_for_invalid_transitions(obj, to_state) if transitions.size == 0
+ raise InvalidTransition, error_message_for_invalid_transitions(obj, to_state) if transitions.empty?
next_state = nil
transitions.each do |transition|
@@ -71,7 +71,7 @@ def ==(event)
name == event.name
end
end
-
+
# Has the timestamp option been specified for this event?
def timestamp_defined?
!@timestamp.nil?
@@ -83,12 +83,12 @@ def update(options = {}, &block)
instance_eval(&block) if block
self
end
-
+
# update the timestamp attribute on obj
def update_event_timestamp(obj, next_state)
obj.send "#{timestamp_attribute_name(obj, next_state)}=", Time.now
end
-
+
# Set the timestamp attribute.
# @raise [ArgumentError] timestamp should be either a String, Symbol or true
def timestamp=(value)
@@ -99,17 +99,17 @@ def timestamp=(value)
raise ArgumentError, "timestamp must be either: true, a String or a Symbol"
end
end
-
+
private
-
+
# Returns the name of the timestamp attribute for this event
# If the timestamp was simply true it returns the default_timestamp_name
# otherwise, returns the user-specified timestamp name
def timestamp_attribute_name(obj, next_state)
timestamp == true ? default_timestamp_name(obj, next_state) : @timestamp
end
-
+
# If @timestamp is true, try a default timestamp name
def default_timestamp_name(obj, next_state)
at_name = "%s_at" % next_state
@@ -118,14 +118,14 @@ def default_timestamp_name(obj, next_state)
when obj.respond_to?(at_name) then at_name
when obj.respond_to?(on_name) then on_name
else
- raise NoMethodError, "Couldn't find a suitable timestamp field for event: #{@name}.
+ raise NoMethodError, "Couldn't find a suitable timestamp field for event: #{@name}.
Please define #{at_name} or #{on_name} in #{obj.class}"
end
end
def transitions(trans_opts)
Array(trans_opts[:from]).each do |s|
- @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym}))
+ @transitions << StateTransition.new(trans_opts.merge({from: s.to_sym}))
end
end
@@ -85,7 +85,7 @@ def current_state_variable
def state(name, options = {})
unless @state_index.key? name # Just ignore duplicates
- state = State.new(name, :machine => self)
+ state = State.new(name, machine: self)
state.update options
@state_index[name] = state
@states << state
@@ -102,7 +102,7 @@ def include_scopes
if @klass.respond_to?(state_name)
raise InvalidMethodOverride, "Transitions: Can not define scope `#{state_name}` because there is already an equally named method defined - either rename the existing method or the state."
end
- @klass.scope state_name, @klass.where(:state => state_name)
+ @klass.scope state_name, @klass.where(state: state_name)
end
end
end
@@ -138,7 +138,7 @@ def setup
@light.update_attribute(:state, 'green')
assert @light.reload.green?, "reloaded state should come from database, not instance variable"
end
-
+
test "calling non-bang event updates state attribute" do
@light.reset!
assert @light.red?

1 comment on commit 20339fd

For great good 👍

Please sign in to comment.