Skip to content

Commit

Permalink
Merge pull request #153 from troessner/introduce-rubocop-properly
Browse files Browse the repository at this point in the history
Introduce rubocop properly.
  • Loading branch information
Timo Rößner committed Dec 25, 2015
2 parents 181a038 + 4093f20 commit 07e2a59
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 22 deletions.
93 changes: 91 additions & 2 deletions .rubocop.yml
@@ -1,4 +1,93 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2015-12-25 17:34:26 +0100 using RuboCop version 0.34.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/AmbiguousRegexpLiteral:
Exclude:
- 'test/event/test_event_being_fired.rb'

# Offense count: 2
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/transitions/machine.rb'
- 'lib/transitions/state.rb'

# Offense count: 2
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'test/state_transition/test_state_transition_guard_check.rb'

# Offense count: 4
Metrics/AbcSize:
Max: 23

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 108

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 7

# Offense count: 13
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 120
Style/Documentation:
Max: 196

# Offense count: 5
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 20

# Offense count: 2
Style/AccessorMethodName:
Exclude:
- 'lib/transitions.rb'
- 'test/helper.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/ClassAndModuleChildren:
Exclude:
- 'lib/active_model/transitions.rb'

# Offense count: 2
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/For:
Enabled: false

# Offense count: 2
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/FormatString:
Exclude:
- 'lib/transitions/event.rb'

# Offense count: 6
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/active_model/transitions.rb'
- 'lib/transitions/machine.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
Style/Next:
Exclude:
- 'lib/transitions/event.rb'

# Offense count: 3
Style/OpMethod:
Exclude:
- 'lib/transitions/event.rb'
- 'lib/transitions/state.rb'
- 'lib/transitions/state_transition.rb'

Documentation:
Enabled: false
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,8 @@ sudo: false
cache: bundler
language: ruby
bundler_args: --without local_development
script:
- bundle exec rake
rvm:
- 2.1
- 2.2
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Expand Up @@ -10,6 +10,10 @@ Rake::TestTask.new(:test) do |test|
test.verbose = true
end

RuboCop::RakeTask.new
RuboCop::RakeTask.new do |task|
task.options << '--display-cop-names'
task.patterns = ['lib/**/*.rb', 'test/**/*.rb']
end

task default: :test
task default: :rubocop
6 changes: 3 additions & 3 deletions bin/console
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "transitions"
require 'bundler/setup'
require 'transitions'

require "pry"
require 'pry'
Pry.start
2 changes: 1 addition & 1 deletion lib/active_model/transitions.rb
Expand Up @@ -66,7 +66,7 @@ def read_state

def set_initial_state
# In case we use a query with a custom select that excludes our state attribute name we need to skip the initialization below.
if self.attribute_names.include?(transitions_state_column_name.to_s) && state_not_set?
if attribute_names.include?(transitions_state_column_name.to_s) && state_not_set?
self[transitions_state_column_name] = self.class.get_state_machine.initial_state.to_s
self.class.get_state_machine.state_index[self[transitions_state_column_name].to_sym].call_action(:enter, self)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/transitions.rb
Expand Up @@ -6,7 +6,7 @@
require 'transitions/version'

module Transitions
class InvalidTransition < StandardError; end
class InvalidTransition < StandardError; end
class InvalidMethodOverride < StandardError; end
include Presenter

Expand Down Expand Up @@ -49,7 +49,7 @@ def update_current_state(new_state, persist = false)

if Transitions.active_model_descendant?(self.class)
write_state(new_state) if persist
write_state_without_persistence(new_state) # TODO This seems like a duplicate, `write_new` already calls `write_state_without_persistence`.
write_state_without_persistence(new_state) # TODO: This seems like a duplicate, `write_new` already calls `write_state_without_persistence`.
end

instance_variable_set(ivar, new_state)
Expand Down
6 changes: 4 additions & 2 deletions lib/transitions/event.rb
Expand Up @@ -3,7 +3,9 @@ class Event
attr_reader :name, :success, :timestamp

def initialize(machine, name, options = {}, &block)
@machine, @name, @transitions = machine, name, []
@machine = machine
@name = name
@transitions = []
if machine
machine.klass.send(:define_method, "#{name}!") do |*args|
machine.fire_event(name, self, true, *args)
Expand Down Expand Up @@ -128,7 +130,7 @@ def build_success_callback(callback_names)
when Proc
callback_names
when Symbol
lambda { |record| record.send(callback_names) }
->(record) { record.send(callback_names) }
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/transitions/machine.rb
Expand Up @@ -5,7 +5,10 @@ class Machine
attr_reader :klass, :auto_scopes

