action_logic 0.2.3 → 0.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 583858aebac317fb33f490775442ecad7a40577d
4
- data.tar.gz: 839af981a8f86ad9da7de704738dddde65609007
3
+ metadata.gz: fb4115622aa03370f1c78ca57d8c45670d2c52bf
4
+ data.tar.gz: f1687c7f429d52c51369d91814cde3927a3eadd2
5
5
  SHA512:
6
- metadata.gz: 4c039cabbcfac0378afb5d30e9fd942d9ae9fee064c30f6e691526f455f29617a8c24ea7e765deb2edfb2878a153347b0ba762836a40a6ed89a3eb5404b6fc75
7
- data.tar.gz: a0cd7c262dc94adcd317db9d624621a3065bb0ac74c5a460a8ed331f29548f2b3aac2ad1649f1e2c6e8cbc74e67080413ae1ac244bf1c053fb99f6d4a0bd12c6
6
+ metadata.gz: 65e4f68bd7c54c1a838fdd81f399c9031c25a1409c423ad622d68203cbde71532f27b6df86a2c50437d6c0d23d360938df536b6593c550ce4580ea3302ac1054
7
+ data.tar.gz: 453fa766e4778a577fcea58ffdfc2c59b8fb256ca42322136874926f1f5bf56748aa9fc79998edbbaff6eec696c7e942e91d6b3e6034d7c13c3ca72580b5d171
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  coverage/
2
2
  *.gem
