steppy 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e1028f03816d488f2dc95fa32b6735e9ee991143a4b50d29a6d26e2464c1514
4
- data.tar.gz: 495da8ede3f4163b17ddfc41ff5fd6c5cdece3a05302515ab4d678581e154e0f
3
+ metadata.gz: 8e5453b4c6d2b926fbb62e294acf91d2a6620628d638120a4891211a425e3573
4
+ data.tar.gz: 9d49834c0e7767236fe25f4ae37f9ad1c48ac04d7c4a8a430b5b9b3a91f1354e
5
5
  SHA512:
6
- metadata.gz: 74a8ae97737d34d4490ae84a681dc49003c3f780c6a024af2d797dd72ce9142acea49cbb15a53bbad66f0b0e4e98633d159ffaf5c741abb0479573ebad27e0e7
7
- data.tar.gz: ed69ffa9dc400b3377f4b4e9aeb651b82ade6cd12e281c49beb6899bcc288836734f21cac026f25ed85a8a9fbfcad9c94be193af6f1df93b9d2dfe783baccc69
6
+ metadata.gz: 86ce9ff1d59ca081127c6beb3ca7e065d0f2dc973ba97bbfae3f1f0704f6e5cee490e48d4035a63f460aed5f8a77e22ed589a67c4420b9957ee52c233cac64e4
7
+ data.tar.gz: 984959949968858249dea683dc5ef6218b099503ec00b1dcbddf47885f89585db18f125b0bc53e70897633637036357487084d38bb7cd6c69ac44bf3bcf68fbf
@@ -16,3 +16,9 @@ Style/StringLiterals:
16
16
 
17
17
  AllCops:
18
18
  TargetRubyVersion: 2.5
19
+
20
+ Airbnb/ClassOrModuleDeclaredInWrongFile:
21
+ Enabled: false
22
+
23
+ Airbnb/ModuleMethodInWrongFile:
24
+ Enabled: false
data/Gemfile CHANGED
@@ -6,3 +6,5 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  # Specify your gem's dependencies in steppy.gemspec
8
8
  gemspec
