takes_macro 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: ac45871868bf22d2b8c236ec68de72f8a86f01e0f69b6e96c1b6ebbdff41f4b5
4
+ data.tar.gz: ba98c96f8568e1b0eb15a43c96479079bfa1829d42a1b1039bc78d6fea8ada78
5
+ SHA512:
6
+ metadata.gz: 9458d509d6fd5161941b876eaf640b73701ecec94a865fe6b124ba8a1414f215ede66b6f83fddd97eef48361925011ef5273a38449cecdb17283a006fb891f50
7
+ data.tar.gz: 219daeaef444a0a9b2dbb1b7d2c0d72279fa1f5d6d78d86fa8ee3abecb94e880a8ac73c75358de3c8cd497b06af7a7e46ffda195f85d963b5cb4f8bbd4ee5107
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
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 takes_macro.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ takes_macro (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ attr_extras (5.2.0)
10
+ benchmark-ips (2.7.2)
11
+ minitest (5.11.3)
12
+ rake (10.5.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ attr_extras (~> 5.2.0)
19
+ benchmark-ips (~> 2.7.2)
20
+ bundler (~> 1.16)
21
+ minitest (~> 5.0)
22
+ rake (~> 10.0)
23
+ takes_macro!
24
+
25
+ BUNDLED WITH
26
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # TakesMacro
2
+
3
+ [attr_extras][] is a **great** gem that lets you remove most of the boilerplate needed for creating different types of initializers in Ruby.
4
+
5
+ This gem contains a reimplementation of `pattr_initialize` from attr_extras that is much faster. If you're using attr_extras, but the only feature of it you're using is `pattr_initialize` then this gem is for you.
6
+
7
+ This gem calls the method `takes` to avoid confusing, but the API is exactly the same.
8
+
9
+ ## Benchmark
10
+
11
+ Run `bundle exec ruby benchmarks/takes_macro_vs_attr_extras.rb` to see how much faster this gem is. The output from doing that on my machine is:
12
+
13
+ $ bundle exec ruby benchmarks/takes_macro_vs_attr_extras.rb
14
+ Warming up --------------------------------------
15
+ attr_extras 15.793k i/100ms
16
+ takes_macro 91.596k i/100ms
17
+ hand written initializer
18
+ 71.351k i/100ms
19
+ Calculating -------------------------------------
20
+ attr_extras 169.353k (± 3.9%) i/s - 852.822k in 5.043680s
21
+ takes_macro 1.209M (± 4.5%) i/s - 6.045M in 5.011814s
22
+ hand written initializer
23
+ 875.721k (± 5.8%) i/s - 4.424M in 5.071249s
24
+
25
+ Comparison:
26
+ takes_macro: 1208748.5 i/s
27
+ hand written initializer: 875721.1 i/s - 1.38x slower
28
+ attr_extras: 169352.8 i/s - 7.14x slower
29
+
30
+ ## How it works
31
+
32
+ This gem expands this
33
+
34
+ ```ruby
35
+ class A
36
+ takes [:foo!]
37
+ end
38
+ ```
39
+
40
+ Into this
41
+
42
+ ```ruby
43
+ class A
44
+ def initialize(options)
45
+ @foo = options.fetch(:foo)
46
+ end
47
+
48
+ attr_reader :foo
49
+ private :foo
50
+ end
51
+ ```
52
+
53
+ It does that by looking at the arguments to `takes` and from that building a `String` of Ruby code that it will then `class_eval`. That means calling `takes` is literally as fast as writing the initializer by hand. Nothing fancy happens when you call `A.new(...)`.
54
+
55
+ ### Possible arguments to `takes`
56
+
57
+ You can call `takes` in many different ways depending on the style of initializer you want. Here are the different styles and what they expand into:
58
+
59
+ #### Positional args
60
+
61
+ ```ruby
62
+ takes :foo, :bar
63
+
64
+ def initialize(foo, bar)
65
+ @foo = foo
66
+ @bar = bar
67
+ end
68
+ ```
69
+
70
+ #### Required keyword args
71
+
72
+ ```ruby
73
+ takes [:foo!, :bar!]
74
+
75
+ def initialize(foo:, bar:)
76
+ @foo = foo
77
+ @bar = bar
78
+ end
79
+ ```
80
+
81
+ #### Optional keyword args
82
+
83
+ ```ruby
84
+ takes [:foo, :bar]
85
+
86
+ def initialize(foo: nil, bar: nil)
87
+ @foo = foo
88
+ @bar = bar
89
+ end
90
+ ```
91
+
92
+ #### Mixed positional, required and optional keyword args
93
+
94
+ ```ruby
95
+ takes :foo, [:bar!, :baz]
96
+
97
+ def initialize(foo, bar:, baz: nil)
98
+ @foo = foo
99
+ @bar = bar
100
+ @baz = baz
101
+ end
102
+ ```
103
+
104
+ __Note:__ Each instance variable set in the initializer also gets a private `attr_reader`, but that was left out of the examples for clarity.
105
+
106
+ ## Installation
107
+
108
+ Add this line to your application's Gemfile:
109
+
110
+ ```ruby
111
+ gem "takes_macro"
112
+ ```
113
+
114
+ And then execute:
115
+
116
+ $ bundle
117
+
118
+ Or install it yourself as:
119
+
120
+ $ gem install takes_macro
121
+
122
+ ## Usage
123
+
124
+ If you want to use `takes` in all your classes add this to your app:
125
+
126
+ ```ruby
127
+ require "takes_macro"
128
+
129
+ TakesMacro.monkey_patch_object
130
+ ```
131
+
132
+ If you're in a Rails app I recommend adding this to `config/application.rb` so you're sure to have it in all your classes.
133
+
134
+ That will include the `TakesMacro` module, which defines the `takes` method, on `Object`.
135
+
136
+ If you don't like monkey patching `Object` you can still do this:
137
+
138
+ ```ruby
139
+ require "takes_macro"
140
+
141
+ class A
142
+ include TakesMacro
143
+
144
+ takes [:foo!, :bar!, :baz!]
145
+ end
146
+ ```
147
+
148
+ ## Development
149
+
150
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
151
+
152
+ 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).
153
+
154
+ [attr_extras]: https://github.com/barsoom/attr_extras
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,41 @@
1
+ require "benchmark/ips"
2
+ require "attr_extras"
3
+ require "takes_macro"
4
+
5
+ class A
6
+ pattr_initialize :a, [:b!, :c]
7
+ end
8
+
9
+ TakesMacro.monkey_patch_object
10
+
11
+ class B
12
+ takes :a, [:b!, :c]
13
+ end
14
+
15
+ class C
16
+ def initialize(a, b:, c: nil)
17
+ @a = a
18
+ @b = b
19
+ @c = c
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :a, :b, :c
25
+ end
26
+
27
+ Benchmark.ips do |x|
28
+ x.report("attr_extras") do
29
+ A.new(:a, b: :b)
30
+ end
31
+
32
+ x.report("takes_macro") do
33
+ B.new(:a, b: :b)
34
+ end
35
+
36
+ x.report("hand written initializer") do
37
+ C.new(:a, b: :b)
38
+ end
39
+
40
+ x.compare!
41
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "takes_macro"
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
@@ -0,0 +1,75 @@
1
+ require "takes_macro/version"
2
+
3
+ module TakesMacro
4
+ def self.monkey_patch_object
5
+ Object.send(:include, TakesMacro)
6
+ end
7
+
8
+ def takes(*args)
9
+ positional_args = args.reject { |arg| arg.is_a?(Array) }
10
+ keyword_args = if args.last.is_a?(Array)
11
+ args.last.map(&:to_s)
12
+ else
13
+ []
14
+ end
15
+ all_args = (positional_args + keyword_args).flatten.map do |arg|
16
+ arg.to_s.delete("!")
17
+ end
18
+
19
+ args_code = [
20
+ positional_args,
21
+ ]
22
+
23
+ if keyword_args.any?
24
+ args_code << "options"
25
+ end
26
+
27
+ args_code = args_code.flatten.compact.join(", ")
28
+
29
+ bind_positional_args_code = positional_args.map do |arg|
30
+ "@#{arg} = #{arg}"
31
+ end.join("\n")
32
+
33
+ bind_keywords_args_code = keyword_args.map do |arg|
34
+ code = if arg =~ /!$/
35
+ arg = arg.delete("!")
36
+ "@#{arg} = options.fetch(:#{arg})"
37
+ else
38
+ "@#{arg} = options[:#{arg}]"
39
+ end
40
+ [code, "options.delete(:#{arg})"].join("\n")
41
+ end.join("\n")
42
+
43
+ define_private_attr_readers_code = all_args.map do |arg|
44
+ <<-RUBY
45
+ attr_reader :#{arg}
46
+ private :#{arg}
47
+ RUBY
48
+ end.join("\n")
49
+
50
+ ensure_options_are_empty =
51
+ if keyword_args.any?
52
+ <<-EOS
53
+ unless options == {}
54
+ raise ArgumentError, "Not all initialize args were used: \#{options.keys.inspect}"
55
+ end
56
+ EOS
57
+ else
58
+ ""
59
+ end
60
+
61
+ code = <<-RUBY
62
+ def initialize(#{args_code})
63
+ #{bind_positional_args_code}
64
+ #{bind_keywords_args_code}
65
+ #{ensure_options_are_empty}
66
+ after_takes
67
+ end
68
+ #{define_private_attr_readers_code}
69
+
70
+ def after_takes; end
71
+ RUBY
72
+
73
+ class_eval code
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module TakesMacro
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "takes_macro/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "takes_macro"
8
+ spec.version = TakesMacro::VERSION
9
+ spec.authors = ["David Pedersen"]
10
+ spec.email = ["david@tonsser.com"]
11
+
12
+ spec.summary = %q{Remove boilerplate for writing initializers}
13
+ spec.description = %q{A faster implementation of `pattr_initialize` from the attr_extras gem}
14
+ spec.homepage = "https://github.com/tonsser/takes_macro"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "benchmark-ips", "~> 2.7.2"
27
+ spec.add_development_dependency "attr_extras", "~> 5.2.0"
28
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: takes_macro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Pedersen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-10 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: benchmark-ips
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.7.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.7.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: attr_extras
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.2.0
83
+ description: A faster implementation of `pattr_initialize` from the attr_extras gem
84
+ email:
85
+ - david@tonsser.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - README.md
95
+ - Rakefile
96
+ - benchmarks/takes_macro_vs_attr_extras.rb
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/takes_macro.rb
100
+ - lib/takes_macro/version.rb
101
+ - takes_macro.gemspec
102
+ homepage: https://github.com/tonsser/takes_macro
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.7.3
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Remove boilerplate for writing initializers
125
+ test_files: []