3
+ benchmark.log
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- action_logic (0.2.2)
4
+ action_logic (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/action_logic.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'action_logic/version'
2
1
  require 'action_logic/action_configuration'
3
2
  require 'action_logic/action_context'
4
3
  require 'action_logic/action_coordinator'
@@ -6,5 +5,8 @@ require 'action_logic/action_core'
6
5
  require 'action_logic/action_task'
7
6
  require 'action_logic/action_use_case'
8
7
  require 'action_logic/action_validation'
8
+ require 'action_logic/action_benchmark'
9
+ require 'action_logic/action_benchmark/default_formatter'
10
+
9
11
  require 'action_logic/errors'
10
- require 'benchmark'
12
+ require 'action_logic/version'
@@ -1,19 +1,38 @@
1
+ require 'benchmark'
2
+
1
3
  module ActionLogic
2
4
  module ActionBenchmark
3
5
 
4
6
  module ClassMethods
5
- def with_benchmark(class_context, &block)
6
- if ActionConfiguration.benchmark?
7
- context = nil
8
- benchmark_result = Benchmark.measure { context = block.call }
9
- ActionConfiguration.benchmark_log.printf("%60s: %15s %15s %15s %15s\n", "Context", "User Time", "System Time", "Total Time", "Real Time")
10
- ActionConfiguration.benchmark_log.printf("%60s: %15f %15f %15f %15f\n\n", class_context, benchmark_result.utime, benchmark_result.stime, benchmark_result.total, benchmark_result.real)
7
+ def with_benchmark(execution_context, &block)
8
+ if benchmark?
9
+ benchmark_result, context = benchmark!(&block)
10
+ log!(benchmark_result, execution_context)
11
11
  context
12
12
  else
13
13
  block.call
14
14
  end
15
15
  end
16
- end
17
16
 
17
+ private
18
+
19
+ def benchmark?
20
+ ActionConfiguration.benchmark?
21
+ end
22
+
23
+ def benchmark!(&block)
24
+ context = nil
25
+ benchmark_result = Benchmark.measure { context = block.call }
26
+ [benchmark_result, context]
27
+ end
28
+
29
+ def log!(benchmark_result, execution_context)
30
+ benchmark_formatter.send(execution_context.__private__type, benchmark_result, execution_context.name)
31
+ end
32
+
33
+ def benchmark_formatter
34
+ ActionConfiguration.benchmark_formatter
35
+ end
36
+ end
18
37
  end
19
38
  end
@@ -0,0 +1,30 @@
1
+ module ActionLogic
2
+ module ActionBenchmark
3
+ class DefaultFormatter
4
+ def initialize(benchmark_log: ActionConfiguration.benchmark_log)
5
+ @benchmark_log = benchmark_log
6
+ end
7
+
8
+ def format(benchmark_result, context_name)
9
+ benchmark_log.printf("%-10s %-50s %-10s %-10f %-10s %-10f %-10s %-10f %-10s %-10f\n",
10
+ "context:",
11
+ context_name,
12
+ "user_time:",
13
+ benchmark_result.utime,
14
+ "system_time:",
15
+ benchmark_result.stime,
16
+ "total_time:",
17
+ benchmark_result.total,
18
+ "real_time:",
19
+ benchmark_result.real)
20
+ end
21
+
22
+ alias_method :coordinator, :format
23
+ alias_method :use_case, :format
24
+ alias_method :task, :format
25
+
26
+ private
27
+ attr_reader :benchmark_log
28
+ end
29
+ end
30
+ end
@@ -18,8 +18,23 @@ module ActionLogic
18
18
  configuration_options.benchmark_log || $stdout
19
19
  end
20
20
 
21
+ def self.benchmark_formatter
22
+ custom_benchmark_formatter || default_formatter
23
+ end
24
+
21
25
  def self.reset!
22
26
  @configuration_options = OpenStruct.new
27
+ @custom_benchmark_formatter = nil
28
+ @default_formatter = nil
29
+ end
30
+
31
+ def self.custom_benchmark_formatter
32
+ @custom_benchmark_formatter ||= configuration_options.benchmark_formatter &&
33
+ configuration_options.benchmark_formatter.new
34
+ end
35
+
36
+ def self.default_formatter
37
+ @default_formatter ||= ::ActionLogic::ActionBenchmark::DefaultFormatter.new
23
38
  end
24
39
  end
25
40
  end
@@ -33,6 +33,10 @@ module ActionLogic
33
33
  execution_context.context
34
34
  end
35
35
  end
36
+
37
+ def __private__type
38
+ :coordinator
39
+ end
36
40
  end
37
41
  end
38
42
  end
@@ -15,6 +15,10 @@ module ActionLogic
15
15
  execution_context.context
16
16
  end
17
17
  end
18
+
19
+ def __private__type
20
+ :task
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -21,6 +21,10 @@ module ActionLogic
21
21
  end
22
22
  end
23
23
  end
24
+
25
+ def __private__type
26
+ :use_case
27
+ end
24
28
  end
25
29
  end
26
30
  end
@@ -1,3 +1,3 @@
1
1
  module ActionLogic
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ module ActionLogic::ActionBenchmark
4
+ describe DefaultFormatter do
5
+
6
+ let(:benchmark_log) { StringIO.new }
7
+ let(:benchmark_result) { double(:benchmark_result, utime: 0.00003, stime: 0.00002, total: 0.00001, real: 0.00030) }
8
+
9
+ subject { described_class.new(benchmark_log: benchmark_log) }
10
+
11
+ it "writes the benchmark result to the log for an ActionCoordinator" do
12
+ subject.coordinator(benchmark_result, "CoordinatorContext")
13
+ expect(benchmark_log.string).to\
14
+ eq "context: CoordinatorContext user_time: 0.000030 system_time: 0.000020 total_time: 0.000010 real_time: 0.000300 \n"
15
+ end
16
+
17
+ it "writes the benchmark result to the log for an ActionUseCase" do
18
+ subject.coordinator(benchmark_result, "UseCaseContext")
19
+ expect(benchmark_log.string).to\
20
+ eq "context: UseCaseContext user_time: 0.000030 system_time: 0.000020 total_time: 0.000010 real_time: 0.000300 \n"
21
+ end
22
+
23
+ it "writes the benchmark result to the log for an ActionTask" do
24
+ subject.coordinator(benchmark_result, "TaskContext")
25
+ expect(benchmark_log.string).to\
26
+ eq "context: TaskContext user_time: 0.000030 system_time: 0.000020 total_time: 0.000010 real_time: 0.000300 \n"
27
+ end
28
+ end
29
+ end
@@ -5,6 +5,10 @@ module ActionLogic
5
5
  describe ActionConfiguration do
6
6
  subject { described_class }
7
7
 
8
+ before do
9
+ described_class.reset!
10
+ end
11
+
8
12
  after do
9
13
  described_class.reset!
10
14
  end
@@ -38,5 +42,21 @@ module ActionLogic
38
42
  expect(described_class.benchmark_log).to eq(temp_file)
39
43
  end
40
44
  end
45
+
46
+ context "benchmark_formatter" do
47
+ it "uses default formatter if a custom formatter is not provided" do
48
+ expect(described_class.benchmark_formatter).to be_a(ActionLogic::ActionBenchmark::DefaultFormatter)
49
+ end
50
+
51
+ it "uses a custom formatter if one is provided" do
52
+ class CustomFormatter; end
53
+
54
+ described_class.configure do |config|
55
+ config.benchmark_formatter = CustomFormatter
56
+ end
57
+
58
+ expect(described_class.benchmark_formatter).to be_a(CustomFormatter)
59
+ end
60
+ end
41
61
  end
42
62
  end
@@ -5,6 +5,10 @@ require 'fixtures/custom_types'
5
5
 
6
6
  module ActionLogic
7
7
  describe ActionCoordinator do
8
+ it "knows its type" do
9
+ expect(TestCoordinator1.__private__type).to eq(:coordinator)
10
+ end
11
+
8
12
  context "no failures and no halts" do
9
13
  it "evaluates all use cases defined by the state transition plan" do
10
14
  result = TestCoordinator1.execute()
@@ -4,6 +4,9 @@ require 'fixtures/tasks'
4
4
 
5
5
  module ActionLogic
6
6
  describe ActionTask do
7
+ it "knows its type" do
8
+ expect(SimpleTestTask.__private__type).to eq(:task)
9
+ end
7
10
 
8
11
  it "returns an instance of ActionContext" do
9
12
  result = SimpleTestTask.execute()
@@ -5,6 +5,9 @@ require 'fixtures/custom_types'
5
5
 
6
6
  module ActionLogic
7
7
  describe ActionUseCase do
8
+ it "knows its type" do
9
+ expect(SimpleTestUseCase.__private__type).to eq(:use_case)
10
+ end
8
11
 
9
12
  it "returns an instance of ActionContext" do
10
13
  result = SimpleTestUseCase.execute()
@@ -1,6 +1,7 @@
1
1
  require 'action_logic'
2
2
  require 'fixtures/constants'
3
3
 
4
+ # :nocov:
4
5
  class TestCoordinator1
5
6
  include ActionLogic::ActionCoordinator
6
7
 
@@ -562,3 +563,4 @@ class FailureTestTask3
562
563
  context.fail!
563
564
  end
564
565
  end
566
+ # :nocov:
@@ -1,7 +1,7 @@
1
1
  require 'action_logic'
2
2
  require 'fixtures/custom_types'
3
3
  require 'fixtures/constants'
4
-
4
+ # :nocov:
5
5
  class SimpleTestTask
6
6
  include ActionLogic::ActionTask
7
7
 
@@ -284,3 +284,4 @@ class HaltTestTask
284
284
  context.halt!(Constants::HALT_MESSAGE)
285
285
  end
286
286
  end
287
+ # :nocov:
@@ -2,6 +2,7 @@ require 'action_logic'
2
2
  require 'fixtures/tasks'
3
3
  require 'fixtures/constants'
4
4
 
5
+ # :nocov:
5
6
  class SimpleTestUseCase
6
7
  include ActionLogic::ActionUseCase
7
8
 
@@ -416,3 +417,4 @@ class UseCaseHaltTestTask
416
417
  context.halt!(Constants::HALT_MESSAGE)
417
418
  end
418
419
  end
420
+ # :nocov:
data/spec/spec_helper.rb CHANGED
@@ -9,8 +9,15 @@ $LOAD_PATH << File.join(File.dirname(__FILE__))
9
9
 
10
10
  require 'action_logic'
11
11
 
12
+ if ENV['BENCHMARK']
13
+ ActionLogic::ActionConfiguration.configure do |config|
14
+ config.benchmark = true
15
+ config.benchmark_log = File.open("benchmark.log", "w")
16
+ end
17
+ end
18
+
12
19
  RSpec.configure do |c|
13
- #c.fail_fast = true
20
+ c.fail_fast = true
14
21
  c.color = true
15
22
  c.formatter = 'documentation'
16
23
  c.order = 'rand'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Winfrey
@@ -80,6 +80,7 @@ files:
80
80
  - action_logic.gemspec
81
81
  - lib/action_logic.rb
82
82
  - lib/action_logic/action_benchmark.rb
83
+ - lib/action_logic/action_benchmark/default_formatter.rb
83
84
  - lib/action_logic/action_configuration.rb
84
85
  - lib/action_logic/action_context.rb
85
86
  - lib/action_logic/action_coordinator.rb
@@ -99,6 +100,7 @@ files:
99
100
  - resources/action_use_case_diagram.png
100
101
  - resources/diagrams.sketch
101
102
  - resources/overview_diagram.png
103
+ - spec/action_logic/action_benchmark/default_formatter_spec.rb
102
104
  - spec/action_logic/action_configuration_spec.rb
103
105
  - spec/action_logic/action_context_spec.rb
104
106
  - spec/action_logic/action_coordinator_spec.rb
@@ -135,6 +137,7 @@ signing_key:
135
137
  specification_version: 4
136
138
  summary: Business logic abstraction
137
139
  test_files:
140
+ - spec/action_logic/action_benchmark/default_formatter_spec.rb
138
141
  - spec/action_logic/action_configuration_spec.rb
139
142
  - spec/action_logic/action_context_spec.rb
140
143
  - spec/action_logic/action_coordinator_spec.rb