screengem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.rubocop +1 -0
  5. data/.rubocop.yml +58 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +5 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +6 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +33 -0
  12. data/Rakefile +6 -0
  13. data/bin/console +14 -0
  14. data/bin/screengem +8 -0
  15. data/bin/setup +8 -0
  16. data/lib/screengem/action.rb +9 -0
  17. data/lib/screengem/actor.rb +86 -0
  18. data/lib/screengem/browser_action.rb +7 -0
  19. data/lib/screengem/cli.rb +32 -0
  20. data/lib/screengem/concerns/actionable.rb +29 -0
  21. data/lib/screengem/concerns/configurable.rb +18 -0
  22. data/lib/screengem/concerns/dampenable.rb +45 -0
  23. data/lib/screengem/concerns/executable.rb +15 -0
  24. data/lib/screengem/configuration.rb +63 -0
  25. data/lib/screengem/dampen_configuration.rb +85 -0
  26. data/lib/screengem/dampen_configuration_generator.rb +57 -0
  27. data/lib/screengem/dsl.rb +14 -0
  28. data/lib/screengem/factories/action_factory.rb +34 -0
  29. data/lib/screengem/factories/browser_action_factory.rb +14 -0
  30. data/lib/screengem/factories/factory_creation_error.rb +18 -0
  31. data/lib/screengem/factories/question_factory.rb +38 -0
  32. data/lib/screengem/factories/standard_action_factory.rb +14 -0
  33. data/lib/screengem/factories/task_factory.rb +38 -0
  34. data/lib/screengem/feature_page.rb +8 -0
  35. data/lib/screengem/incorrect_answer.rb +23 -0
  36. data/lib/screengem/page_references.rb +42 -0
  37. data/lib/screengem/primitive_key.rb +10 -0
  38. data/lib/screengem/question.rb +16 -0
  39. data/lib/screengem/screengem_error.rb +3 -0
  40. data/lib/screengem/task.rb +19 -0
  41. data/lib/screengem/version.rb +3 -0
  42. data/lib/screengem.rb +47 -0
  43. data/screengem.gemspec +34 -0
  44. metadata +212 -0
