simple_struct 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/lib/simple_struct.rb +78 -0
- data/simple_struct.gemspec +17 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jared Grippe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# SimpleStruct
|
2
|
+
|
3
|
+
SimpleStruct is a lighter weight struct then ruby's standard struct in that it doest not include Enumerable.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_struct'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_struct
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
class Car < SimpleStruct.new(:color, :size)
|
22
|
+
end
|
23
|
+
|
24
|
+
# or
|
25
|
+
|
26
|
+
Car = SimpleStruct.new(:color, :size) do
|
27
|
+
end
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
class SimpleStruct
|
2
|
+
|
3
|
+
VERSION = "0.0.1"
|
4
|
+
|
5
|
+
def self.new *members, &block
|
6
|
+
subclass = Class.new(self)
|
7
|
+
subclass.define_singleton_method(:new, self.superclass.method(:new))
|
8
|
+
subclass.define_singleton_method(:members){ members }
|
9
|
+
subclass.class_eval(&block) if block_given?
|
10
|
+
subclass
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize *args
|
14
|
+
self.class.members.zip(args).each do |member, value|
|
15
|
+
instance_variable_set(:"@#{member}", value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.inspect
|
20
|
+
%[#{to_s}(#{members*', '})]
|
21
|
+
end
|
22
|
+
|
23
|
+
def inspect
|
24
|
+
%(#<#{self.class}: #{self.class.members.map{|member| "#{member}=#{send(member).inspect}"}.join(', ')}>)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
eval <<-RUBY if $0 == __FILE__
|
31
|
+
|
32
|
+
require 'test/unit'
|
33
|
+
|
34
|
+
|
35
|
+
class MethodObjectTestUnitTestCase < Test::Unit::TestCase
|
36
|
+
|
37
|
+
def test_class_is_subclass_of_SimpleStruct
|
38
|
+
car_class = SimpleStruct.new(:color, :size){
|
39
|
+
attr_accessor :color
|
40
|
+
}
|
41
|
+
car_instance = car_class.new('red', 42)
|
42
|
+
|
43
|
+
assert_equal car_class.superclass, SimpleStruct
|
44
|
+
assert_equal car_class.members, [:color, :size]
|
45
|
+
assert car_instance.public_methods.include?(:color)
|
46
|
+
assert car_instance.public_methods.include?(:color=)
|
47
|
+
assert !car_instance.public_methods.include?(:size)
|
48
|
+
assert !car_instance.public_methods.include?(:size=)
|
49
|
+
assert car_instance.is_a?(SimpleStruct), "Car is not a SimpleStruct"
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
if ENV['BENCHMARK']
|
56
|
+
|
57
|
+
require 'benchmark'
|
58
|
+
|
59
|
+
ITERATIONS = 100_000
|
60
|
+
|
61
|
+
Benchmark.bmbm do |benchmark|
|
62
|
+
benchmark.report("Struct.new(:color, :size)") do
|
63
|
+
ITERATIONS.times do
|
64
|
+
Struct.new(:color, :size)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
benchmark.report("SimpleStruct.new(:color, :size)") do
|
69
|
+
ITERATIONS.times do
|
70
|
+
SimpleStruct.new(:color, :size){}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
RUBY
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/simple_struct', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jared Grippe"]
|
6
|
+
gem.email = ["jared@deadlyicon.com"]
|
7
|
+
gem.description = %q{SimpleStruct is a lighter weight struct then ruby's standard struct.}
|
8
|
+
gem.summary = %q{SimpleStruct is a lighter weight struct then ruby's standard struct.}
|
9
|
+
gem.homepage = "https://github.com/deadlyicon/simple_struct"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "simple_struct"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = SimpleStruct::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jared Grippe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: SimpleStruct is a lighter weight struct then ruby's standard struct.
|
15
|
+
email:
|
16
|
+
- jared@deadlyicon.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/simple_struct.rb
|
27
|
+
- simple_struct.gemspec
|
28
|
+
homepage: https://github.com/deadlyicon/simple_struct
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
hash: -3037540300651783389
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
hash: -3037540300651783389
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: SimpleStruct is a lighter weight struct then ruby's standard struct.
|
58
|
+
test_files: []
|