adornable 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b5866ce4d639cd7201a6467800e5ac85fa9e6b823c57496e8ac670059b5d7f5a
4
+ data.tar.gz: '0418007247c26928a448fd069495b41cdd722d8902f7894185c7589ac8586d2e'
5
+ SHA512:
6
+ metadata.gz: be5afe7b6f0eaadd690d4c1e62112a71d1e2f9d26481cec53bf4358a99394809d1d8f2b6a71bf464316320776753bb687bc65e71b96378aabe378da0f2065463
7
+ data.tar.gz: 250c536c81d0ad0b5c7388af95623e1fc563b0fc41522902542a1e7e2ff9ca0f3f25f6e39378cc1d780ce048e4f184ab4d8a75c2a2f5d1c4ca3621e1084b645d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.7
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in adornable.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ adornable (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (10.5.0)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ adornable!
30
+ bundler (~> 1.17)
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.17.3
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Adornable
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/adornable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'adornable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install adornable
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/adornable.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/adornable.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "adornable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "adornable"
8
+ spec.version = Adornable::VERSION
9
+ spec.authors = ["Keegan Leitz"]
10
+ spec.email = ["kjleitz@gmail.com"]
11
+
12
+ spec.summary = "Method decorators for Ruby"
13
+ spec.description = "Method decorators for Ruby"
14
+ spec.homepage = "https://github.com/kjleitz/adornable"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "adornable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/adornable.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "adornable/version"
2
+ require "adornable/utils"
3
+ require "adornable/error"
4
+ require "adornable/decorators"
5
+ require "adornable/machinery"
6
+
7
+ module Adornable
8
+ def adornable_machinery
9
+ @adornable_machinery ||= Adornable::Machinery.new
10
+ end
11
+
12
+ def decorate(decorator_name, from: nil, defer_validation: false)
13
+ if Adornable::Utils.blank?(name)
14
+ raise Adornable::Error::InvalidDecoratorArguments, "Decorator name must be provided."
15
+ end
16
+
17
+ adornable_machinery.accumulate_decorator!(
18
+ name: decorator_name,
19
+ receiver: from,
20
+ defer_validation: !!defer_validation
21
+ )
22
+ end
23
+
24
+ def add_decorators_from(receiver)
25
+ adornable_machinery.register_decorator_receiver!(receiver)
26
+ end
27
+
28
+ def method_added(method_name)
29
+ machinery = adornable_machinery # for local variable
30
+ return unless machinery.has_accumulated_decorators?
31
+ machinery.apply_accumulated_decorators_to_instance_method!(method_name)
32
+ original_method = self.instance_method(method_name)
33
+ define_method(method_name) do |*args|
34
+ bound_method = original_method.bind(self)
35
+ machinery.run_decorated_instance_method(bound_method, *args)
36
+ end
37
+ super
38
+ end
39
+
40
+ def singleton_method_added(method_name)
41
+ machinery = adornable_machinery # for local variable
42
+ return unless machinery.has_accumulated_decorators?
43
+ machinery.apply_accumulated_decorators_to_class_method!(method_name)
44
+ original_method = self.method(method_name)
45
+ define_singleton_method(method_name) do |*args|
46
+ machinery.run_decorated_class_method(original_method, *args)
47
+ end
48
+ super
49
+ end
50
+ end
@@ -0,0 +1,29 @@
1
+ module Adornable
2
+ class Decorators
3
+ def self.log(method_receiver, method_name, arguments)
4
+ receiver_name, name_delimiter = if method_receiver.is_a?(Class)
5
+ [method_receiver.to_s, '::']
6
+ else
7
+ [method_receiver.class.to_s, '#']
8
+ end
9
+ full_name = "`#{receiver_name}#{name_delimiter}#{method_name}`"
10
+ arguments_desc = arguments.empty? ? "no arguments" : "arguments `#{arguments}`"
11
+ puts "Calling method #{full_name} with #{arguments_desc}"
12
+ yield
13
+ end
14
+
15
+ def self.memoize(method_receiver, method_name, arguments)
16
+ memo_var_name = :"@adornable_memoized_#{method_receiver.object_id}_#{method_name}"
17
+ existing = instance_variable_get(memo_var_name)
18
+ value = existing.nil? ? yield : existing
19
+ instance_variable_set(memo_var_name, value)
20
+ end
21
+
22
+ def self.memoize_for_arguments(method_receiver, method_name, arguments)
23
+ memo_var_name = :"@adornable_memoized_#{method_receiver.object_id}__#{method_name}__#{arguments}"
24
+ existing = instance_variable_get(memo_var_name)
25
+ value = existing.nil? ? yield : existing
26
+ instance_variable_set(memo_var_name, value)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module Adornable
2
+ module Error
3
+ class Base < ::StandardError
4
+ end
5
+
6
+ class InvalidDecoratorArguments < Adornable::Error::Base
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,124 @@
1
+ require 'adornable/utils'
2
+ require 'adornable/error'
3
+
4
+ module Adornable
5
+ class Machinery
6
+ def register_decorator_receiver!(receiver)
7
+ registered_decorator_receivers.unshift(receiver)
8
+ end
9
+
10
+ def accumulate_decorator!(name:, receiver:, defer_validation:)
11
+ name = name.to_sym
12
+ receiver ||= find_suitable_receiver_for(name)
13
+ validate_decorator!(name, receiver) unless defer_validation
14
+
15
+ decorator = { name: name, receiver: receiver }
16
+ accumulated_decorators << decorator
17
+ end
18
+
19
+ def has_accumulated_decorators?
20
+ Adornable::Utils.present?(accumulated_decorators)
21
+ end
22
+
23
+ def apply_accumulated_decorators_to_instance_method!(method_name)
24
+ set_instance_method_decorators!(method_name, accumulated_decorators)
25
+ clear_accumulated_decorators!
26
+ end
27
+
28
+ def apply_accumulated_decorators_to_class_method!(method_name)
29
+ set_class_method_decorators!(method_name, accumulated_decorators)
30
+ clear_accumulated_decorators!
31
+ end
32
+
33
+ def run_decorated_instance_method(bound_method, *args)
34
+ decorators = get_instance_method_decorators(bound_method.name)
35
+ run_decorators(decorators, bound_method, *args)
36
+ end
37
+
38
+ def run_decorated_class_method(bound_method, *args)
39
+ decorators = get_class_method_decorators(bound_method.name)
40
+ run_decorators(decorators, bound_method, *args)
41
+ end
42
+
43
+ private
44
+
45
+ def registered_decorator_receivers
46
+ @registered_decorator_receivers ||= [Adornable::Decorators]
47
+ end
48
+
49
+ def accumulated_decorators
50
+ @accumulated_decorators ||= []
51
+ end
52
+
53
+ def clear_accumulated_decorators!
54
+ @accumulated_decorators = []
55
+ end
56
+
57
+ def get_instance_method_decorators(method_name)
58
+ name = method_name.to_sym
59
+ @instance_method_decorators ||= {}
60
+ @instance_method_decorators[name] ||= []
61
+ @instance_method_decorators[name]
62
+ end
63
+
64
+ def set_instance_method_decorators!(method_name, decorators)
65
+ name = method_name.to_sym
66
+ @instance_method_decorators ||= {}
67
+ @instance_method_decorators[name] = decorators || []
68
+ end
69
+
70
+ def get_class_method_decorators(method_name)
71
+ name = method_name.to_sym
72
+ @class_method_decorators ||= {}
73
+ @class_method_decorators[name] ||= []
74
+ @class_method_decorators[name]
75
+ end
76
+
77
+ def set_class_method_decorators!(method_name, decorators)
78
+ name = method_name.to_sym
79
+ @class_method_decorators ||= {}
80
+ @class_method_decorators[name] = decorators || []
81
+ end
82
+
83
+ def run_decorators(decorators, bound_method, *args)
84
+ return bound_method.call(*args) if Adornable::Utils.blank?(decorators)
85
+ decorator, *remaining_decorators = decorators
86
+ name = decorator[:name]
87
+ receiver = decorator[:receiver]
88
+ validate_decorator!(name, receiver, bound_method)
89
+ receiver.send(name, bound_method.receiver, bound_method.name, args) do
90
+ run_decorators(remaining_decorators, bound_method, *args)
91
+ end
92
+ end
93
+
94
+ def find_suitable_receiver_for(decorator_name)
95
+ registered_decorator_receivers.detect do |receiver|
96
+ receiver.respond_to?(decorator_name)
97
+ end
98
+ end
99
+
100
+ def validate_decorator!(decorator_name, decorator_receiver, bound_method = nil)
101
+ return if decorator_receiver.respond_to?(decorator_name)
102
+
103
+ location_hint = if bound_method
104
+ method_receiver = bound_method.receiver
105
+ method_full_name = method_receiver.is_a?(Class) ? "#{method_receiver}::#{method.name}" : "#{method_receiver.class}##{method.name}"
106
+ method_location = bound_method.source_location
107
+ "Cannot decorate `#{method_full_name}` (defined at `#{method_location.first}:#{method_location.second})."
108
+ end
109
+
110
+ base_message = "Decorator method `#{decorator_name.inspect}` cannot be found on `#{decorator_receiver.inspect}`."
111
+
112
+ definition_hint = if decorator_receiver.is_a?(Class) && decorator_receiver.instance_methods.include?(decorator_name)
113
+ class_name = decorator_receiver.inspect
114
+ "It is, however, an instance method of the class. To use this decorator method, either A) supply an instance of the `#{class_name}` class to the `found_on:` option (instead of the class itself), B) convert the instance method `#{class_name}##{decorator_name}` to a class method, or C) create a new class method on `#{class_name}` of the same decorator_name."
115
+ elsif !decorator_receiver.is_a?(Class) && decorator_receiver.class.methods.include?(decorator_name)
116
+ class_name = decorator_receiver.class.inspect
117
+ "It is, however, a method of this instance's class. To use this decorator method, either A) supply the `#{class_name}` class itself to the `found_on:` option (instead of an instance of that class), B) convert the class method `#{class_name}::#{decorator_name}` to an instance method, or C) create a new instance method on `#{class_name}` of the same name."
118
+ end
119
+
120
+ message = [location_hint, base_message, definition_hint].compact.join(" ")
121
+ raise Adornable::Error::InvalidDecoratorArguments, message
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,17 @@
1
+ module Adornable
2
+ class Utils
3
+ class << self
4
+ def blank?(value)
5
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
6
+ end
7
+
8
+ def present?(value)
9
+ !blank?(value)
10
+ end
11
+
12
+ def presence(value)
13
+ value if present?(value)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Adornable
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adornable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Keegan Leitz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Method decorators for Ruby
56
+ email:
57
+ - kjleitz@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - adornable.gemspec
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/adornable.rb
73
+ - lib/adornable/decorators.rb
74
+ - lib/adornable/error.rb
75
+ - lib/adornable/machinery.rb
76
+ - lib/adornable/utils.rb
77
+ - lib/adornable/version.rb
78
+ homepage: https://github.com/kjleitz/adornable
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.8
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Method decorators for Ruby
101
+ test_files: []