typed_array 1.0.1

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: 51df2a5bdd1bf2ef18e2d930fded2af9434367b7bfc29312bd3538a3b8d5cf71
4
+ data.tar.gz: 22dbcfe75d718da1528a19a388b41d7c7b03552737354931bf8a5575b874cfad
5
+ SHA512:
6
+ metadata.gz: 851e90ba45588b231d456730308f504db1b7d135a58879873a305717599460c280234750847c9ed56cc366df209276926274599cc137b25281be828ba6367cd4
7
+ data.tar.gz: 3568614e4cffe9eff314a31a25597c4fbd491fba7e700108ffd3b6863db41fca68f0cef1090911f95b79fa390b9b51e1881484e06faf176e724072c17a6ef1d8
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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.8
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 typed_array.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ typed_array (1.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ typed_array!
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alexander Koltun
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,116 @@
1
+ # TypedArray
2
+
3
+ This gem provides new type `TypedArray` which is direct successor of ruby `Array` class.
4
+ `TypedArray` requires to specify a type of its elements and allows to add only elements of this type.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'typed_array'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install typed_array
21
+
22
+ ## Usage
23
+
24
+ ### Creation
25
+
26
+ `TypedArray` has a read-only attribute `:item_class` which specifies the class of its elements.
27
+ Any attempt to add elements of different class will cause `ArgumentError` exception with corresponding message.
28
+
29
+ `TypedArray` instance can be created in the same way as ordinary `Array`.
30
+ If it is created from the list of elements than the class of first non-nil element
31
+ is considered as `:item_class` and all other elements should be either of the same class or `nil`.
32
+
33
+ ```ruby
34
+ ta = TypedArray[nil, 1, 2, 3] # => [nil, 1, 2, 3]
35
+ ta.item_class # => Fixnum
36
+ ta << 5 # => [nil, 1, 2, 3, 5]
37
+ ta << :a # ArgumentError: assigned item(s) should be of the type Fixnum
38
+ ta << nil # => [nil, 1, 2, 3, 5, nil]
39
+
40
+ ta = TypedArray[1, :a, 'b'] # ArgumentError: all arguments should be of the same type
41
+
42
+ ta = TypedArray[nil, nil] # ArgumentError: [] constructor requires at least one non-nil element
43
+ ```
44
+
45
+ If it is created without elements than the first argument of constructor call should be the class of its future elements.
46
+
47
+ ```ruby
48
+ ta = TypedArray.new(Fixnum) # []
49
+ ta.item_class # => Fixnum
50
+ ta << nil # => [nil]
51
+ ta << 1 # => [nil, 1]
52
+ ta << :a # ArgumentError: assigned item(s) should be of the type Fixnum
53
+ ```
54
+
55
+ ### Comparison
56
+
57
+ Two `TypedArray`s are considered as equal if and only if both of them have the same `:item_class` and elements.
58
+ `TypedArray` is considered as equal (in terms of :== operator) to an ordinary `Array` if they have the same elements.
59
+ `TypedArray` is always considered as non-equal (in terms of :eql? operator) to an ordinary `Array`.
60
+
61
+ ```ruby
62
+ TypedArray[1, 2, 3] == TypedArray[1, 2, 3] # => true
63
+ TypedArray[1, 2, 3] == TypedArray[1, 2, 4] # => false
64
+ TypedArray.new(Fixnum) == TypedArray.new(Fixnum) # => true
65
+ TypedArray.new(Fixnum) == TypedArray.new(Symbol) # => false
66
+ TypedArray[1, 2, 3] == [1, 2, 3] # => true
67
+ TypedArray[1, 2, 3] == [1, 2, 4] # => false
68
+ TypedArray.new(Fixnum) == [] # => true
69
+ TypedArray[1, 2, 3].eql? TypedArray[1, 2, 3] # => true
70
+ TypedArray[1, 2, 3].eql? [1, 2, 3] # => false
71
+ ```
72
+
73
+ ### Inherited Array behaviour
74
+
75
+ All `Array` methods that are supposed to return an array/slice of elements of original array will returns `TypedArray` in case of `TypedArray`.
76
+ ```ruby
77
+ TypedArray[1, 2, 3][1..2] # => [2, 3]
78
+ TypedArray[1, 2, 3][1..2].class # => TypedArray
79
+ TypedArray[1, 2, 3][1..2].item_class # => Fixnum
80
+ ```
81
+
82
+ Any operations between `TypedArray`s with the same `:item_class` will return `TypedArray`.
83
+ Any operations between `TypedArray`s with different `:item_class`es will return `Array`.
84
+ Any operations between `TypedArray` and `Array` will return `Array`.
85
+
86
+ ```ruby
87
+ TypedArray[1, 2, 3] | TypedArray[2, 4] # => [1, 2, 3, 4]
88
+ (TypedArray[1, 2, 3] | TypedArray[2, 4]).class # => TypedArray
89
+ (TypedArray[1, 2, 3] | TypedArray[2, 4]).item_class # => Fixnum
90
+
91
+ TypedArray[1, 2, 3] | TypedArray[:a, :b] # => [1, 2, 3, :a, :b]
92
+ (TypedArray[1, 2, 3] | TypedArray[:a, :b]).class # => Array
93
+ (TypedArray[1, 2, 3] | TypedArray[:a, :b]).item_class # NoMethodError: undefined method `item_class' for [1, 2, 3, :a, :b]:Array
94
+
95
+ TypedArray[1, 2, 3] | [2, 4] # => [1, 2, 3, 4]
96
+ (TypedArray[1, 2, 3] | [2, 4]).class # => Array
97
+ (TypedArray[1, 2, 3] | [2, 4]).item_class # NoMethodError: undefined method `item_class' for [1, 2, 3, :a, :b]:Array
98
+
99
+ TypedArray[1, 2, 3] | [:a, :b] # => [1, 2, 3, :a, :b]
100
+ (TypedArray[1, 2, 3] | [:a, :b]).class # => Array
101
+ (TypedArray[1, 2, 3] | [:a, :b]).item_class # NoMethodError: undefined method `item_class' for [1, 2, 3, :a, :b]:Array
102
+ ```
103
+
104
+ ## Development
105
+
106
+ 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.
107
+
108
+ 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).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/akoltun/typed_array.
113
+
114
+ ## License
115
+
116
+ 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 "typed_array"
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
+ class TypedArray < Array
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,241 @@
1
+ require "typed_array/version"
2
+
3
+ class TypedArray < Array
4
+ attr_reader :item_class
5
+
6
+ def self.[](*args)
7
+ klasses = args.compact.map(&:class).uniq
8
+ raise ArgumentError, '[] constructor requires at least one non-nil element' if klasses.empty?
9
+ raise ArgumentError, 'all arguments should be of the same type' if klasses.size > 1
10
+ self.new(klasses.first).push(*args)
11
+ end
12
+
13
+ def initialize(item_class, *args)
14
+ @item_class = item_class
15
+ super(*args)
16
+ end
17
+
18
+ def ==(item)
19
+ (!item.is_a?(TypedArray) || item_class == item.item_class) && super
20
+ end
21
+
22
+ def eql?(item)
23
+ item.is_a?(TypedArray) && item_class == item.item_class && super
24
+ end
25
+
26
+ def [](*args)
27
+ if args.size == 2 || (args.size == 1 && args.first.is_a?(Range))
28
+ self.class.new(item_class).push(*super)
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ def []=(*args)
35
+ if (args.size == 3 || (args.size == 2 && args.first.is_a?(Range))) && args.last.is_a?(Array)
36
+ validate_assigned_items(args.last)
37
+ else
38
+ validate_assigned_items([args.last])
39
+ end
40
+
41
+ super
42
+ end
43
+
44
+ def concat(items)
45
+ validate_assigned_items(items)
46
+ super
47
+ end
48
+
49
+ def <<(item)
50
+ validate_assigned_items([item])
51
+ super
52
+ end
53
+
54
+ def insert(index, *items)
55
+ validate_assigned_items(items)
56
+ super
57
+ end
58
+
59
+ def push(*items)
60
+ validate_assigned_items(items)
61
+ super
62
+ end
63
+
64
+ def unshift(*items)
65
+ validate_assigned_items(items)
66
+ super
67
+ end
68
+
69
+ def pop(*args)
70
+ if args.size > 0
71
+ TypedArray.new(item_class).concat(super)
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ def shift(*args)
78
+ if args.size > 0
79
+ TypedArray.new(item_class).concat(super)
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ def reverse
86
+ TypedArray.new(item_class).concat(super)
87
+ end
88
+
89
+ def rotate(count=1)
90
+ TypedArray.new(item_class).concat(super)
91
+ end
92
+
93
+ def sort(*args)
94
+ TypedArray.new(item_class).concat(super)
95
+ end
96
+
97
+ def sort_by(&block)
98
+ if block_given?
99
+ TypedArray.new(item_class).concat(super)
100
+ else
101
+ super
102
+ end
103
+ end
104
+
105
+ def select(&block)
106
+ if block_given?
107
+ TypedArray.new(item_class).concat(super)
108
+ else
109
+ super
110
+ end
111
+ end
112
+
113
+ def values_at(*args)
114
+ TypedArray.new(item_class).concat(super)
115
+ end
116
+
117
+ def reject(&block)
118
+ if block_given?
119
+ TypedArray.new(item_class).concat(super)
120
+ else
121
+ super
122
+ end
123
+ end
124
+
125
+ def replace(items)
126
+ validate_assigned_items(items)
127
+ super
128
+ end
129
+
130
+ def fill(*args, &block)
131
+ if block_given?
132
+ super(*args) do |index|
133
+ value = block.call(index)
134
+ validate_assigned_items([value])
135
+ value
136
+ end
137
+ else
138
+ validate_assigned_items([args.first])
139
+ super
140
+ end
141
+ end
142
+
143
+ def slice(*args)
144
+ copy_item_class(super)
145
+ end
146
+
147
+ def slice!(*args)
148
+ copy_item_class(super)
149
+ end
150
+
151
+ def +(items)
152
+ validate_assigned_items(items)
153
+ TypedArray.new(item_class).concat(super)
154
+ end
155
+
156
+ def *(*args)
157
+ copy_item_class(super)
158
+ end
159
+
160
+ def -(items)
161
+ TypedArray.new(item_class).concat(super)
162
+ end
163
+
164
+ def &(items)
165
+ TypedArray.new(item_class).concat(super)
166
+ end
167
+
168
+ def |(items)
169
+ if items.is_a?(TypedArray) && items.item_class == item_class
170
+ TypedArray.new(item_class).concat(super)
171
+ else
172
+ super
173
+ end
174
+ end
175
+
176
+ def uniq(&block)
177
+ copy_item_class(super)
178
+ end
179
+
180
+ def compact
181
+ TypedArray.new(item_class).concat(super)
182
+ end
183
+
184
+ def shuffle(*args)
185
+ TypedArray.new(item_class).concat(super)
186
+ end
187
+
188
+ def sample(*args)
189
+ if args.size > 0 && !args.first.is_a?(Hash)
190
+ TypedArray.new(item_class).concat(super)
191
+ else
192
+ super
193
+ end
194
+ end
195
+
196
+ def product(*args)
197
+ if args.all? { |items| items.is_a?(TypedArray) && items.item_class == item_class }
198
+ super.map { |items| TypedArray.new(item_class).concat(items) }
199
+ else
200
+ super
201
+ end
202
+ end
203
+
204
+ def take(*args)
205
+ TypedArray.new(item_class).concat(super)
206
+ end
207
+
208
+ def take_while(&block)
209
+ if block_given?
210
+ TypedArray.new(item_class).concat(super)
211
+ else
212
+ super
213
+ end
214
+ end
215
+
216
+ def drop(*args)
217
+ TypedArray.new(item_class).concat(super)
218
+ end
219
+
220
+ def drop_while(&block)
221
+ if block_given?
222
+ TypedArray.new(item_class).concat(super)
223
+ else
224
+ super
225
+ end
226
+ end
227
+
228
+ private
229
+
230
+ def validate_assigned_items(items)
231
+ unless items.all? { |item| item.nil? || item.class == item_class }
232
+ raise ArgumentError, "assigned item(s) should be of the type #{item_class}"
233
+ end
234
+ end
235
+
236
+ def copy_item_class(typed_array)
237
+ typed_array.instance_variable_set(:@item_class, item_class) if typed_array.is_a? TypedArray
238
+ typed_array
239
+ end
240
+
241
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "typed_array/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "typed_array"
8
+ spec.version = TypedArray::VERSION
9
+ spec.authors = ["Alexander Koltun"]
10
+ spec.email = ["alexander.koltun@gmail.com"]
11
+
12
+ spec.summary = %q{Array with element strict typing}
13
+ spec.homepage = "https://github.com/akoltun/typed_array"
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
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typed_array
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Koltun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-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.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: 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:
56
+ email:
57
+ - alexander.koltun@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
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/typed_array.rb
73
+ - lib/typed_array/version.rb
74
+ - typed_array.gemspec
75
+ homepage: https://github.com/akoltun/typed_array
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ allowed_push_host: https://rubygems.org
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.7.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Array with element strict typing
100
+ test_files: []