ruby-obj 1.0.0

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.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.md +21 -0
  4. data/README.md +68 -0
  5. data/lib/obj.rb +38 -0
  6. data/lib/obj/version.rb +3 -0
  7. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee90a0b8dbce2e44efb1efc814cc6eec5dc90966
4
+ data.tar.gz: 9d57124f917b6d266020d2886aa547024397a023
5
+ SHA512:
6
+ metadata.gz: aeaf1e1455c904936fb76ef31c363c13adb0a8932f55114324e875d604071ae29bc5c1bec55a37725906623d2591067c331aac2bb283b30fdd42ea8f1e342a52
7
+ data.tar.gz: 3624e4d3ee608dbf3d44a930913d36611a72dd23c80fd28d2350514907e737f2ccd4c69874d46d093e70abcb68979b88df8280e13a24a30ca911d1131a62533c
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT LICENSE
2
+
3
+ Copyright (c) 2019 Sven Fuchs <me@svenfuchs.com>
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,68 @@
1
+ # Obj
2
+
3
+ A Struct replacement that allows default arguments, and omits the
4
+ hash-style API that Struct implements.
5
+
6
+ ## Installation
7
+
8
+ ```
9
+ gem install ruby-obj
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+ class One < Obj.new(:one, two: :default)
16
+ end
17
+
18
+ obj = One.new(1)
19
+ obj.one # => 1
20
+ obj.one? # => true
21
+ obj.two # => :default
22
+ obj.two? # => true
23
+
24
+ obj = One.new(nil)
25
+ obj.one? # => false
26
+ ```
27
+
28
+ Modules included to `Obj` are propagated to all instances:
29
+
30
+ ```ruby
31
+ module Foo
32
+ def foo
33
+ end
34
+ end
35
+
36
+ Obj.include(Foo)
37
+
38
+ class One < Obj.new(:one)
39
+ end
40
+
41
+ one = One.new(1)
42
+ one.respond_to?(:foo) # => true
43
+ ```
44
+
45
+ # Benchmark
46
+
47
+ `Obj` is marginally slower than `Struct` (which is implemented in C):
48
+
49
+ ```ruby
50
+ require 'benchmark'
51
+ require 'obj'
52
+
53
+ n = 1_000_000
54
+
55
+ str = Struct.new(:foo)
56
+ obj = Obj.new(:foo)
57
+
58
+ Benchmark.bm(10) do |b|
59
+ b.report('str:') { n.times { str.new(:foo).foo } }
60
+ b.report('obj:') { n.times { obj.new(:foo).foo } }
61
+ end
62
+ ```
63
+
64
+ ```
65
+ user system total real
66
+ str: 0.180000 0.000000 0.180000 ( 0.174254)
67
+ obj: 0.180000 0.000000 0.180000 ( 0.181985)
68
+ ```
data/lib/obj.rb ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ class Obj
3
+ class << self
4
+ def include(*modules)
5
+ super
6
+ includes.concat(modules)
7
+ end
8
+
9
+ def includes
10
+ @includes ||= []
11
+ end
12
+
13
+ def new(*attrs, &block)
14
+ defaults = attrs.last.is_a?(Hash) ? attrs.pop : {}
15
+
16
+ Class.new do
17
+ include *Obj.includes if Obj.includes.any?
18
+
19
+ args = attrs + defaults.map(&->(pair) { '%s = %p' % pair })
20
+ init = attrs.map(&->(name) { '@%s = %s' % [name, name] })
21
+ init = init + defaults.map(&->(pair) { '@%s = %s || %p' % [pair.first, *pair] })
22
+
23
+ class_eval %(
24
+ def initialize(#{args.join(', ')})
25
+ #{init.join("\n")}
26
+ end
27
+ )
28
+
29
+ attrs.concat(defaults.keys).each do |attr|
30
+ attr_accessor *attr
31
+ define_method(:"#{attr}?") { !!send(attr) }
32
+ end
33
+
34
+ class_eval &block if block
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Obj
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-obj
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sven Fuchs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Struct replacement with argument defaults.
14
+ email:
15
+ - me@svenfuchs.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - LICENSE.md
22
+ - README.md
23
+ - lib/obj.rb
24
+ - lib/obj/version.rb
25
+ homepage: https://github.com/svenfuchs/obj
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.13
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Struct replacement with argument defaults
49
+ test_files: []