def initialize(klass, options = {}, &block)
@klass, @states, @state_index, @events = klass, [], {}, {}
@klass = klass
@states = []
@state_index = {}
@events = {}
update(options, &block)
end

Expand All @@ -21,7 +24,7 @@ def update(options = {}, &block)
self
end

# TODO There is still way to much parameter passing around going on down below.
# TODO: There is still way to much parameter passing around going on down below.
def fire_event(event, record, persist, *args)
handle_state_exit_callback record
if new_state = transition_to_new_state(record, event, *args)
Expand Down Expand Up @@ -49,7 +52,7 @@ def events_for(state)
end

def current_state_variable
# TODO Refactor me away.
# TODO: Refactor me away.
:@current_state
end

Expand Down
5 changes: 3 additions & 2 deletions lib/transitions/state.rb
Expand Up @@ -29,7 +29,7 @@ def call_action(action, record)
end

def display_name
@display_name ||= name.to_s.gsub(/_/, ' ').capitalize
@display_name ||= name.to_s.tr('_', ' ').capitalize
end

def for_select
Expand All @@ -45,7 +45,8 @@ def update(options = {})
private

def define_state_query_method(machine)
method_name, state_name = "#{@name}?", @name # Instance vars are out of scope when calling define_method below, so we use local variables.
method_name = "#{@name}?"
state_name = @name # Instance vars are out of scope when calling define_method below, so we use local variables.
if machine.klass.method_defined?(method_name.to_sym)
fail InvalidMethodOverride, "Transitions: Can not define method `#{method_name}` because it is already defined - either rename the existing method or the state."
end
Expand Down
7 changes: 5 additions & 2 deletions lib/transitions/state_transition.rb
Expand Up @@ -3,7 +3,10 @@ class StateTransition
attr_reader :from, :to, :options

def initialize(opts)
@from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
@from = opts[:from]
@to = opts[:to]
@guard = opts[:guard]
@on_transition = opts[:on_transition]
@options = opts
end

Expand All @@ -24,7 +27,7 @@ def execute(obj, *args)
obj.send(callback, *args)
end
else
# TODO We probably should check for this in the constructor and not that late.
# TODO: We probably should check for this in the constructor and not that late.
fail ArgumentError, "You can only pass a Symbol, a String, a Proc or an Array to 'on_transition' - got #{@on_transition.class}." unless @on_transition.nil?
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/active_record/test_active_record_timestamps.rb
Expand Up @@ -32,7 +32,7 @@ class Order < ActiveRecord::Base

# should set paid_at timestamp
event :pay, timestamp: true do
transitions from: :placed, to: :paid, guard: lambda { |obj| obj.allow_transition }
transitions from: :placed, to: :paid, guard: ->(obj) { obj.allow_transition }
end

# should set prepared_on
Expand Down
2 changes: 1 addition & 1 deletion test/event/test_event.rb
Expand Up @@ -4,7 +4,7 @@ class TestEvent < Test::Unit::TestCase
def setup
@state_name = :close_order
@success_as_symbol = :success_callback
@success_as_lambda = lambda { |record| record.success_callback }
@success_as_lambda = ->(record) { record.success_callback }
@success_as_array = [@success_as_symbol, @success_as_lambda]
end

Expand Down
Expand Up @@ -14,7 +14,7 @@ class DrivingSchoolCar
state :driving
state :switched_off

event :start_driving, success: lambda { |_car| DrivingInstructor.applause! } do
event :start_driving, success: ->(_car) { DrivingInstructor.applause! } do
transitions from: :parked, to: :driving, on_transition: [:start_engine, :loosen_handbrake, :push_gas_pedal]
end

Expand Down
2 changes: 1 addition & 1 deletion transitions.gemspec
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.0'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'test-unit', '~> 2.5'
spec.add_development_dependency 'mocha', '~> 0.11.0' # With mocha 0.12 we get: undefined method `run' for #<StateMachineMachineTest:0x94918b8> (NoMethodError)
spec.add_development_dependency 'random_data'
Expand Down

0 comments on commit 07e2a59

Please sign in to comment.