nxt_init 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: 4083d4d1fb38f7710efe56b73885d80c4a17162972a5f612c6f823c99dcd929e
4
+ data.tar.gz: 35f436cf95fc2ece798d72adbf752f2cf0f7589c942ecaa4b284dda86ab45694
5
+ SHA512:
6
+ metadata.gz: 96b94320915b5537e2900abaff80ae834c8ee4b6040a50fb0714bc2d68a722267972ed0263f0a2f6852f4c840defc3010abe50ca530247b9e1770e673b75019a
7
+ data.tar.gz: 589a450168492b34faf948b3a724ecd86f7b35aaf699753ad8192638d773633ac12ad9fa0f0eb985516462de2f86e6321da40eb70c54d9cc7fba5b3374c0607e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
12
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.1
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 1.17.2
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 nxt_init.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nxt_init (0.1.1)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ coderay (1.1.2)
16
+ concurrent-ruby (1.1.4)
17
+ diff-lcs (1.3)
18
+ i18n (1.5.3)
19
+ concurrent-ruby (~> 1.0)
20
+ method_source (0.9.2)
21
+ minitest (5.11.3)
22
+ pry (0.12.2)
23
+ coderay (~> 1.1.0)
24
+ method_source (~> 0.9.0)
25
+ rake (10.5.0)
26
+ rspec (3.8.0)
27
+ rspec-core (~> 3.8.0)
28
+ rspec-expectations (~> 3.8.0)
29
+ rspec-mocks (~> 3.8.0)
30
+ rspec-core (3.8.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-expectations (3.8.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-mocks (3.8.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-support (3.8.0)
39
+ thread_safe (0.3.6)
40
+ tzinfo (1.2.5)
41
+ thread_safe (~> 0.1)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 1.17)
48
+ nxt_init!
49
+ pry
50
+ rake (~> 10.0)
51
+ rspec (~> 3.0)
52
+
53
+ BUNDLED WITH
54
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Andreas Robecke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # NxtInit
2
+
3
+ Create an initializer that accepts option arguments and define private readers for your
4
+ arguments at the same time.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'nxt_init'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install attrinit
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ class MyService
26
+ include NxtInit
27
+ attr_init :one,
28
+ two: 'has a default',
29
+ three: nil, # makes the attribute optional
30
+ four: -> { "This is set on initialize: #{Time.now} - means it will not be evaluated multiple times" }
31
+
32
+ def call
33
+ {
34
+ one: one,
35
+ two: two,
36
+ three: three,
37
+ four: four
38
+ }
39
+ end
40
+ end
41
+
42
+ my_service = MyService.new(one: 'this is required')
43
+ my_service.call
44
+
45
+ # Will output the following:
46
+ {
47
+
48
+ one: "this is required",
49
+ two: "has a default",
50
+ three: nil,
51
+ four: "This is evaluated on initialize: 2019-02-04 18:10:56 +0100 - means it will not be evaluated multiple times"
52
+ }
53
+ ```
54
+
55
+ The attribute readers are private. If you need public accessors you have to add them yourself. That's all there is.
56
+ Check out the specs for examples how we handle inheritance.
57
+
58
+ ## Development
59
+
60
+ 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.
61
+
62
+ 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).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nxt-insurance/nxt_init.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nxt_init"
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,3 @@
1
+ module NxtInit
2
+ VERSION = "0.1.0"
3
+ end
data/lib/nxt_init.rb ADDED
@@ -0,0 +1,73 @@
1
+ require "nxt_init/version"
2
+ require 'active_support'
3
+
4
+ module NxtInit
5
+ InvalidOptionError = Class.new(ArgumentError)
6
+
7
+ module ClassMethods
8
+ def attr_init(*args)
9
+ flat_args = flatten_options(*args)
10
+ self.attr_init_opts ||= []
11
+ self.attr_init_opts += flat_args
12
+ define_private_readers(*flat_args)
13
+ end
14
+
15
+ attr_accessor :attr_init_opts
16
+
17
+ private
18
+
19
+ def inherited(subclass)
20
+ subclass.attr_init_opts = attr_init_opts.map(&:dup)
21
+ end
22
+
23
+ def define_private_readers(*args)
24
+ keys = args.map { |attr| attr.is_a?(Hash) ? attr.keys.first : attr }
25
+ attr_reader *keys
26
+ private *keys
27
+ end
28
+
29
+ def flatten_options(*args)
30
+ options_hash = *args.extract_options!
31
+ options_hash.each { |key, value| args << {"#{key}": value} }
32
+ args
33
+ end
34
+ end
35
+
36
+ module InstanceMethods
37
+ def initialize(*args, **attrs)
38
+ option_keys = self.class.send(:attr_init_opts).map do |option|
39
+ option.is_a?(Hash) ? option.keys.first : option
40
+ end
41
+
42
+ attr_init_opts = attrs.slice(*option_keys)
43
+ other_options = attrs.slice!(*option_keys)
44
+ # passing **{} is like calling super({}) which does not work when super does not except arguments
45
+ initialize_attrs_from_options(**attr_init_opts)
46
+ other_options.empty? ? super(*args) : super(*args, **other_options)
47
+ end
48
+
49
+ private
50
+
51
+ def initialize_attrs_from_options(**attrs)
52
+ self.class.send(:attr_init_opts).each do |opt|
53
+ if opt.is_a?(Hash)
54
+ key = opt.keys.first
55
+ value = opt.values.first
56
+ value = attrs[key] || (value.respond_to?(:call) ? instance_exec(&value) : value)
57
+ elsif opt.is_a?(Symbol)
58
+ key = opt
59
+ value = attrs.fetch(opt)
60
+ else
61
+ raise InvalidOptionError, "Don't know how to deal with #{opt}"
62
+ end
63
+ instance_variable_set("@#{key}", value)
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ def self.included(base)
70
+ base.extend(ClassMethods)
71
+ base.include(InstanceMethods)
72
+ end
73
+ end
data/nxt_init.gemspec ADDED
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "nxt_init/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nxt_init"
8
+ spec.version = NxtInit::VERSION
9
+ spec.authors = ["Andreas Robecke"]
10
+ spec.email = ["a.robecke@getsafe.de"]
11
+
12
+ spec.summary = %q{nxt_init allows you to define an initializers that takes option arguments and defines private readers for those on the fly}
13
+ spec.homepage = "https://github.com/nxt-insurance"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/nxt-insurance/nxt_init"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "activesupport"
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "pry"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nxt_init
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andreas Robecke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-06 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
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - a.robecke@getsafe.de
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-version"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/nxt_init.rb
102
+ - lib/nxt_init/version.rb
103
+ - nxt_init.gemspec
104
+ homepage: https://github.com/nxt-insurance
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ allowed_push_host: https://rubygems.org
109
+ homepage_uri: https://github.com/nxt-insurance
110
+ source_code_uri: https://github.com/nxt-insurance/nxt_init
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.0.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: nxt_init allows you to define an initializers that takes option arguments
130
+ and defines private readers for those on the fly
131
+ test_files: []