9
+
10
+ gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- steppy (0.0.1)
4
+ steppy (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -11,14 +11,17 @@ GEM
11
11
  descendants_tracker (~> 0.0.4)
12
12
  ice_nine (~> 0.11.0)
13
13
  thread_safe (~> 0.3, >= 0.3.1)
14
+ byebug (10.0.2)
14
15
  codeclimate-engine-rb (0.4.1)
15
16
  virtus (~> 1.0)
16
17
  coercible (1.0.0)
17
18
  descendants_tracker (~> 0.0.1)
18
19
  descendants_tracker (0.0.4)
19
20
  thread_safe (~> 0.3, >= 0.3.1)
21
+ docile (1.3.1)
20
22
  equalizer (0.0.11)
21
23
  ice_nine (0.11.2)
24
+ json (2.1.0)
22
25
  minitest (5.11.3)
23
26
  parallel (1.12.1)
24
27
  parser (2.5.1.0)
@@ -43,6 +46,11 @@ GEM
43
46
  rubocop-rspec (1.22.1)
44
47
  rubocop (>= 0.52.1)
45
48
  ruby-progressbar (1.9.0)
49
+ simplecov (0.16.1)
50
+ docile (~> 1.1)
51
+ json (>= 1.8, < 3)
52
+ simplecov-html (~> 0.10.0)
53
+ simplecov-html (0.10.2)
46
54
  thread_safe (0.3.6)
47
55
  unicode-display_width (1.3.2)
48
56
  virtus (1.0.5)
@@ -56,11 +64,13 @@ PLATFORMS
56
64
 
57
65
  DEPENDENCIES
58
66
  bundler (~> 1.16)
59
- minitest (~> 5.0)
67
+ byebug
68
+ minitest (~> 5.1)
60
69
  rake (~> 10.0)
61
70
  reek (~> 4.8.1)
62
71
  rubocop (~> 0.52.1)
63
72
  rubocop-airbnb
73
+ simplecov (~> 0.16.1)
64
74
  steppy!
65
75
 
66
76
  BUNDLED WITH
@@ -0,0 +1,5 @@
1
+ "test":
2
+ IrresponsibleModule:
3
+ enabled: false
4
+ InstanceVariableAssumption:
5
+ enabled: false
@@ -1,7 +1,141 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'steppy/version'
4
+ require 'steppy/cache'
4
5
 
6
+ # The Steppy module you'll include in your classes to give them steps!
5
7
  module Steppy
6
- # Your code goes here...
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ base.include InstanceMethods
11
+ end
12
+
13
+ # Steppy class methods that will be added to your included classes.
14
+ module ClassMethods
15
+ if !defined? step
16
+ def step(method, **args)
17
+ steppy_add step_method: method, step_args: args
18
+ end
19
+ end
20
+
21
+ if !defined? step_if
22
+ def step_if(condition, &block)
23
+ steppy_add step_if: condition, step_block: block
24
+ end
25
+ end
26
+
27
+ if !defined? step_set
28
+ def step_set(*sets)
29
+ steppy_cache[:sets] = *sets
30
+ end
31
+ end
32
+
33
+ def steppy_cache
34
+ @steppy_cache ||= SteppyCache.new(steps: nil, sets: [], block: nil)
35
+ end
36
+
37
+ def steppy_steps
38
+ steppy_cache[:steps] ||= []
39
+ end
40
+
41
+ def steppy_add(step)
42
+ steppy_steps.push(step)
43
+ steppy_cache
44
+ end
45
+
46
+ def steppy(&block)
47
+ steppy_cache[:block] = block
48
+ end
49
+ end
50
+
51
+ # Steppy instance methods that will be added.
52
+ module InstanceMethods
53
+ def steppy(attributes)
54
+ @steppy = { attributes: attributes.freeze }
55
+
56
+ steppy_run(
57
+ (steppy_class_block || steppy_class_cache).to_h
58
+ )
59
+ end
60
+
61
+ def steppy_attributes
62
+ @steppy[:attributes]
63
+ end
64
+
65
+ protected
66
+
67
+ def steppy_run(steps:, sets:, **)
68
+ steppy_run_sets(sets)
69
+ steppy_run_steps(steps)
70
+
71
+ @steppy[:result] || nil
72
+ end
73
+
74
+ def steppy_class_block
75
+ block = steppy_class_cache[:block]
76
+ block && steppy_class.instance_exec(&block)
77
+ end
78
+
79
+ def steppy_class_cache
80
+ self.class.steppy_cache
81
+ end
82
+
83
+ def steppy_run_sets(sets)
84
+ sets ||= steppy_class_cache[:sets]
85
+
86
+ sets.each do |set|
87
+ steppy_set(set, steppy_attributes[set])
88
+ end
89
+ end
90
+
91
+ def steppy_run_steps(steps)
92
+ steps.each do |step|
93
+ @steppy[:result] = step.key?(:step_if) ? steppy_if_block(step) : steppy_step(step)
94
+ end
95
+ end
96
+
97
+ def steppy_step(step_method:, step_args:)
98
+ method_name = "step_#{step_method}"
99
+
100
+ steppy_if(step_args[:if]) && return
101
+
102
+ if method(method_name).arity > 0
103
+ result = public_send(method_name, steppy_attributes)
104
+ else
105
+ result = public_send(method_name)
106
+ end
107
+
108
+ steppy_set(step_args[:set], result)
109
+ end
110
+
111
+ def steppy_if(step_if)
112
+ return unless step_if
113
+
114
+ if step_if.arity > 0
115
+ !instance_exec(steppy_attributes, &step_if)
116
+ else
117
+ !instance_exec(&step_if)
118
+ end
119
+ end
120
+
121
+ def steppy_set(step_set, result)
122
+ step_set && instance_variable_set("@#{step_set}", result)
123
+
124
+ result
125
+ end
126
+
127
+ def steppy_if_block(step_if:, step_block:)
128
+ passed = if step_if.arity > 0
129
+ instance_exec(steppy_attributes, &step_if)
130
+ else
131
+ instance_exec(&step_if)
132
+ end
133
+
134
+ passed && steppy_run(steppy_class.instance_exec(&step_block).to_h)
135
+ end
136
+
137
+ def steppy_class
138
+ Class.new { include Steppy }
139
+ end
140
+ end
7
141
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steppy
4
+ # https://github.com/jeremyevans/roda/blob/master/lib/roda.rb#L14-L42
5
+ # A thread safe cache class, offering only #[] and #[]= methods,
6
+ # each protected by a mutex.
7
+ class SteppyCache
8
+ # Create a new thread safe cache.
9
+ def initialize(hash = {})
10
+ @mutex = Mutex.new
11
+ @hash = hash
12
+ end
13
+
14
+ # Make getting value from underlying hash thread safe.
15
+ def [](key)
16
+ @mutex.synchronize { @hash[key] }
17
+ end
18
+
19
+ # Make setting value in underlying hash thread safe.
20
+ def []=(key, value)
21
+ @mutex.synchronize { @hash[key] = value }
22
+ end
23
+
24
+ def to_h
25
+ @mutex.synchronize { @hash }
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Steppy
4
- VERSION = '0.0.1'
4
+ VERSION = '0.1.0'
5
5
  end
@@ -24,8 +24,9 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.16'
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
- spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'minitest', '~> 5.1'
28
28
  spec.add_development_dependency 'reek', '~> 4.8.1'
29
29
  spec.add_development_dependency 'rubocop', '~> 0.52.1'
30
30
  spec.add_development_dependency 'rubocop-airbnb'
31
+ spec.add_development_dependency 'simplecov', '~> 0.16.1'
31
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steppy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-27 00:00:00.000000000 Z
11
+ date: 2018-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '5.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '5.1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: reek
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.16.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.16.1
97
111
  description: ''
98
112
  email:
99
113
  - cjlazell@gmail.com
@@ -112,8 +126,10 @@ files:
112
126
  - Rakefile
113
127
  - bin/console
114
128
  - bin/setup
129
+ - config.reek
115
130
  - hooks/pre-commit
116
131
  - lib/steppy.rb
132
+ - lib/steppy/cache.rb
117
133
  - lib/steppy/version.rb
118
134
  - steppy.gemspec
119
135
  homepage: https://github.com/cj/steppy