responding 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +11 -0
- data/lib/responding.rb +47 -0
- data/lib/responding/version.rb +3 -0
- data/responding.gemspec +21 -0
- data/spec/benchmark.rb +30 -0
- data/spec/helper.rb +4 -0
- data/spec/responding_spec.rb +83 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 80d0d77686bf15491c12b9871c43bd5f49650007
|
4
|
+
data.tar.gz: bcf3725b4884c1fe75bbe201a9532cb8c883026e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fed4ff92920d607211525f23a2bc59f2e04e1fdd4ee46524b358e6433355fa3118bd30f97d1eb2e8a2760d56d5b10a3abf3144532022bafa3827ead35814720c
|
7
|
+
data.tar.gz: 3f59e0dfeaedd23c16d16d85031fc792ec5ba8217bbb5bf3d391303e0fda90263ac333a969c4f0c4a406a6e733d12bb39610276ce86c2de8a055f1bff0e1ca18
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
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,48 @@
|
|
1
|
+
# Responding
|
2
|
+
|
3
|
+
Kind of a Struct implementation, but with a nested structure.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'responding'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install responding
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Just define your class:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Response = Responding.new(:id, :status => [:ok?, :message])
|
25
|
+
|
26
|
+
response = Response.new(:id => 2, :status => {:ok? => true})
|
27
|
+
|
28
|
+
response.id # => 2
|
29
|
+
response.status.ok? # => true
|
30
|
+
response.status.message # => nil
|
31
|
+
```
|
32
|
+
|
33
|
+
Every cache-invalidations occur only on creation of the dynamic class, after
|
34
|
+
this is done, instanciation is safe for your performance.
|
35
|
+
|
36
|
+
## Performance
|
37
|
+
|
38
|
+
There is a benchmark-script, you can run it like this:
|
39
|
+
|
40
|
+
`bundle exec ruby spec/benchmark.rb`
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/responding.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "responding/version"
|
2
|
+
|
3
|
+
class Responding
|
4
|
+
def self.new(*attributes)
|
5
|
+
raise ArgumentError.new('At least one attribute required') if attributes.empty?
|
6
|
+
|
7
|
+
::Class.new(Class) do
|
8
|
+
attributes.each do |attribute|
|
9
|
+
define_attribute attribute
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Class
|
15
|
+
def initialize(hash = {})
|
16
|
+
hash.each do |attr, value|
|
17
|
+
public_send :"#{attr}=", value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def self.define_attribute(attribute)
|
23
|
+
case attribute
|
24
|
+
when Symbol
|
25
|
+
attr_accessor attribute
|
26
|
+
when Hash
|
27
|
+
attribute.each do |name, nested_attributes|
|
28
|
+
nested_cls = Responding.new(*nested_attributes)
|
29
|
+
|
30
|
+
define_method :"#{name}" do
|
31
|
+
var_name = :"@#{name}"
|
32
|
+
result = instance_variable_get(var_name)
|
33
|
+
unless result
|
34
|
+
result = nested_cls.new
|
35
|
+
instance_variable_set var_name, result
|
36
|
+
end
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method :"#{name}=" do |value|
|
41
|
+
instance_variable_set :"@#{name}", nested_cls.new(value)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/responding.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'responding/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "responding"
|
8
|
+
spec.version = Responding::VERSION
|
9
|
+
spec.authors = ["Jakob Holderbaum"]
|
10
|
+
spec.email = ["jakob@featurefabrik.de"]
|
11
|
+
spec.summary = %q{Kind of a Struct implementation, but with a nested structure.}
|
12
|
+
spec.homepage = "https://github.com/featurefabrik/responding"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
end
|
data/spec/benchmark.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'responding'
|
4
|
+
|
5
|
+
StructClass = Struct.new(:a, :b)
|
6
|
+
NestedStructClass = Struct.new(:c, :d)
|
7
|
+
|
8
|
+
RespondingClass = Responding.new(:a, :b => [:c, :d])
|
9
|
+
|
10
|
+
n = 10_000
|
11
|
+
|
12
|
+
Benchmark.bmbm do |x|
|
13
|
+
x.report("struct") do
|
14
|
+
n.times do
|
15
|
+
StructClass.new(12, NestedStructClass.new(24, 48))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
x.report("ostruct") do
|
20
|
+
n.times do
|
21
|
+
OpenStruct.new(:a => 12, :b => OpenStruct.new(:c => 24, :d => 48))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
x.report("responding") do
|
26
|
+
n.times do
|
27
|
+
RespondingClass.new(:a => 12, :b => {:c => 24, :d => 48})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Responding do
|
4
|
+
describe 'init' do
|
5
|
+
it 'fails w/o attributes' do
|
6
|
+
error = assert_raises ArgumentError do
|
7
|
+
Responding.new
|
8
|
+
end
|
9
|
+
|
10
|
+
assert_match(/attribute/, error.message)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns a class object when attributes given' do
|
14
|
+
assert_kind_of Class, Responding.new(:attr1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'with plain attributes' do
|
19
|
+
it 'simply adds accessors to the class' do
|
20
|
+
cls = Responding.new(:name, :age)
|
21
|
+
|
22
|
+
obj = cls.new
|
23
|
+
|
24
|
+
refute obj.age
|
25
|
+
obj.age = 22
|
26
|
+
assert_equal 22, obj.age
|
27
|
+
|
28
|
+
refute obj.name
|
29
|
+
obj.name = "Steve"
|
30
|
+
assert_equal "Steve", obj.name
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'with hash attributes' do
|
35
|
+
it 'nests the accessors into separate classes' do
|
36
|
+
cls = Responding.new(:age, :name => [:first, :last])
|
37
|
+
|
38
|
+
obj = cls.new
|
39
|
+
|
40
|
+
refute obj.age
|
41
|
+
obj.age = 22
|
42
|
+
assert_equal 22, obj.age
|
43
|
+
|
44
|
+
assert obj.name, obj.inspect
|
45
|
+
refute obj.name.first, obj.name.inspect
|
46
|
+
refute obj.name.last, obj.name.inspect
|
47
|
+
obj.name.first = 'Steve'
|
48
|
+
assert_equal 'Steve', obj.name.first
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'constructor' do
|
53
|
+
it 'accepts a hash as argument' do
|
54
|
+
cls = Responding.new(:name, :age)
|
55
|
+
|
56
|
+
obj = cls.new(:name => 'Bob', :age => 33)
|
57
|
+
|
58
|
+
assert_equal 'Bob', obj.name
|
59
|
+
assert_equal 33, obj.age
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'accepts also a nested hash as argument' do
|
63
|
+
cls = Responding.new(:age, :name => [:first, :last])
|
64
|
+
|
65
|
+
obj = cls.new(:name => {:first => 'Bob', :last => 'Smith'}, :age => 44)
|
66
|
+
|
67
|
+
assert_equal 'Bob', obj.name.first
|
68
|
+
assert_equal 'Smith', obj.name.last
|
69
|
+
assert_equal 44, obj.age
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'nested accessor' do
|
74
|
+
it 'delegates to the class constructor' do
|
75
|
+
cls = Responding.new(:age, :name => [:first])
|
76
|
+
|
77
|
+
obj = cls.new(:name => {:first => 'Bob'}, :age => 44)
|
78
|
+
|
79
|
+
obj.name = {:first => 'Steve'}
|
80
|
+
assert_equal 'Steve', obj.name.first
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: responding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakob Holderbaum
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-06 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- jakob@featurefabrik.de
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/responding.rb
|
54
|
+
- lib/responding/version.rb
|
55
|
+
- responding.gemspec
|
56
|
+
- spec/benchmark.rb
|
57
|
+
- spec/helper.rb
|
58
|
+
- spec/responding_spec.rb
|
59
|
+
homepage: https://github.com/featurefabrik/responding
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Kind of a Struct implementation, but with a nested structure.
|
83
|
+
test_files:
|
84
|
+
- spec/benchmark.rb
|
85
|
+
- spec/helper.rb
|
86
|
+
- spec/responding_spec.rb
|