ks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f36c29feda7f2d5ef40f578461982bfc9d1b19d3
4
+ data.tar.gz: 1b15cad80b3bdfa52af0f482096c0eae77836c0b
5
+ SHA512:
6
+ metadata.gz: bc92b6fc8685faea432f14c1953e2191fa963231bd2260966b49d9849198b43b333717fad75ac870a334764cfcba3cf0d0ac0f4a0cc9618fd67b4410c5c0de87
7
+ data.tar.gz: 20768fa163981bc152fdd424a7946e7709e4bbe6344ba3a9a3969a3195d0937ba8d8837a142a2ca8e234750e5c6780f69e1426df0bea9d096574cdbc3aab2bad
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 3.2.0"
5
+ gem "rdoc", "~> 3.12"
6
+ gem "bundler", "~> 1.0"
7
+ gem "jeweler", "~> 2.0.1"
8
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 WeTransfer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # ks - a generator for keyworded Structs
2
+
3
+ Normally when you create a Struct in Ruby you get a positional arguments
4
+ initializer.
5
+
6
+ S = Struct.new(:a, :b)
7
+ S.new(1, 2)
8
+
9
+ This gem allows for creation of Structs that accept keyword arguments in the same way.
10
+
11
+ S = Ks.strict(:a, :b)
12
+ S.new(a: 1, b: 2)
13
+
14
+ The resulting Struct will complain if you omit some keyword arguments, and will take care
15
+ of handling them for you as well. The generated classes are cached and the created
16
+ initializer is inserted in a class between your descendant and the Struct subclass
17
+ itself. If you want defaults, use a `Module#prepend` or `Module#include` solution,
18
+ or just call `super` with some keyword arguments set to their defaults.
19
+
20
+ ## Contributing to ks
21
+
22
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
23
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
24
+ * Fork the project.
25
+ * Start a feature/bugfix branch.
26
+ * Commit and push until you are happy with your contribution.
27
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
28
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
29
+
30
+ ## Copyright
31
+
32
+ Copyright (c) 2015 WeTransfer. See LICENSE.txt for
33
+ further details.
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require_relative 'lib/ks'
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ gem.version = Ks::VERSION
18
+ gem.name = "ks"
19
+ gem.homepage = "http://github.com/wetransfer/ks"
20
+ gem.license = "MIT"
21
+ gem.description = %Q{Keyword-initialized Structs}
22
+ gem.description = %Q{Keyword-initialized Structs}
23
+ gem.email = "me@julik.nl"
24
+ gem.authors = ["Julik Tarkhanov"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ desc "Code coverage detail"
36
+ task :simplecov do
37
+ ENV['COVERAGE'] = "true"
38
+ Rake::Task['spec'].execute
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "ks #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
@@ -0,0 +1,43 @@
1
+ require 'thread'
2
+ require 'set'
3
+
4
+ # "Ks" - as in "kiss" - a generator of keyworded Structs.
5
+ module Ks
6
+ VERSION = '0.0.1'
7
+
8
+ @@caching_mutex = Mutex.new
9
+ @@predefined_structs = {}
10
+
11
+ # Returns a class that is a descendant of Struct, with a strict
12
+ # keyword initializer.
13
+ #
14
+ # Info = Ks.strict(:item_count, :weight)
15
+ # data = Info.new(item_count: 1, weight: 2)
16
+ #
17
+ # Note that all the keyword arguments defined for the class (all the members)
18
+ # are going to be required keyword arguments for the initializer.
19
+ #
20
+ # The created classes (Struct descendants) are cached to make reloading easier,
21
+ # since when reloading a usual Struct descendant it will receive a different parent
22
+ # class. This is mitigated by caching the created subclasses using their member lists
23
+ #
24
+ # @param members[Array<Symbol>] the names of members to create
25
+ # @return created_class[Class]
26
+ def self.strict(*members)
27
+ k = members.sort.join(':')
28
+ @@caching_mutex.synchronize do
29
+ return @@predefined_structs[k] if @@predefined_structs[k]
30
+
31
+ struct_ancestor = Struct.new(*members)
32
+ predefined = Class.new(struct_ancestor) do
33
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
34
+ def initialize(#{members.map { |a| "#{a}:" }.join(', ')}) # def initialize(bar:, baz:)
35
+ super(#{members.join(', ')}) # super(bar, baz)
36
+ end # end
37
+ METHOD
38
+ end
39
+ @@predefined_structs[k] = predefined
40
+ predefined
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Ks" do
4
+ describe '.strict' do
5
+ it "allocates a Struct class that can be initialized with keywords" do
6
+ k = Ks.strict(:foo, :bar)
7
+ item = k.new(foo: 1, bar: 2)
8
+ expect(item.foo).to eq(1)
9
+ expect(item.bar).to eq(2)
10
+ expect(item.class.ancestors).to include(Struct)
11
+ expect(item.members).to eq([:foo, :bar])
12
+ end
13
+
14
+ it 'raises when keyword arguments are omitted' do
15
+ k = Ks.strict(:foo, :bar)
16
+ expect {
17
+ k.new(foo: 1)
18
+ }.to raise_error(ArgumentError, 'missing keyword: bar')
19
+ end
20
+
21
+ it 'caches the created Struct ancestor even when using multiple threads' do
22
+ classes = (1..12).map do
23
+ Thread.new { Ks.strict(:one, :another) }
24
+ end.map(&:join).map(&:value)
25
+ expect(classes.uniq.length).to eq(1)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'ks'
6
+
7
+ RSpec.configure do |config|
8
+ config.order = 'random'
9
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Julik Tarkhanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.1
69
+ description: Keyword-initialized Structs
70
+ email: me@julik.nl
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.md
76
+ files:
77
+ - ".document"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/ks.rb
84
+ - spec/ks_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: http://github.com/wetransfer/ks
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: ''
110
+ test_files: []