@@ -0,0 +1,57 @@
1
+ module Screengem
2
+ #
3
+ # Knows how to generate the sample dampen configuration.
4
+ #
5
+ class DampenConfigurationGenerator
6
+ include Screengem::PrimitiveKey
7
+
8
+ # Seconds to sleep after executing a question or task.
9
+ DEFAULT_SLEEP = 2
10
+
11
+ def generate
12
+ configuration = empty_configuration
13
+
14
+ default_configuration = configuration["default"]
15
+
16
+ default_configuration["questions"] = all_questions
17
+ default_configuration["tasks"] = all_tasks
18
+
19
+ configuration
20
+ end
21
+
22
+ private
23
+
24
+ def empty_configuration
25
+ {
26
+ "default" => {
27
+ "questions" => {},
28
+ "tasks" => {}
29
+ }
30
+ }
31
+ end
32
+
33
+ def all_questions
34
+ all_primitives(Screengem::Question)
35
+ end
36
+
37
+ def all_tasks
38
+ all_primitives(Screengem::Task)
39
+ end
40
+
41
+ def all_primitives(primitive_root)
42
+ sorted_descendents(primitive_root).each_with_object({}) do |primitive_class, memo|
43
+ next unless apply_dampening?(primitive_class)
44
+
45
+ memo[primitive_key(primitive_class.name)] = DEFAULT_SLEEP
46
+ end
47
+ end
48
+
49
+ def sorted_descendents(primitive_root)
50
+ primitive_root.descendants.sort_by { |primitive_class| primitive_key(primitive_class.name) }
51
+ end
52
+
53
+ def apply_dampening?(primitive_class)
54
+ primitive_class.supports_dampening? && primitive_class.descendants.empty?
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,14 @@
1
+ module Screengem
2
+ #
3
+ # Mixin that makes tasks and questions available.
4
+ #
5
+ module DSL
6
+ def question
7
+ Screengem::Factories::QuestionFactory.instance
8
+ end
9
+
10
+ def task
11
+ Screengem::Factories::TaskFactory.instance
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ module Screengem
2
+ module Factories
3
+ #
4
+ # Makes actions available to tasks.
5
+ #
6
+ class ActionFactory < BasicObject
7
+ def method_missing(action_name, *args) # rubocop:disable Style/MethodMissingSuper
8
+ action_class_name = raw_action_class_name(action_name).camelize
9
+ action_class = "#{action_scope}::#{action_class_name}".constantize
10
+
11
+ action_class.new(*args)
12
+ rescue => e
13
+ ActionError.new(<<~MSG)
14
+ Unable to create action: '#{action_class_name}'.
15
+ Details: #{e.message}
16
+ MSG
17
+ end
18
+
19
+ def respond_to_missing?(_action_name, *)
20
+ true
21
+ end
22
+
23
+ class ActionError < ::Screengem::Action
24
+ include FactoryCreationError
25
+ end
26
+
27
+ private
28
+
29
+ def action_scope
30
+ @action_scope ||= ::Screengem.configuration.action_scope
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ module Screengem
2
+ module Factories
3
+ #
4
+ # Makes browser actions available.
5
+ #
6
+ class BrowserActionFactory < Screengem::Factories::ActionFactory
7
+ include ::Singleton
8
+
9
+ def raw_action_class_name(action_name)
10
+ "#{action_name}_action_browser"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Screengem
2
+ module Factories
3
+ #
4
+ # Knows the behaviour for error creation primitives.
5
+ #
6
+ module FactoryCreationError
7
+ attr_reader :error_message
8
+
9
+ def initialize(error_message)
10
+ @error_message = error_message
11
+ end
12
+
13
+ def execute
14
+ raise Screengem::ScreengemError, error_message
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ module Screengem
2
+ module Factories
3
+ #
4
+ # Knows how to create a Question from a question name.
5
+ #
6
+ class QuestionFactory < BasicObject
7
+ include ::Singleton
8
+
9
+ def method_missing(question_name, *args) # rubocop:disable Style/MethodMissingSuper
10
+ question_class_name = "#{question_name}_question".camelize
11
+ question_class = "#{question_scope}::#{question_class_name}".constantize
12
+
13
+ question_class.new(*args)
14
+ rescue => e
15
+ QuestionError.new(<<~MSG)
16
+ Unable to create question: '#{question_class_name}'.
17
+ Details: #{e.message}
18
+ MSG
19
+ end
20
+
21
+ def respond_to_missing?(_question_name, *)
22
+ true
23
+ end
24
+
25
+ class QuestionError < ::Screengem::Question
26
+ include FactoryCreationError
27
+
28
+ skip_dampening
29
+ end
30
+
31
+ private
32
+
33
+ def question_scope
34
+ @question_scope ||= ::Screengem.configuration.question_scope
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module Screengem
2
+ module Factories
3
+ #
4
+ # Makes standard actions available.
5
+ #
6
+ class StandardActionFactory < Screengem::Factories::ActionFactory
7
+ include ::Singleton
8
+
9
+ def raw_action_class_name(action_name)
10
+ "#{action_name}_action"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module Screengem
2
+ module Factories
3
+ #
4
+ # Knows how to create a Task from a task name.
5
+ #
6
+ class TaskFactory < BasicObject
7
+ include ::Singleton
8
+
9
+ def method_missing(task_name, *args) # rubocop:disable Style/MethodMissingSuper
10
+ task_class_name = "#{task_name}_task".camelize
11
+ task_class = "#{task_scope}::#{task_class_name}".constantize
12
+
13
+ task_class.new(*args)
14
+ rescue => e
15
+ TaskError.new(<<~MSG)
16
+ Unable to create task: '#{task_class_name}'.
17
+ Details: #{e.message}
18
+ MSG
19
+ end
20
+
21
+ def respond_to_missing?(_task_name, *)
22
+ true
23
+ end
24
+
25
+ class TaskError < ::Screengem::Task
26
+ include FactoryCreationError
27
+
28
+ skip_dampening
29
+ end
30
+
31
+ private
32
+
33
+ def task_scope
34
+ @task_scope ||= ::Screengem.configuration.task_scope
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module Screengem
2
+ #
3
+ # Base class for all feature pages.
4
+ #
5
+ class FeaturePage
6
+ include Capybara::DSL
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module Screengem
2
+ #
3
+ # Common error to signal a question that was answered incorrectly.
4
+ #
5
+ class IncorrectAnswer < RuntimeError
6
+ def initialize(question)
7
+ super(extract_question_text(question))
8
+ end
9
+
10
+ private
11
+
12
+ #
13
+ # Transforms question class name into a text message.
14
+ #
15
+ # Example:
16
+ #
17
+ # Questions::SignatureCapturedQuestion is transformed to 'Signature captured?'.
18
+ #
19
+ def extract_question_text(question)
20
+ question.class.name.demodulize.underscore.humanize.gsub(" question", "?")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ module Screengem
2
+ #
3
+ # Mixin that gives the ability to get references to page objects.
4
+ #
5
+
6
+ # rubocop:disable Style/GuardClause
7
+ module PageReferences
8
+ def self.extended(_mod)
9
+ # One time definition of the page object references.
10
+ return unless PageReferences.instance_methods.empty?
11
+
12
+ #
13
+ # Generate a factory method for each page object we can find.
14
+ #
15
+ # Example:
16
+ #
17
+ # def login_page
18
+ # @login_page ||= Pages::LoginPage.new
19
+ # end
20
+ #
21
+ generated_method_names = []
22
+
23
+ Screengem::FeaturePage.descendants.each do |page_object|
24
+ class_name = page_object.name
25
+ method_name = class_name.demodulize.underscore
26
+
27
+ if generated_method_names.include?(method_name)
28
+ raise "Name collision: two page objects resolve to '#{method_name}'."
29
+ else
30
+ generated_method_names << method_name
31
+ end
32
+
33
+ module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
34
+ def #{method_name}
35
+ @#{method_name} ||= #{class_name}.new
36
+ end
37
+ RUBY
38
+ end
39
+ end
40
+ end
41
+ # rubocop:enable Style/GuardClause
42
+ end
@@ -0,0 +1,10 @@
1
+ module Screengem
2
+ #
3
+ # Mixin that knows how to generate the primitive key for a dampening configuration.
4
+ #
5
+ module PrimitiveKey
6
+ def primitive_key(primitive_class_name)
7
+ Screengem::DampenConfiguration.instance.dampen_configuration_key(primitive_class_name)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Screengem
2
+ #
3
+ # Base class for all questions.
4
+ #
5
+ class Question
6
+ include Screengem::Configurable
7
+ include Screengem::Dampenable
8
+ include Screengem::Executable
9
+
10
+ dampen_scope :questions
11
+
12
+ def answer
13
+ execute.tap { sleep(seconds_to_dampen) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Screengem
2
+ ScreengemError = Class.new(RuntimeError)
3
+ end
@@ -0,0 +1,19 @@
1
+ module Screengem
2
+ #
3
+ # Base class for all tasks.
4
+ #
5
+ class Task
6
+ include Screengem::Actionable
7
+ include Screengem::Configurable
8
+ include Screengem::Dampenable
9
+ include Screengem::Executable
10
+
11
+ include Screengem::DSL
12
+
13
+ dampen_scope :tasks
14
+
15
+ def perform
16
+ execute.tap { sleep(seconds_to_dampen) }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Screengem
2
+ VERSION = "0.1.0"
3
+ end
data/lib/screengem.rb ADDED
@@ -0,0 +1,47 @@
1
+ require "singleton"
2
+ require "yaml"
3
+
4
+ require "active_support"
5
+ require "active_support/core_ext"
6
+
7
+ require "capybara/dsl"
8
+
9
+ require "screengem/feature_page"
10
+ require "screengem/incorrect_answer"
11
+ require "screengem/screengem_error"
12
+
13
+ require "screengem/configuration"
14
+ require "screengem/primitive_key"
15
+ require "screengem/dampen_configuration"
16
+ require "screengem/dampen_configuration_generator"
17
+
18
+ require "screengem/concerns/actionable"
19
+ require "screengem/concerns/configurable"
20
+ require "screengem/concerns/dampenable"
21
+ require "screengem/concerns/executable"
22
+
23
+ require "screengem/dsl"
24
+
25
+ require "screengem/action"
26
+ require "screengem/browser_action"
27
+ require "screengem/question"
28
+ require "screengem/task"
29
+
30
+ require "screengem/actor"
31
+
32
+ require "screengem/factories/factory_creation_error"
33
+
34
+ require "screengem/factories/action_factory"
35
+ require "screengem/factories/browser_action_factory"
36
+ require "screengem/factories/standard_action_factory"
37
+ require "screengem/factories/question_factory"
38
+ require "screengem/factories/task_factory"
39
+
40
+ require "screengem/page_references"
41
+
42
+ require "screengem/cli"
43
+
44
+ require "screengem/version"
45
+
46
+ module Screengem
47
+ end
data/screengem.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "screengem/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "screengem"
7
+ spec.version = Screengem::VERSION
8
+ spec.authors = ["Alistair McKinnell"]
9
+ spec.email = ["alistairm@nulogy.com"]
10
+
11
+ spec.summary = "Ruby implementation of the Screengem Pattern."
12
+ spec.homepage = "https://github.com/nulogy/screengem"
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| 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_runtime_dependency "activesupport", ">= 4.2"
25
+ spec.add_runtime_dependency "capybara", "~> 2.18"
26
+ spec.add_runtime_dependency "require_all", "~> 2.0"
27
+ spec.add_runtime_dependency "thor", ">= 0.20"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 12.0"
31
+ spec.add_development_dependency "rspec", "~> 3.8"
32
+ spec.add_development_dependency "rubocop", "~> 0.61"
33
+ spec.add_development_dependency "rubocop-rspec", "~> 1.30"
34
+ end
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: screengem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alistair McKinnell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: capybara
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.18'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.18'
41
+ - !ruby/object:Gem::Dependency
42
+ name: require_all
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0.20'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0.20'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '12.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '12.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.8'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.61'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.61'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.30'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.30'
139
+ description:
140
+ email:
141
+ - alistairm@nulogy.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop"
149
+ - ".rubocop.yml"
150
+ - ".ruby-version"
151
+ - ".travis.yml"
152
+ - CODE_OF_CONDUCT.md
153
+ - Gemfile
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/screengem
159
+ - bin/setup
160
+ - lib/screengem.rb
161
+ - lib/screengem/action.rb
162
+ - lib/screengem/actor.rb
163
+ - lib/screengem/browser_action.rb
164
+ - lib/screengem/cli.rb
165
+ - lib/screengem/concerns/actionable.rb
166
+ - lib/screengem/concerns/configurable.rb
167
+ - lib/screengem/concerns/dampenable.rb
168
+ - lib/screengem/concerns/executable.rb
169
+ - lib/screengem/configuration.rb
170
+ - lib/screengem/dampen_configuration.rb
171
+ - lib/screengem/dampen_configuration_generator.rb
172
+ - lib/screengem/dsl.rb
173
+ - lib/screengem/factories/action_factory.rb
174
+ - lib/screengem/factories/browser_action_factory.rb
175
+ - lib/screengem/factories/factory_creation_error.rb
176
+ - lib/screengem/factories/question_factory.rb
177
+ - lib/screengem/factories/standard_action_factory.rb
178
+ - lib/screengem/factories/task_factory.rb
179
+ - lib/screengem/feature_page.rb
180
+ - lib/screengem/incorrect_answer.rb
181
+ - lib/screengem/page_references.rb
182
+ - lib/screengem/primitive_key.rb
183
+ - lib/screengem/question.rb
184
+ - lib/screengem/screengem_error.rb
185
+ - lib/screengem/task.rb
186
+ - lib/screengem/version.rb
187
+ - screengem.gemspec
188
+ homepage: https://github.com/nulogy/screengem
189
+ licenses:
190
+ - MIT
191
+ metadata: {}
192
+ post_install_message:
193
+ rdoc_options: []
194
+ require_paths:
195
+ - lib
196
+ required_ruby_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubyforge_project:
208
+ rubygems_version: 2.7.7
209
+ signing_key:
210
+ specification_version: 4
211
+ summary: Ruby implementation of the Screengem Pattern.
212
+ test_files: []