bound 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 +29 -0
- data/Rakefile +1 -0
- data/bound.gemspec +22 -0
- data/lib/bound.rb +103 -0
- data/lib/bound/version.rb +3 -0
- data/spec/bound_spec.rb +63 -0
- data/spec/spec_helper.rb +31 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 734a7fe5bfbce0c1bfc3b0aa576dddaf6f7a74ed
|
4
|
+
data.tar.gz: 45a61cc26cce9f38c6428b2043b502ffa66cf406
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9471823d8fcedf11584e256e35551b999aa7fefa2b845ce6325bb14929b29a12ab955520cf10d7acbf3d388ec726364744e136a4a75268f36dad56780cb28674
|
7
|
+
data.tar.gz: ffd8473401383c0e8f3d2ff0659363795411f751d40690b852f3e340658fe1b55f5c10bacfd26e1ff6eade821fa2a7cae2c650bf4465db0a694a78cf5c61cf00
|
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,29 @@
|
|
1
|
+
# Bound
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bound'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bound
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bound.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bound/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bound"
|
8
|
+
spec.version = Bound::VERSION
|
9
|
+
spec.authors = ["Jakob Holderbaum", "Jan Owiesniak"]
|
10
|
+
spec.email = ["jh@neopoly.de", "jo@neopoly.de"]
|
11
|
+
spec.summary = %q{Implements a nice helper for fas boundary definitions}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
data/lib/bound.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require "bound/version"
|
2
|
+
|
3
|
+
class Bound
|
4
|
+
def self.new(*args)
|
5
|
+
new_bound_class(*args)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.new_bound_class(*attributes)
|
11
|
+
Class.new(BoundClass) do
|
12
|
+
set_attributes(*attributes)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class BoundClass
|
17
|
+
class << self
|
18
|
+
attr_accessor :attributes, :optionals
|
19
|
+
|
20
|
+
def set_attributes(*attributes)
|
21
|
+
attributes = attributes.dup
|
22
|
+
|
23
|
+
optionals = extract_optionals(attributes)
|
24
|
+
|
25
|
+
self.optionals = optionals
|
26
|
+
attr_accessor(*optionals)
|
27
|
+
|
28
|
+
self.attributes = attributes
|
29
|
+
attr_accessor(*attributes)
|
30
|
+
end
|
31
|
+
|
32
|
+
def extract_optionals(attributes)
|
33
|
+
if attributes.last.kind_of? Hash
|
34
|
+
attributes.pop[:optional]
|
35
|
+
else
|
36
|
+
[]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(hash_or_object)
|
42
|
+
build_hash(hash_or_object)
|
43
|
+
validate!
|
44
|
+
seed
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def validate!
|
50
|
+
self.class.attributes.each do |attribute|
|
51
|
+
raise ArgumentError unless @hash.key?(attribute)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def seed
|
56
|
+
HashSeeder.new(self).seed(@hash)
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_hash(hash_or_object)
|
60
|
+
case hash_or_object
|
61
|
+
when Hash
|
62
|
+
@hash = hash_or_object
|
63
|
+
else
|
64
|
+
@hash = {}
|
65
|
+
insert_attributes_into_hash(hash_or_object)
|
66
|
+
insert_optionals_into_hash(hash_or_object)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def insert_attributes_into_hash(object)
|
71
|
+
self.class.attributes.inject(@hash) do |h, attr|
|
72
|
+
begin
|
73
|
+
h[attr] = object.public_send(attr)
|
74
|
+
rescue NoMethodError
|
75
|
+
raise ArgumentError.new(attr)
|
76
|
+
end
|
77
|
+
h
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def insert_optionals_into_hash(object)
|
82
|
+
self.class.optionals.inject(@hash) do |h, attr|
|
83
|
+
begin
|
84
|
+
h[attr] = object.public_send(attr)
|
85
|
+
rescue NoMethodError
|
86
|
+
end
|
87
|
+
h
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class HashSeeder
|
93
|
+
def initialize(receiver)
|
94
|
+
@receiver = receiver
|
95
|
+
end
|
96
|
+
|
97
|
+
def seed(hash)
|
98
|
+
hash.each do |key, value|
|
99
|
+
@receiver.public_send "#{key}=", value
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/spec/bound_spec.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bound do
|
4
|
+
User = Bound.new(:name, :age)
|
5
|
+
|
6
|
+
let(:object) { HashObject.new(hash) }
|
7
|
+
let(:hash) { {:name => 'foo', :age => 23} }
|
8
|
+
|
9
|
+
it 'sets all attributes' do
|
10
|
+
[hash, object].each do |subject|
|
11
|
+
user = User.new(subject)
|
12
|
+
|
13
|
+
assert_equal hash[:name], user.name
|
14
|
+
assert_equal hash[:age], user.age
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'fails if attribute is missing' do
|
19
|
+
hash.delete :age
|
20
|
+
|
21
|
+
[hash, object].each do |subject|
|
22
|
+
assert_raises ArgumentError, subject.inspect do
|
23
|
+
User.new(subject)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'works if attribute is nil' do
|
29
|
+
hash[:age] = nil
|
30
|
+
|
31
|
+
[hash, object].each do |subject|
|
32
|
+
User.new(subject)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'optional attributes' do
|
37
|
+
UserWithoutAge = Bound.new(:name, :optional => [:age])
|
38
|
+
|
39
|
+
it 'sets optional attributes' do
|
40
|
+
[hash, object].each do |subject|
|
41
|
+
user = UserWithoutAge.new(subject)
|
42
|
+
|
43
|
+
assert_equal hash[:age], user.age
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'works if optional attribute is missing' do
|
48
|
+
hash.delete :age
|
49
|
+
|
50
|
+
[hash, object].each do |subject|
|
51
|
+
UserWithoutAge.new(subject)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'works if attribute is nil' do
|
56
|
+
hash[:age] = nil
|
57
|
+
|
58
|
+
[hash, object].each do |subject|
|
59
|
+
UserWithoutAge.new(subject)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/spec'
|
3
|
+
|
4
|
+
require 'bound'
|
5
|
+
|
6
|
+
# HashObject behaves like an OpenStruct
|
7
|
+
# but there is no method_missing involved.
|
8
|
+
#
|
9
|
+
# Missing keys in the source hash result in
|
10
|
+
# NoMethodErrors on a later call
|
11
|
+
#
|
12
|
+
# OpenStruct.new(:id => 2).name # => nil
|
13
|
+
# HashObject.new(:id => 2).name # => NoMethodError
|
14
|
+
#
|
15
|
+
class HashObject
|
16
|
+
def self.new(hash)
|
17
|
+
build_class(hash.keys).new(hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.build_class(attributes)
|
21
|
+
Class.new do
|
22
|
+
attr_accessor(*attributes)
|
23
|
+
|
24
|
+
def initialize(hash)
|
25
|
+
hash.each do |attr, value|
|
26
|
+
public_send "#{attr}=", value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bound
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakob Holderbaum
|
8
|
+
- Jan Owiesniak
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
description:
|
43
|
+
email:
|
44
|
+
- jh@neopoly.de
|
45
|
+
- jo@neopoly.de
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- .gitignore
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bound.gemspec
|
56
|
+
- lib/bound.rb
|
57
|
+
- lib/bound/version.rb
|
58
|
+
- spec/bound_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
homepage: ''
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.0.0
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Implements a nice helper for fas boundary definitions
|
84
|
+
test_files:
|
85
|
+
- spec/bound_spec.rb
|
86
|
+
- spec/spec_helper.rb
|