enum_state_machine 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -12
  3. data/.ruby-version +1 -1
  4. data/.ruby-version.orig +5 -0
  5. data/Gemfile +0 -1
  6. data/Rakefile +0 -18
  7. data/enum_state_machine.gemspec +35 -0
  8. data/enum_state_machine.gemspec.orig +43 -0
  9. data/lib/enum_state_machine/assertions.rb +36 -0
  10. data/lib/enum_state_machine/branch.rb +225 -0
  11. data/lib/enum_state_machine/callback.rb +232 -0
  12. data/lib/enum_state_machine/core.rb +12 -0
  13. data/lib/enum_state_machine/core_ext/class/state_machine.rb +5 -0
  14. data/lib/enum_state_machine/core_ext.rb +2 -0
  15. data/lib/enum_state_machine/error.rb +13 -0
  16. data/lib/enum_state_machine/eval_helpers.rb +87 -0
  17. data/lib/enum_state_machine/event.rb +257 -0
  18. data/lib/enum_state_machine/event_collection.rb +141 -0
  19. data/lib/enum_state_machine/extensions.rb +149 -0
  20. data/lib/enum_state_machine/graph.rb +93 -0
  21. data/lib/enum_state_machine/helper_module.rb +17 -0
  22. data/lib/enum_state_machine/initializers/rails.rb +22 -0
  23. data/lib/enum_state_machine/initializers.rb +4 -0
  24. data/lib/enum_state_machine/integrations/active_model/locale.rb +11 -0
  25. data/lib/enum_state_machine/integrations/active_model/observer.rb +33 -0
  26. data/lib/enum_state_machine/integrations/active_model/observer_update.rb +42 -0
  27. data/lib/enum_state_machine/integrations/active_model/versions.rb +31 -0
  28. data/lib/enum_state_machine/integrations/active_model.rb +585 -0
  29. data/lib/enum_state_machine/integrations/active_record/locale.rb +20 -0
  30. data/lib/enum_state_machine/integrations/active_record/versions.rb +123 -0
  31. data/lib/enum_state_machine/integrations/active_record.rb +548 -0
  32. data/lib/enum_state_machine/integrations/base.rb +100 -0
  33. data/lib/enum_state_machine/integrations.rb +97 -0
  34. data/lib/enum_state_machine/machine.rb +2292 -0
  35. data/lib/enum_state_machine/machine_collection.rb +86 -0
  36. data/lib/enum_state_machine/macro_methods.rb +518 -0
  37. data/lib/enum_state_machine/matcher.rb +123 -0
  38. data/lib/enum_state_machine/matcher_helpers.rb +54 -0
  39. data/lib/enum_state_machine/node_collection.rb +222 -0
  40. data/lib/enum_state_machine/path.rb +120 -0
  41. data/lib/enum_state_machine/path_collection.rb +90 -0
  42. data/lib/enum_state_machine/state.rb +297 -0
  43. data/lib/enum_state_machine/state_collection.rb +112 -0
  44. data/lib/enum_state_machine/state_context.rb +138 -0
  45. data/lib/enum_state_machine/state_enum.rb +23 -0
  46. data/lib/enum_state_machine/transition.rb +470 -0
  47. data/lib/enum_state_machine/transition_collection.rb +245 -0
  48. data/lib/enum_state_machine/version.rb +3 -0
  49. data/lib/enum_state_machine/yard/handlers/base.rb +32 -0
  50. data/lib/enum_state_machine/yard/handlers/event.rb +25 -0
  51. data/lib/enum_state_machine/yard/handlers/machine.rb +344 -0
  52. data/lib/enum_state_machine/yard/handlers/state.rb +25 -0
  53. data/lib/enum_state_machine/yard/handlers/transition.rb +47 -0
  54. data/lib/enum_state_machine/yard/handlers.rb +12 -0
  55. data/lib/enum_state_machine/yard/templates/default/class/html/setup.rb +30 -0
  56. data/lib/enum_state_machine/yard/templates/default/class/html/state_machines.erb +12 -0
  57. data/lib/enum_state_machine/yard/templates.rb +3 -0
  58. data/lib/enum_state_machine/yard.rb +8 -0
  59. data/lib/enum_state_machine.rb +9 -0
  60. data/lib/tasks/enum_state_machine.rake +1 -0
  61. data/lib/tasks/enum_state_machine.rb +24 -0
  62. data/lib/yard-enum_state_machine.rb +2 -0
  63. data/test/functional/state_machine_test.rb +1066 -0
  64. data/test/unit/graph_test.rb +9 -5
  65. data/test/unit/integrations/active_model_test.rb +1245 -0
  66. data/test/unit/integrations/active_record_test.rb +2551 -0
  67. data/test/unit/integrations/base_test.rb +104 -0
  68. data/test/unit/integrations_test.rb +71 -0
  69. data/test/unit/invalid_event_test.rb +20 -0
  70. data/test/unit/invalid_parallel_transition_test.rb +18 -0
  71. data/test/unit/invalid_transition_test.rb +115 -0
  72. data/test/unit/machine_collection_test.rb +603 -0
  73. data/test/unit/machine_test.rb +3395 -0
  74. data/test/unit/state_machine_test.rb +31 -0
  75. metadata +212 -44
  76. data/Appraisals +0 -28
  77. data/gemfiles/active_model_4.0.4.gemfile +0 -9
  78. data/gemfiles/active_model_4.0.4.gemfile.lock +0 -51
  79. data/gemfiles/active_record_4.0.4.gemfile +0 -11
  80. data/gemfiles/active_record_4.0.4.gemfile.lock +0 -61
  81. data/gemfiles/default.gemfile +0 -7
  82. data/gemfiles/default.gemfile.lock +0 -27
  83. data/gemfiles/graphviz_1.0.9.gemfile +0 -7
  84. data/gemfiles/graphviz_1.0.9.gemfile.lock +0 -30
