statum 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52f0fb1eba87da4ce2d080b1256d3929628c2a05
4
+ data.tar.gz: 10871eafd35c6dd767ec219d0f4fcdf6342d7cae
5
+ SHA512:
6
+ metadata.gz: 4cfe42b84a4b55fad13213eb792f5b96a19cac90f7b2236892db35fa3eb3320a2dc3be897f8e7b5f9b50576d583309383206d123889e4239e729f284a79d8386
7
+ data.tar.gz: d5442a1c6fb819574e3bf1ec13258868ea7f5dac6c836bfc902ce8fbbce1ecd64f6e65d59c440fe63f9b916b1b8d8096ce423599ce6c65656159b9ffef0fcb72
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
4
+ --require pry
@@ -0,0 +1,58 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - db/schema.rb
5
+ - config/**/*.rb
6
+
7
+ Rails:
8
+ Enabled: true
9
+
10
+ Rails/HasAndBelongsToMany:
11
+ Enabled: false
12
+
13
+ # Bundler
14
+ Bundler/OrderedGems:
15
+ Enabled: false
16
+
17
+ # Metrics
18
+ Metrics/CyclomaticComplexity:
19
+ Enabled: true
20
+ Max: 6
21
+
22
+ Metrics/LineLength:
23
+ Enabled: true
24
+ Max: 100
25
+ AllowHeredoc: true
26
+ IgnoredPatterns: ['#.*']
27
+
28
+ Metrics/BlockLength:
29
+ Enabled: false
30
+
31
+ Metrics/MethodLength:
32
+ Enabled: false
33
+
34
+ Metrics/AbcSize:
35
+ Enabled: false
36
+
37
+ # Style
38
+ Style/StringLiterals:
39
+ Enabled: false
40
+
41
+ Style/Documentation:
42
+ Enabled: false
43
+
44
+ Style/ClassAndModuleChildren:
45
+ Enabled: false
46
+
47
+ Style/FrozenStringLiteralComment:
48
+ Enabled: false
49
+
50
+ Style/RegexpLiteral:
51
+ Enabled: false
52
+
53
+ # Lint
54
+ Lint/AmbiguousOperator:
55
+ Enabled: false
56
+
57
+ Lint/AmbiguousBlockAssociation:
58
+ Enabled: false
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ before_install: gem install bundler
6
+ script: bundle exec rspec
7
+ rvm:
8
+ - 2.4.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in statum.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+ # Statum
2
+
3
+ ![Build Status](https://travis-ci.org/nulldef/statum.svg?branch=master)
4
+ [![Coverage Status](https://coveralls.io/repos/github/nulldef/statum/badge.svg?branch=master)](https://coveralls.io/github/nulldef/statum?branch=master&v=1)
5
+
6
+ Finite state machine for your objects
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'statum'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install statum
23
+
24
+ ## Usage
25
+
26
+ ### Basic usage
27
+ Statum provides a DSL for defining state machine for your class
28
+
29
+ ```ruby
30
+ class Car
31
+ include Statum
32
+
33
+ attr_accessor :state
34
+
35
+ statum :state, initial: :idle do
36
+ state :idle
37
+ state :riding
38
+
39
+ event :ride, idle: :riding
40
+ end
41
+ end
42
+ ```
43
+
44
+ Then `Car` object will receive following methods:
45
+ ```ruby
46
+ car = Car.new
47
+ car.idle? # => true
48
+ car.riding? # => false
49
+ car.ride! # changes idle state to riding
50
+ ```
51
+
52
+ ### Hooks
53
+ You can be able to execute some procs before and after event will be fired
54
+
55
+ ```ruby
56
+ class Car
57
+ include Statum
58
+
59
+ attr_accessor :state, :started
60
+
61
+ statum :state, initial: :idle do
62
+ state :idle
63
+ state :riding
64
+
65
+ event :ride, idle: :riding,
66
+ before: -> { self.started = true },
67
+ after: :stop_engine
68
+ end
69
+
70
+ def stop_engine
71
+ self.started = false
72
+ end
73
+ end
74
+ ```
75
+
76
+ And then before state changes will be executed `before` proc, and after
77
+ changing - `after` proc (in instance context).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nulldef/statum.
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "statum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,54 @@
1
+ require "statum/version"
2
+ require "statum/state_definer"
3
+ require "statum/machine"
4
+ require "statum/event"
5
+
6
+ module Statum
7
+ UnknownEventError = Class.new(ArgumentError)
8
+ ErrorTransitionError = Class.new(StandardError)
9
+
10
+ class << self
11
+ def included(base)
12
+ base.extend(Statum::ClassMethods)
13
+ base.include(Statum::InstanceMethods)
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ def statum(field, options = {}, &block)
19
+ definer = Statum::StateDefiner.new(self, field, options)
20
+ definer.instance_eval(&block) if block_given?
21
+ instance_variable_set('@__statum_machine', definer.state_machine)
22
+ end
23
+
24
+ def state_machine
25
+ instance_variable_get('@__statum_machine')
26
+ end
27
+ end
28
+
29
+ module InstanceMethods
30
+ def method_missing(meth, *args)
31
+ if meth.to_s.end_with?('?') && state_machine.state?(meth[0...-1])
32
+ state_machine.current(self) == meth[0...-1].to_sym
33
+ elsif meth.to_s.end_with?('!') && state_machine.event?(meth[0...-1])
34
+ state_machine.fire!(self, meth[0...-1])
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def respond_to_missing?(meth, *args)
41
+ if meth.to_s.end_with?('?')
42
+ state_machine.state?(meth[0...-1])
43
+ elsif meth.to_s.end_with?('!')
44
+ state_machine.event?(meth[0...-1])
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ def state_machine
51
+ self.class.state_machine
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ module Statum
2
+ # Class for storing event info
3
+ class Event
4
+ attr_reader :from, :to, :before, :after
5
+
6
+ # Creates an event class
7
+ #
8
+ # @param [String|Symbol] from From state name
9
+ # @param [String|Symbol] to To state name
10
+ # @param [Hash] options Options for event
11
+ def initialize(from, to, options = {})
12
+ @from = from
13
+ @to = to
14
+ @before = options.fetch(:before, nil)&.to_proc
15
+ @after = options.fetch(:after, nil)&.to_proc
16
+ end
17
+
18
+ # Check if before hook exists
19
+ # @return [boolean]
20
+ def before?
21
+ !before.nil?
22
+ end
23
+
24
+ # Checks if after hook present
25
+ # @return [boolean]
26
+ def after?
27
+ !after.nil?
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,62 @@
1
+ module Statum
2
+ # Class for representing event machine
3
+ class Machine
4
+ attr_reader :events, :states, :field
5
+
6
+ # Creates machine instance
7
+ #
8
+ # @param [Hash] options options hash
9
+ def initialize(options)
10
+ @field = options.delete(:field)
11
+ @initial = options.delete(:initial)
12
+ @states = options.delete(:states)
13
+ @events = options.delete(:events)
14
+ end
15
+
16
+ # Checks if state present
17
+ #
18
+ # @param [String|Symbol] name state name
19
+ #
20
+ # @return [Boolean]
21
+ def state?(name)
22
+ @states.include?(name.to_sym)
23
+ end
24
+
25
+ # Checks if event present
26
+ #
27
+ # @param [String|Boolean] name event name
28
+ #
29
+ # @return [Boolean]
30
+ def event?(name)
31
+ @events.keys.include?(name.to_sym)
32
+ end
33
+
34
+ # Execute an event
35
+ #
36
+ # @param [Object] instance Instance of class, that includes Statum
37
+ # @param [String|Symbol] name Event name
38
+ def fire!(instance, name)
39
+ raise Statum::UnknownEventError, "Event #{name} not found" unless event?(name)
40
+
41
+ current_state = current(instance)
42
+ event = events[name.to_sym]
43
+
44
+ if event.from != current_state
45
+ raise Statum::ErrorTransitionError, "Cannot transition from #{current_state} to #{event.to}"
46
+ end
47
+
48
+ instance.instance_eval(&event.before) if event.before?
49
+ instance.send("#{field}=", event.to)
50
+ instance.instance_eval(&event.after) if event.after?
51
+ end
52
+
53
+ # Returns current state of instance
54
+ #
55
+ # @param [Object] instance Instance of class
56
+ #
57
+ # @return [Symbol] Current instance's state
58
+ def current(instance)
59
+ instance.send(field)&.to_sym || @initial
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,51 @@
1
+ module Statum
2
+ # Class for create hash options for machine
3
+ class StateDefiner
4
+ # Creates an instance of definer
5
+ #
6
+ # @param [Class] klass Class that includes Statum
7
+ # @param [String|Symbol] field Field that will be used for storing current state
8
+ # @param [Hash] options Hash options
9
+ def initialize(klass, field, options)
10
+ @klass = klass
11
+ @field = field.to_sym
12
+ @initial = options.fetch(:initial, nil)
13
+ @states = []
14
+ @events = {}
15
+
16
+ state(@initial) unless @initial.nil?
17
+ end
18
+
19
+ # Returns state maching
20
+ #
21
+ # @return [Statum::Machine]
22
+ def state_machine
23
+ Statum::Machine.new(
24
+ field: @field,
25
+ initial: @initial,
26
+ states: @states,
27
+ events: @events
28
+ )
29
+ end
30
+
31
+ # Define a new state
32
+ #
33
+ # @param [String|Symbol] name State name
34
+ def state(name)
35
+ @states << name.to_sym unless @states.include?(name.to_sym)
36
+ end
37
+
38
+ # Define a new event
39
+ #
40
+ # @param [String|Symbol] name Event name
41
+ # @param [Hash] options Options hash
42
+ # First key-value pair must be 'from' and 'to' transition states
43
+ # Other pairs are event options
44
+ def event(name, options)
45
+ return if @events.key?(name.to_sym)
46
+
47
+ from, to = options.shift
48
+ @events[name.to_sym] = Statum::Event.new(from, to, options)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Statum
2
+ VERSION = "0.2.0".freeze
3
+ end
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "statum/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "statum"
7
+ spec.version = Statum::VERSION
8
+ spec.authors = ["Alexey Bespalov"]
9
+ spec.email = ["alex.coder1@gmail.com"]
10
+
11
+ spec.summary = "Ruby gem to control your states"
12
+ spec.description = "Ruby state machine"
13
+ spec.homepage = "https://github.com/nulldef/statum"
14
+ spec.license = "MIT"
15
+
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "coveralls", "~> 0.8"
26
+ spec.add_development_dependency "pry", "~> 0.11"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Bespalov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Ruby state machine
84
+ email:
85
+ - alex.coder1@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/statum.rb
101
+ - lib/statum/event.rb
102
+ - lib/statum/machine.rb
103
+ - lib/statum/state_definer.rb
104
+ - lib/statum/version.rb
105
+ - statum.gemspec
106
+ homepage: https://github.com/nulldef/statum
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.8
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Ruby gem to control your states
130
+ test_files: []