tiny_struct 0.0.1

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
+ SHA1:
3
+ metadata.gz: 758264eb4fc3bc300e1b10ca9189795d8e2174b0
4
+ data.tar.gz: aa152037cc0e94e9d01f122020766d7e52fe85b1
5
+ SHA512:
6
+ metadata.gz: 74379e8af24b9361e1a8308ca4309384d6dfab9c1272280eed3327810ff4cd84ee6c002b38117871a25ace233d8eb7c49875daf0a6f4b92a722708d61ca1d1da
7
+ data.tar.gz: 04caf65066d4717adf629ea998c766811d5b378d7ffd3cdbf38b63f3af6092703a5f38864b51c4b94cc23b651a6bff2c55256bee086e772557ae1cae2f591511
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/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.4
5
+ Exclude:
6
+ - 'vendor/**/*'
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm: 2.4.2
3
+ cache: bundler
4
+ script:
5
+ - bundle exec rake
6
+ - bundle exec rubocop
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tiny_struct (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.3.0)
10
+ docile (1.1.5)
11
+ json (2.1.0)
12
+ minitest (5.10.3)
13
+ parallel (1.12.0)
14
+ parser (2.4.0.0)
15
+ ast (~> 2.2)
16
+ powerpack (0.1.1)
17
+ rainbow (2.2.2)
18
+ rake
19
+ rake (12.2.1)
20
+ rubocop (0.51.0)
21
+ parallel (~> 1.10)
22
+ parser (>= 2.3.3.1, < 3.0)
23
+ powerpack (~> 0.1)
24
+ rainbow (>= 2.2.2, < 3.0)
25
+ ruby-progressbar (~> 1.7)
26
+ unicode-display_width (~> 1.0, >= 1.0.1)
27
+ ruby-progressbar (1.9.0)
28
+ simplecov (0.15.1)
29
+ docile (~> 1.1.0)
30
+ json (>= 1.8, < 3)
31
+ simplecov-html (~> 0.10.0)
32
+ simplecov-html (0.10.2)
33
+ unicode-display_width (1.3.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.16.a)
40
+ minitest (~> 5.10)
41
+ rake (~> 12.2)
42
+ rubocop (~> 0.51)
43
+ simplecov (~> 0.15)
44
+ tiny_struct!
45
+
46
+ BUNDLED WITH
47
+ 1.16.0.pre.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kevin Deisz
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,72 @@
1
+ # TinyStruct
2
+
3
+ [![Build Status](https://travis-ci.org/kddeisz/tiny_struct.svg?branch=master)](https://travis-ci.org/kddeisz/tiny_struct)
4
+
5
+ Build `Struct` classes that do less. `TinyStruct` is a very similar concept to `Struct` classes in Ruby, with a few key differences:
6
+
7
+ * In `Struct` classes all parameters are optional, in `TinyStruct` they are all required.
8
+ * In `Struct` classes each parameter is accessible through an `attr_accessor`, in `TinyStruct` they are accessible through `attr_reader`s.
9
+ * In `Struct` classes you can call all kinds of querying methods on the objects as if they were an enumerable of the various values they represent (e.g., `to_a`, `to_h`, `size`, `[]`, `values_at`, etc.), in `TinyStruct` none of these methods are defined.
10
+
11
+ Use `Struct` if you need the flexibility and extra query methods provided by the constructor. If not, consider using `TinyStruct`.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'tiny_struct'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install tiny_struct
28
+
29
+ ## Usage
30
+
31
+ ### Basic usage
32
+
33
+ Build `TinyStruct` classes like so:
34
+
35
+ ```ruby
36
+ class User < TinyStruct.new(:first_name, :last_name)
37
+ def full_name
38
+ "#{first_name} #{last_name}"
39
+ end
40
+ end
41
+ ```
42
+
43
+ Now you can build `User` objects like so:
44
+
45
+ ```ruby
46
+ user = User.new('Kevin', 'Deisz')
47
+ # => #<User @first_name="Kevin" @last_name="Deisz">
48
+ ```
49
+
50
+ ### Configuration
51
+
52
+ `TinyStruct` by default will cache each of the classes that is constructed through `TinyStruct::new` in order to deduplicate potential new classes with those that are already constructed. This cache is by default stored in memory. If you're worried about memory performance and don't mind taking a hit to performance, you can turn this off (and have it instead just loop through `ObjectSpace`) by doing the following before you configure anything:
53
+
54
+ ```ruby
55
+ TinyStruct.configure do |config|
56
+ config.cache = false
57
+ end
58
+ ```
59
+
60
+ ## Development
61
+
62
+ 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.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/tiny_struct.
69
+
70
+ ## License
71
+
72
+ 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,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
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'tiny_struct'
5
+
6
+ require 'irb'
7
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,101 @@
1
+ require 'tiny_struct/memory_cache'
2
+ require 'tiny_struct/object_space_cache'
3
+ require 'tiny_struct/version'
4
+
5
+ # A class for encompassing the simple pattern of required and ordered parameters
6
+ # and their associated reader methods. To create a new class, run:
7
+ #
8
+ # class User < TinyStruct.new(:first_name, :last_name)
9
+ # def full_name
10
+ # "#{first_name} #{last_name}"
11
+ # end
12
+ # end
13
+ #
14
+ # This will create a class that now has `attr_reader`s for `first_name` and
15
+ # `last_name`, as well as having an initializer that sets those values.
16
+ class TinyStruct
17
+ ATTRIBUTE_PATTERN = /\A[a-z][a-zA-Z0-9_]*\z/
18
+
19
+ # `true` if the members of the other `TinyStruct` instance are equal to
20
+ # the values of this `TinyStruct` instance.
21
+ def eql?(other)
22
+ other.class.respond_to?(:members) &&
23
+ self.class.members == other.class.members &&
24
+ self.class.members.all? do |attribute|
25
+ public_send(attribute) == other.public_send(attribute)
26
+ end
27
+ end
28
+ alias == eql?
29
+
30
+ def inspect
31
+ pairs =
32
+ self.class.members.map { |name| "@#{name}=#{public_send(name).inspect}" }
33
+ "#<#{self.class.name || 'TinyStruct'} #{pairs.join(' ')}>"
34
+ end
35
+ alias to_s inspect
36
+
37
+ class << self
38
+ attr_accessor :cache
39
+
40
+ # Builds a new `TinyStruct` subclass based on the given members. The
41
+ # given members must be symbols that represents names that could
42
+ # otherwise be used as argument names.
43
+ def new(*members)
44
+ success =
45
+ members.all? do |attribute|
46
+ attribute.is_a?(Symbol) && attribute.match?(ATTRIBUTE_PATTERN)
47
+ end
48
+
49
+ unless success
50
+ raise ArgumentError, 'arguments to TinyStruct::new must be valid ' \
51
+ 'attribute names represented as symbols'
52
+ end
53
+
54
+ class_cache[members] ||= define_class(members)
55
+ end
56
+
57
+ # Yields `TinyStruct` to a block such that you can toggle configuration
58
+ # settings. For example:
59
+ #
60
+ # TinyStruct.configure do |config|
61
+ # config.cache = false
62
+ # end
63
+ def configure
64
+ yield self
65
+ end
66
+
67
+ private
68
+
69
+ def define_class(members)
70
+ clazz =
71
+ Class.new(TinyStruct) do
72
+ attr_reader(*members)
73
+ define_singleton_method(:new, Object.method(:new))
74
+ define_singleton_method(:members) { members }
75
+ end
76
+
77
+ define_initialize_method(clazz, members)
78
+ clazz
79
+ end
80
+
81
+ def define_initialize_method(clazz, members)
82
+ clazz.send(:define_method, :initialize) do |*arguments|
83
+ if members.size != arguments.size
84
+ message = "wrong number of arguments (given #{arguments.size}, " \
85
+ "expected #{members.size})"
86
+ raise ArgumentError, message
87
+ end
88
+
89
+ members.zip(arguments).each do |name, value|
90
+ instance_variable_set(:"@#{name}", value)
91
+ end
92
+ end
93
+ end
94
+
95
+ def class_cache
96
+ @class_cache ||= cache ? MemoryCache.new : ObjectSpaceCache.new
97
+ end
98
+ end
99
+
100
+ self.cache = true
101
+ end
@@ -0,0 +1,24 @@
1
+ class TinyStruct
2
+ # Maintains an in-memory cache of the defined `TinyStruct` classes, such that
3
+ # classes with the same attribute list do not end up getting created twice.
4
+ #
5
+ # This cache makes performance quite a bit better since looping through
6
+ # `ObjectSpace` takes a while, but takes a hit on memory because everything
7
+ # is now stored. Also prevents the classes from being freed if they were
8
+ # created anonymously and could have otherwise have been freed.
9
+ class MemoryCache
10
+ attr_reader :cache
11
+
12
+ def initialize
13
+ @cache = {}
14
+ end
15
+
16
+ def []=(members, clazz)
17
+ cache[members] = clazz
18
+ end
19
+
20
+ def [](members)
21
+ cache[members]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ class TinyStruct
2
+ # Allows for finding previously constructed `TinyStruct` classes that contain
3
+ # a certain set of members so that they do not end up getting created twice.
4
+ #
5
+ # This cache saves some on space because we're not storing them in memory,
6
+ # but takes a slight hit to performance whenever a new class in defined.
7
+ class ObjectSpaceCache
8
+ def []=(*); end
9
+
10
+ def [](members)
11
+ ObjectSpace.each_object(TinyStruct.singleton_class).detect do |clazz|
12
+ clazz != TinyStruct && clazz.members == members
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class TinyStruct
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'tiny_struct/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'tiny_struct'
7
+ spec.version = TinyStruct::VERSION
8
+ spec.authors = ['Kevin Deisz']
9
+ spec.email = ['kevin.deisz@gmail.com']
10
+
11
+ spec.summary = 'Build Struct classes that do less.'
12
+ spec.homepage = 'https://github.com/kddeisz/tiny_struct'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.16.a'
23
+ spec.add_development_dependency 'minitest', '~> 5.10'
24
+ spec.add_development_dependency 'rake', '~> 12.2'
25
+ spec.add_development_dependency 'rubocop', '~> 0.51'
26
+ spec.add_development_dependency 'simplecov', '~> 0.15'
27
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Deisz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-30 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.a
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.a
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.51'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.51'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.15'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.15'
83
+ description:
84
+ email:
85
+ - kevin.deisz@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rubocop.yml"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/tiny_struct.rb
101
+ - lib/tiny_struct/memory_cache.rb
102
+ - lib/tiny_struct/object_space_cache.rb
103
+ - lib/tiny_struct/version.rb
104
+ - tiny_struct.gemspec
105
+ homepage: https://github.com/kddeisz/tiny_struct
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.6.13
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Build Struct classes that do less.
129
+ test_files: []