@@ -0,0 +1,100 @@
1
+ module EnumStateMachine
2
+ module Integrations
3
+ # Provides a set of base helpers for managing individual integrations
4
+ module Base
5
+ module ClassMethods
6
+ # The default options to use for state machines using this integration
7
+ attr_reader :defaults
8
+
9
+ # The name of the integration
10
+ def integration_name
11
+ @integration_name ||= begin
12
+ name = self.name.split('::').last
13
+ name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
14
+ name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
15
+ name.downcase!
16
+ name.to_sym
17
+ end
18
+ end
19
+
20
+ # Whether this integration is available for the current library. This
21
+ # is only true if the ORM that the integration is for is currently
22
+ # defined.
23
+ def available?
24
+ matching_ancestors.any? && Object.const_defined?(matching_ancestors[0].split('::')[0])
25
+ end
26
+
27
+ # The list of ancestor names that cause this integration to matched.
28
+ def matching_ancestors
29
+ []
30
+ end
31
+
32
+ # Whether the integration should be used for the given class.
33
+ def matches?(klass)
34
+ matches_ancestors?(klass.ancestors.map {|ancestor| ancestor.name})
35
+ end
36
+
37
+ # Whether the integration should be used for the given list of ancestors.
38
+ def matches_ancestors?(ancestors)
39
+ (ancestors & matching_ancestors).any?
40
+ end
41
+
42
+ # Tracks the various version overrides for an integration
43
+ def versions
44
+ @versions ||= []
45
+ end
46
+
47
+ # Creates a new version override for an integration. When this
48
+ # integration is activated, each version that is marked as active will
49
+ # also extend the integration.
50
+ #
51
+ # == Example
52
+ #
53
+ # module EnumStateMachine
54
+ # module Integrations
55
+ # module ORMLibrary
56
+ # version '0.2.x - 0.3.x' do
57
+ # def self.active?
58
+ # ::ORMLibrary::VERSION >= '0.2.0' && ::ORMLibrary::VERSION < '0.4.0'
59
+ # end
60
+ #
61
+ # def invalidate(object, attribute, message, values = [])
62
+ # # Override here...
63
+ # end
64
+ # end
65
+ # end
66
+ # end
67
+ # end
68
+ #
69
+ # In the above example, a version override is defined for the ORMLibrary
70
+ # integration when the version is between 0.2.x and 0.3.x.
71
+ def version(name, &block)
72
+ versions << mod = Module.new(&block)
73
+ mod
74
+ end
75
+
76
+ # The path to the locale file containing translations for this
77
+ # integration. This file will only exist for integrations that actually
78
+ # support i18n.
79
+ def locale_path
80
+ path = "#{File.dirname(__FILE__)}/#{integration_name}/locale.rb"
81
+ path if File.exists?(path)
82
+ end
83
+
84
+ # Extends the given object with any version overrides that are currently
85
+ # active
86
+ def extended(base)
87
+ versions.each do |version|
88
+ base.extend(version) if version.active?
89
+ end
90
+ end
91
+ end
92
+
93
+ extend ClassMethods
94
+
95
+ def self.included(base) #:nodoc:
96
+ base.class_eval { extend ClassMethods }
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,97 @@
1
+ # Load each available integration
2
+ require 'enum_state_machine/integrations/base'
3
+ Dir["#{File.dirname(__FILE__)}/integrations/*.rb"].sort.each do |path|
4
+ require "enum_state_machine/integrations/#{File.basename(path)}"
5
+ end
6
+
7
+ require 'enum_state_machine/error'
8
+
9
+ module EnumStateMachine
10
+ # An invalid integration was specified
11
+ class IntegrationNotFound < Error
12
+ def initialize(name)
13
+ super(nil, "#{name.inspect} is an invalid integration")
14
+ end
15
+ end
16
+
17
+ # Integrations allow state machines to take advantage of features within the
18
+ # context of a particular library. This is currently most useful with
19
+ # database libraries. For example, the various database integrations allow
20
+ # state machines to hook into features like:
21
+ # * Saving
22
+ # * Transactions
23
+ # * Observers
24
+ # * Scopes
25
+ # * Callbacks
26
+ # * Validation errors
27
+ #
28
+ # This type of integration allows the user to work with state machines in a
29
+ # fashion similar to other object models in their application.
30
+ #
31
+ # The integration interface is loosely defined by various unimplemented
32
+ # methods in the EnumStateMachine::Machine class. See that class or the various
33
+ # built-in integrations for more information about how to define additional
34
+ # integrations.
35
+ module Integrations
36
+ # Attempts to find an integration that matches the given class. This will
37
+ # look through all of the built-in integrations under the EnumStateMachine::Integrations
38
+ # namespace and find one that successfully matches the class.
39
+ #
40
+ # == Examples
41
+ #
42
+ # class Vehicle
43
+ # end
44
+ #
45
+ # class ActiveModelVehicle
46
+ # include ActiveModel::Observing
47
+ # include ActiveModel::Validations
48
+ # end
49
+ #
50
+ # class ActiveRecordVehicle < ActiveRecord::Base
51
+ # end
52
+ #
53
+ #
54
+ # EnumStateMachine::Integrations.match(Vehicle) # => nil
55
+ # EnumStateMachine::Integrations.match(ActiveModelVehicle) # => EnumStateMachine::Integrations::ActiveModel
56
+ # EnumStateMachine::Integrations.match(ActiveRecordVehicle) # => EnumStateMachine::Integrations::ActiveRecord
57
+ def self.match(klass)
58
+ all.detect {|integration| integration.matches?(klass)}
59
+ end
60
+
61
+ # Attempts to find an integration that matches the given list of ancestors.
62
+ # This will look through all of the built-in integrations under the EnumStateMachine::Integrations
63
+ # namespace and find one that successfully matches one of the ancestors.
64
+ #
65
+ # == Examples
66
+ #
67
+ # EnumStateMachine::Integrations.match([]) # => nil
68
+ # EnumStateMachine::Integrations.match(['ActiveRecord::Base') # => EnumStateMachine::Integrations::ActiveModel
69
+ def self.match_ancestors(ancestors)
70
+ all.detect {|integration| integration.matches_ancestors?(ancestors)}
71
+ end
72
+
73
+ # Finds an integration with the given name. If the integration cannot be
74
+ # found, then a NameError exception will be raised.
75
+ #
76
+ # == Examples
77
+ #
78
+ # EnumStateMachine::Integrations.find_by_name(:active_record) # => EnumStateMachine::Integrations::ActiveRecord
79
+ # EnumStateMachine::Integrations.find_by_name(:active_model) # => EnumStateMachine::Integrations::ActiveModel
80
+ # EnumStateMachine::Integrations.find_by_name(:invalid) # => EnumStateMachine::IntegrationNotFound: :invalid is an invalid integration
81
+ def self.find_by_name(name)
82
+ all.detect {|integration| integration.integration_name == name} || raise(IntegrationNotFound.new(name))
83
+ end
84
+
85
+ # Gets a list of all of the available integrations for use. This will
86
+ # always list the ActiveModel integration last.
87
+ #
88
+ # == Example
89
+ #
90
+ # EnumStateMachine::Integrations.all
91
+ # # => [EnumStateMachine::Integrations::ActiveRecord, EnumStateMachine::Integrations::ActiveModel]
92
+ def self.all
93
+ constants = self.constants.map {|c| c.to_s}.select {|c| c != 'ActiveModel'}.sort << 'ActiveModel'
94
+ constants.map {|c| const_get(c)}
95
+ end
96
+ end
97
+ end