closed_struct 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7f35748d5d8a2bea68120c5548e8547e5c56db1
4
- data.tar.gz: c8fe579de16a796dc317fb4c6dc8936b304cddc1
3
+ metadata.gz: 870ae047c83a20f0836d92bdec2aefc3c40fba7a
4
+ data.tar.gz: 1ce68c69beaa55da5a6c25328b8eaf260636700b
5
5
  SHA512:
6
- metadata.gz: cb739a0b046ce1e4139a324471d5e07507456af0891778efc9e5e99605ceb5a90ea5b4fcbaad0529ff5ae95e7007d63a89bb03115aa48015fd637fe1e6434abc
7
- data.tar.gz: 6da3994c12476fd7a746ad3f1bcdf93186d627b39e3ebfcc411536460a500bd4b9057d29e1a044ef8a78878a2773ef4e2466efc5e95624ec2d069326ffeef892
6
+ metadata.gz: 5b56fcf53e6642e6e78c402b22f4f3df0af875ecb1a14bc83ff18ee596daf869ac810acf02dce50fe94bf9a4dd0a1c825dc4463e7fd62b890fe7f25da1cafee2
7
+ data.tar.gz: ba5ac81b034a84980d9fc36972cd77c3d69be72ce2c8f6aad9e1f9381643cd8c71f908ba92e8b3ddd68e94b426087c5d7958ad11ebad9196f11094d2f0ddccf4
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-19mode
7
+ - jruby-18mode
8
+ - rbx
9
+ - 1.8.7
10
+ script: "bundle exec rspec"
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # ClosedStruct
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://travis-ci.org/obrok/closed_struct.png?branch=master)](https://travis-ci.org/obrok/closed_struct)
4
+
5
+ ClosedStructs work like OpenStruct, with the exception of being immutable and not responding to methods which haven't been listed in the input hash
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,12 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ ```ruby
24
+
25
+ ClosedStruct.new(:a => :b).a # => :b
26
+ ClosedStruct.new("a" => :b).a # => :b
27
+ ClosedStruct.new(:a => :b).c # => raises NoMethodError
28
+ ```
22
29
 
23
30
  ## Contributing
24
31
 
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
data/lib/closed_struct.rb CHANGED
@@ -1,5 +1,25 @@
1
1
  require "closed_struct/version"
2
2
 
3
- module ClosedStruct
4
- # Your code goes here...
3
+ class ClosedStruct
4
+ def initialize(contents)
5
+ @contents = contents
6
+
7
+ singleton_class = (class << self; self; end)
8
+ @contents.each do |key, value|
9
+ singleton_class.send(:define_method, key) { value }
10
+ end
11
+ end
12
+
13
+ def to_h
14
+ @contents
15
+ end
16
+
17
+ def hash
18
+ @contents.hash
19
+ end
20
+
21
+ def ==(other)
22
+ (other.class == self.class) && (other.to_h == self.to_h)
23
+ end
24
+ alias_method :eql?, :==
5
25
  end
@@ -1,3 +1,3 @@
1
- module ClosedStruct
2
- VERSION = "0.0.1"
1
+ class ClosedStruct
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,31 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "closed_struct"
4
+
5
+ describe ClosedStruct do
6
+ it "delegates method calls to hash lookups" do
7
+ expect(ClosedStruct.new(:a => :b).a).to eq(:b)
8
+ end
9
+
10
+ it "does not delegate for keys that don't exist in the input" do
11
+ expect { ClosedStruct.new(:a => :b).c }.to raise_error(NoMethodError)
12
+ end
13
+
14
+ it "is equal for the same arguments" do
15
+ first = ClosedStruct.new(:a => :b)
16
+ second = ClosedStruct.new(:a => :b)
17
+
18
+ expect(first == second).to be_true
19
+ expect(first.eql?(second)).to be_true
20
+ expect(first.hash).to eq(second.hash)
21
+ end
22
+
23
+ it "is unequal for different arguments" do
24
+ first = ClosedStruct.new(:a => :b)
25
+ second = ClosedStruct.new(:a => :c)
26
+
27
+ expect(first == second).to be_false
28
+ expect(first.eql?(second)).to be_false
29
+ expect(first.hash).not_to eq(second.hash)
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: closed_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paweł Obrok
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.6'
19
+ version: '1.5'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.6'
26
+ version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: ClosedStructs work like OpenStruct, with the exception of being immutable
42
56
  and not responding to methods which haven't been listed in the input hash
43
57
  email:
@@ -47,6 +61,7 @@ extensions: []
47
61
  extra_rdoc_files: []
48
62
  files:
49
63
  - ".gitignore"
64
+ - ".travis.yml"
50
65
  - Gemfile
51
66
  - LICENSE.txt
52
67
  - README.md
@@ -54,6 +69,7 @@ files:
54
69
  - closed_struct.gemspec
55
70
  - lib/closed_struct.rb
56
71
  - lib/closed_struct/version.rb
72
+ - spec/closed_struct_spec.rb
57
73
  homepage: ''
58
74
  licenses:
59
75
  - MIT
@@ -78,5 +94,6 @@ rubygems_version: 2.2.2
78
94
  signing_key:
79
95
  specification_version: 4
80
96
  summary: An immutable, strict version of OpenStruct
81
- test_files: []
97
+ test_files:
98
+ - spec/closed_struct_spec.rb
82
99
  has_rdoc: