Skip to content

Commit

Permalink
Merge pull request #10 from avispatech/silent-story
Browse files Browse the repository at this point in the history
Adds silent mode
  • Loading branch information
lomefin authored Mar 31, 2023
2 parents 57147a5 + 9c1ca5d commit be2517e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
## [Unreleased]

## [0.4.3] - 2023-03-29

- Adds silent mode, if a story is executed or initialized
with `silent_story: true` the `after_run` will not be executed.
This is meant to be used for additional, optional tasks, like
notifications.

- `after_run` is a different stage than run
## [0.4.2] - 2022-08-15

Adds rubocop rules
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
storyteller (0.4.2.2)
storyteller (0.4.3)
activesupport
smart_init

Expand Down Expand Up @@ -77,4 +77,4 @@ DEPENDENCIES
storyteller!

BUNDLED WITH
2.4.7
2.3.17
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Storyteller

version 0.4.2.2
version 0.4.3

Run user stories based on a simple DSL

Expand Down Expand Up @@ -83,7 +83,10 @@ Every Story **must** have at least on step to be able to execute, if no steps ar

Verification steps can be added to check if the Story has concluded successfully.

### Closure

If some activities are nice to have, but not necessary, i.e. Notifying a newly created
user, the `after_run`s may come in handy.

## Lifecycle

Expand All @@ -97,6 +100,8 @@ Verification steps can be added to check if the Story has concluded successfully
- `step`
- verification
- `done_criteria`
- closure
- `after_run`


## Helper methods
Expand Down Expand Up @@ -134,6 +139,41 @@ Require the gem, if needed

`require storyteller`

Create your Stories extending from `Storyteller::Story`

```
class CreateUser < Storyteller::Story
initialize_with :email, :name
step -> { @user = User.create(email:, name:) }
step -> { @user.add_role :member }
after_run :send_emails
def send_emails
Mailer.send_email(email:, template: :welcome)
end
end
```

Invoke your story

```
CreateUser.execute(email: '[email protected]', name: 'Example User')
# Or the new, and execute way
use_case = CreateUser.new(email: '[email protected]', name: 'Example User')
use_case.execute
```

### Silent mode

To disable the `after_run` method add the `silent_story: true` parameter.

`CreateUser.execute(email:, name:, silent_story: true)`



## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
16 changes: 12 additions & 4 deletions lib/storyteller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ class Error < StandardError; end

class Story
extend SmartInit

def self.initialize_with(*params, **harams)
new_harams = harams.merge({ silent_story: false })
super(*params, **new_harams)
end

is_callable method_name: :execute
include ActiveSupport::Callbacks
attr_reader :errors, :result

define_callbacks :init, :validation, :preparation, :run, :verification
define_callbacks :init, :validation, :preparation, :run, :after_run, :verification

set_callback :init, :after do
@stage = :initialized
Expand Down Expand Up @@ -88,9 +94,9 @@ def self.check(args = [], &)
# @note One callback at a time
def self.after_run(arg, &)
if block_given?
set_callback(:run, :after, &)
set_callback(:after_run, :after, &)
else
set_callback :run, :after, arg
set_callback :after_run, :after, arg
end
end

Expand Down Expand Up @@ -129,7 +135,7 @@ def initialized? = @stage != :initializing

def prepared? = %i[initializing initialized prepared].exclude? @stage

def execute
def execute # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
@stage ||= :initializing
@result = nil
@errors = []
Expand All @@ -145,6 +151,8 @@ def execute
run_callbacks :verification if errors.empty?
return self unless success?

run_callbacks :after_run unless @silent_story

self
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/storyteller/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Storyteller
VERSION = '0.4.2.2'
VERSION = '0.4.3'
end
29 changes: 28 additions & 1 deletion spec/storyteller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,38 @@ class FailedStepWithDoneCriteriaStory < Storyteller::Story
def check = spy.call
end

spy = spy('Thing') # rubocop:disable RSpec/VerifiedDoubles)
spy = spy('Thing') # rubocop:disable RSpec/VerifiedDoubles
expect(FailedStepWithDoneCriteriaStory.execute(spy:)).not_to be_success
expect(spy).not_to have_received(:call)
end
end
end
end

describe '#after_run' do
context 'when there silent_story is active' do
it do
class SilentStory < Storyteller::Story
initialize_with :spy, captcha: false, returns: :blank

step -> { @a ||= 1 }
step -> { @a += 1 }
after_run :call_spy

def call_spy
spy.call
end
end

spy = spy('Thing') # rubocop:disable RSpec/VerifiedDoubles
expect(SilentStory.execute(spy:, silent_story: true)).to be_success
expect(spy).not_to have_received(:call)
ss = SilentStory.new(spy:, silent_story: true)
ss.execute
expect(spy).not_to have_received(:call)
expect(SilentStory.execute(spy:)).to be_success
expect(spy).to have_received(:call)
end
end
end
end

0 comments on commit be2517e

Please sign in to comment.