urist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b8370d9d15bd2a284febc8530d2d7cba7c7f69e
4
+ data.tar.gz: f97e9d3dd1264f92c978fc45ca05678e5aa7194d
5
+ SHA512:
6
+ metadata.gz: 6f58af7ef728c2c8e9a456ca84ca3fdf963b68117938ce726859f0e12ca61194ce5ba30cc7af3adfb424193fb63f13078021a413a7a7d000654b62632b0dc044
7
+ data.tar.gz: 7df39f3102906aa1574d4787e0402688c357274286629559b5e63609f3b7c6a4e186bc95d19fefe8c578dee81545c2300849d6624de19ae9167748ce159a576f
data/lib/urist.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'urist/base'
2
+ require 'urist/uber_waffel'
3
+ require 'urist/runner'
4
+ require 'urist/scenario'
5
+
6
+ # == How to use: ==
7
+ #
8
+ # A. Fetch all the data you need:
9
+ # client = Client.needed_now # instance of some class
10
+ # price = 10000.65 # numbers
11
+ # products = client.products # arrays
12
+ #
13
+ # B. Assemble them into hash:
14
+ # information = {:client => client, :price => price, :products => products}
15
+ #
16
+ # C. Create new runner and, well, run it:
17
+ # runner = Urist.new(:startup_scenario => NewAndLongScenario, :information => information)
18
+ # result = runner.go!
19
+ #
20
+ # D. Extract the decision and other parameters from result:
21
+ # decision = result[:decision]
22
+ # description = result[:description]
23
+ # limit = result[:limit]
24
+ #
25
+ # E. Capture all execution logs
26
+ # log = runner.logs # array with all results
27
+ #
28
+ # F. Summarize all executed scenarios
29
+ # scenarios = runner.scenarios # array with all executed scenarios
30
+ #
31
+ # G. You're done!
32
+
33
+ module Urist
34
+ def self.new options={}
35
+ Base.new options
36
+ end
37
+ end
data/lib/urist/base.rb ADDED
@@ -0,0 +1,24 @@
1
+ module Urist
2
+ class Base
3
+ attr_accessor :runner
4
+
5
+ def initialize options={}
6
+ @runner = Runner.new options
7
+ end
8
+
9
+ # starts script execution
10
+ def go!
11
+ @runner.go!
12
+ end
13
+
14
+ # returns all performed scenarios
15
+ def scenarios
16
+ @runner.done_scenarios
17
+ end
18
+
19
+ # returns all results from all performed scenarios
20
+ def logs
21
+ @runner.log
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,62 @@
1
+ module Urist
2
+ class Runner
3
+ attr_accessor :information, :result, :log
4
+ attr_accessor :active_scenario, :done_scenarios
5
+
6
+ def initialize options={}
7
+ @information = options[:information] || {}
8
+ @active_scenario = options[:startup_scenario]
9
+ @done_scenarios = []
10
+ @log = []
11
+
12
+ validate!
13
+ end
14
+
15
+ def go!
16
+ run_active_scenario while @active_scenario
17
+
18
+ return @result
19
+ end
20
+
21
+ private
22
+ def run_active_scenario
23
+ check_for_duplicate_scenario!
24
+ execute_active_scenario!
25
+ validate_result!
26
+ log_result!
27
+ assign_next_scenario!
28
+ end
29
+
30
+ def assign_next_scenario!
31
+ decision = @result[:decision]
32
+ @active_scenario = @active_scenario.links[decision]
33
+ end
34
+
35
+ def log_result!
36
+ @log << @result
37
+ end
38
+
39
+ def validate_result!
40
+ raise ArgumentError, "Incorrect 'result' in #{@active_scenario.inspect}. Result must be returned as Hash." unless @result.is_a? Hash
41
+ raise ArgumentError, "Incorrect 'result' in #{@active_scenario.inspect}. Result must include ':decision' key." unless @result.has_key? :decision
42
+ end
43
+
44
+ def execute_active_scenario!
45
+ @result = @active_scenario.new(:information => @information).logic
46
+ end
47
+
48
+ # raise an exception if detected that scripts start looping
49
+ def check_for_duplicate_scenario!
50
+ if @done_scenarios.include? @active_scenario
51
+ raise RuntimeError, "Current scenario has already been passed. Current #{@active_scenario.inspect}. Previous #{@done_scenarios.last}"
52
+ else
53
+ @done_scenarios << @active_scenario
54
+ end
55
+ end
56
+
57
+ def validate!
58
+ raise ArgumentError, ":information should be assigned. #{@information.inspect} given." if @information.nil?
59
+ raise ArgumentError, ":startup_scenario should be assigned. #{@active_scenario.inspect} given." if @active_scenario.nil?
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,22 @@
1
+ module Urist
2
+ class Scenario
3
+ attr_accessor :information
4
+
5
+ # more candies and sugar for scenarios
6
+ include UberWaffel
7
+ inheritable_attributes :links
8
+ @links = {}
9
+
10
+ def initialize options={}
11
+ @information = options[:information]
12
+
13
+ validate!
14
+ end
15
+
16
+ private
17
+ def validate!
18
+ raise ArgumentError, ":information should be assigned to #{self.class}. #{@information.inspect} given." if @information.nil?
19
+ raise ArgumentError, "@links should be a Hash in #{self.class}. #{self.class.links.inspect} given." unless self.class.links.is_a? Hash
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ module Urist
2
+ module UberWaffel
3
+ # adds some metaprogramming magic:
4
+ # - removes the need for :attr_accessor for @links in every scenario
5
+ # since it's an inheritable attribute (with default value already set)
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def inheritable_attributes(*args)
12
+ @inheritable_attributes ||= [:inheritable_attributes]
13
+ @inheritable_attributes += args
14
+ args.each do |arg|
15
+ class_eval %(
16
+ class << self; attr_accessor :#{arg} end
17
+ )
18
+ end
19
+ @inheritable_attributes
20
+ end
21
+
22
+ def inherited(subclass)
23
+ @inheritable_attributes.each do |inheritable_attribute|
24
+ instance_var = "@#{inheritable_attribute}"
25
+ subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: urist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Kornienko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Library for creating of scenarios of business logic
14
+ email: send.this.to.moff@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/urist.rb
20
+ - lib/urist/base.rb
21
+ - lib/urist/runner.rb
22
+ - lib/urist/scenario.rb
23
+ - lib/urist/uber_waffel.rb
24
+ homepage:
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.0.6
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Scenario logic library
48
+ test_files: []