exercise_options 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
+ SHA256:
3
+ metadata.gz: c8525b7df8fecd5a8814480f9ee33726fb430f07cdb20c8a48610f4df98ad27f
4
+ data.tar.gz: 71f9c1a405aaffd708380aec4ea86b620fd693d7a9ee5430fa5dd0cb77677c84
5
+ SHA512:
6
+ metadata.gz: 12714c0567994b5b466cbefa1dfc0d4ead9644195b522b804bca2312c042d74b36a110498bb71bad09ee81f6daf4db4a20aa26d5fb98eaaecff23c121b5e8402
7
+ data.tar.gz: caca9d1c900ab25f927b03a3397466c3d4ce15d5ec1d485762df41ceec7d695341236fb50dcbc259cd7b542fd4993bed15423f763a97f67253c603f91ea9404c
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in exercise_options.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # ExerciseOptions
2
+
3
+ An experimental gem for making amendments to upstream dependencies.
4
+
5
+ This project came about when I was working with Samvera's Actor Stack and needed to add additional variables to a few different classes.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add exercise_options
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install exercise_options
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ class MyObject
21
+ def initialize(name:)
22
+ self.name = name
23
+ end
24
+ attr_accessor :name
25
+ end
26
+
27
+ MyObject.prepend(
28
+ ExerciseOptions.for(
29
+ default_is_true: true,
30
+ conditionally_true_with_receiver: ->(obj) { obj.name == "Hello World" },
31
+ )
32
+ )
33
+
34
+ obj = MyObject.new(name: "Nobody")
35
+ expect(obj.default_is_true).to be_truthy
36
+ expect(obj.conditionally_true_with_receiver).to be_falsey
37
+
38
+
39
+ next_object = MyObject.new(name: "Hello World", default_is_true: false)
40
+ expect(next_object.default_is_true).to be_falsey
41
+ expect(next_object.conditionally_true_with_receiver).to be_truthy
42
+ ```
43
+
44
+ ## Development
45
+
46
+ Let's see if this thing make sense?
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExerciseOptions
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "exercise_options/version"
4
+ require "active_support"
5
+
6
+ module ExerciseOptions
7
+ class Error < StandardError; end
8
+ # Decorate an upstream object with additional attributes. By including the generated module,
9
+ # ExerciseOptions will intercept a call to `new` and apply the declared options to the newly
10
+ # instantiated object.
11
+ #
12
+ # @param options [Hash<Symbol, Object>] a Hash with keys that are symbols. Each value may be
13
+ # either a lambda or not. When it is a lambda, that will be called at object
14
+ # initialization. When it is not a lambda, that will be the default value at object
15
+ # initialization.
16
+ #
17
+ # @return [Module]
18
+ #
19
+ # @example
20
+ # class MyObject
21
+ # def initialize(name:)
22
+ # self.name = name
23
+ # end
24
+ # attr_accessor :name
25
+ # end
26
+ #
27
+ # MyObject.prepend(
28
+ # ExerciseOptions.for(
29
+ # default_is_true: true,
30
+ # conditionally_true_with_receiver: ->(obj) { obj.name == "Hello World" },
31
+ # )
32
+ # )
33
+ #
34
+ # obj = MyObject.new(name: "Nobody")
35
+ # expect(obj.default_is_true).to be_truthy
36
+ # expect(obj.conditionally_true_with_receiver).to be_falsey
37
+ #
38
+ #
39
+ # next_object = MyObject.new(name: "Hello World", default_is_true: false)
40
+ # expect(next_object.default_is_true).to be_falsey
41
+ # expect(next_object.conditionally_true_with_receiver).to be_truthy
42
+ def self.for(**options)
43
+ Module.new do
44
+ extend ActiveSupport::Concern
45
+ class_methods do
46
+ define_method(:new) do |*args, **kwargs, &block|
47
+ super(*args, **kwargs.except(*options.keys), &block).tap { |instance|
48
+ options.keys.each do |key|
49
+ value = if kwargs.key?(key)
50
+ kwargs.fetch(key)
51
+ else
52
+ instance.send("default_#{key}")
53
+ end
54
+ instance.public_send("#{key}=", value)
55
+ end
56
+ }
57
+ end
58
+ end
59
+
60
+ options.each do |key, value|
61
+ attr_accessor key
62
+ define_method "default_#{key}" do
63
+ if value.respond_to?(:call)
64
+ if value.arity == 1
65
+ value.call(self)
66
+ else
67
+ value.call
68
+ end
69
+ else
70
+ value
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,4 @@
1
+ module ExerciseOptions
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exercise_options
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Friesen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-21 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: An experiment in extending object behavior.
28
+ email:
29
+ - jeremy.n.friesen@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - lib/exercise_options.rb
38
+ - lib/exercise_options/version.rb
39
+ - sig/exercise_options.rbs
40
+ homepage: https://github.com/jeremyf/exercise_options
41
+ licenses: []
42
+ metadata:
43
+ homepage_uri: https://github.com/jeremyf/exercise_options
44
+ source_code_uri: https://github.com/jeremyf/exercise_options
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.6.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.3.7
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: An experiment in extending object behavior.
64
+ test_files: []