strict_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 +4 -4
- data/.travis.yml +3 -0
- data/Gemfile.travis +7 -0
- data/README.md +7 -2
- data/lib/strict_struct/version.rb +1 -1
- data/lib/strict_struct.rb +6 -4
- data/spec/strict_struct_spec.rb +20 -1
- metadata +15 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52edb2316281bbc0e51cf385831f5e3efd962f88
|
4
|
+
data.tar.gz: f98b762c30921c057530eaa6772af5480832b87d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a95119689d525fc07f9dc5500f3795056abd0094ee23fe29900d228e2948488fb4126ce309231122e73e284c4397fef8075321f79fdf6717d8f037e20463bd88
|
7
|
+
data.tar.gz: 90aeb68616e42f04f19f248ac8e22d23bf7945de4d1994b50871e1a559390194d4c2f9a18bcdda63280696a7d4723b51420512052d4a93041fe40c67da174390
|
data/.travis.yml
CHANGED
data/Gemfile.travis
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# StrictStruct [![Build Status](https://travis-ci.org/markijbema/strict_struct.png)](https://travis-ci.org/markijbema/strict_struct) [![Code Climate](https://codeclimate.com/github/markijbema/strict_struct.png)](https://codeclimate.com/github/markijbema/strict_struct)
|
1
|
+
# StrictStruct [![Build Status](https://travis-ci.org/markijbema/strict_struct.png)](https://travis-ci.org/markijbema/strict_struct) [![Code Climate](https://codeclimate.com/github/markijbema/strict_struct.png)](https://codeclimate.com/github/markijbema/strict_struct) [![Gem Version](https://badge.fury.io/rb/strict_struct.png)](http://badge.fury.io/rb/strict_struct) [![Gem Version](https://badge.fury.io/rb/strict_struct.png)](http://badge.fury.io/rb/strict_struct)
|
2
2
|
|
3
3
|
This gems aims to provide a modern version of Struct.
|
4
4
|
While Struct is a nice easy way to create a light-weight
|
@@ -23,13 +23,15 @@ Rectangle = StrictStruct.new(:x, :y) do
|
|
23
23
|
end
|
24
24
|
```
|
25
25
|
|
26
|
-
This would conceptually create the following object:
|
26
|
+
This would conceptually create something like the following object:
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
class Rectange
|
30
30
|
attr_reader :x, :y
|
31
31
|
|
32
32
|
def initialize(x:, y:)
|
33
|
+
@x = x
|
34
|
+
@y = y
|
33
35
|
end
|
34
36
|
|
35
37
|
def area
|
@@ -38,6 +40,9 @@ class Rectange
|
|
38
40
|
end
|
39
41
|
```
|
40
42
|
|
43
|
+
Since this is meant to create immutable objects, the values aren't actually assigned to instance variables but safed internally in a hash.
|
44
|
+
|
45
|
+
|
41
46
|
## Installation
|
42
47
|
|
43
48
|
Add this line to your application's Gemfile:
|
data/lib/strict_struct.rb
CHANGED
@@ -24,16 +24,18 @@ module StrictStruct
|
|
24
24
|
@@attributes = attributes
|
25
25
|
|
26
26
|
define_method :initialize do |hash={}|
|
27
|
-
|
28
|
-
|
27
|
+
Helper.validate_arguments(hash.keys, @@attributes)
|
28
|
+
@_strict_struct_hash = Hash[hash.to_a].freeze
|
29
29
|
end
|
30
30
|
|
31
31
|
attributes.each do |attribute|
|
32
|
-
define_method(attribute)
|
32
|
+
define_method(attribute) do
|
33
|
+
@_strict_struct_hash[attribute]
|
34
|
+
end
|
33
35
|
end
|
34
36
|
|
35
37
|
def to_h
|
36
|
-
@
|
38
|
+
Hash[@_strict_struct_hash.to_a]
|
37
39
|
end
|
38
40
|
|
39
41
|
def == (other)
|
data/spec/strict_struct_spec.rb
CHANGED
@@ -98,10 +98,29 @@ describe 'StrictStruct' do
|
|
98
98
|
|
99
99
|
expect do
|
100
100
|
a_foo.y
|
101
|
-
end.to raise_error NoMethodError
|
101
|
+
end.to raise_error NoMethodError
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
+
describe 'immutability' do
|
106
|
+
let(:klass) { StrictStruct.new(:x) }
|
107
|
+
|
108
|
+
it "isn't broken by modifiying the input hash" do
|
109
|
+
params = {x: 'foo'}
|
110
|
+
k = klass.new(params)
|
111
|
+
params[:x] = 'bar'
|
112
|
+
|
113
|
+
k.x.should eq 'foo'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "isn't broken by modifying output of to_h" do
|
117
|
+
k = klass.new(x: 'foo')
|
118
|
+
hash = k.to_h
|
119
|
+
hash[:x] = 'bar'
|
120
|
+
|
121
|
+
k.x.should eq 'foo'
|
122
|
+
end
|
123
|
+
end
|
105
124
|
|
106
125
|
describe '#to_h' do
|
107
126
|
it "returns all the values the object is defined by" do
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strict_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark IJbema
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
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
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Extremely simple value objects
|
@@ -59,9 +59,10 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
-
|
63
|
-
-
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
64
|
- Gemfile
|
65
|
+
- Gemfile.travis
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
67
68
|
- Rakefile
|
@@ -80,20 +81,21 @@ require_paths:
|
|
80
81
|
- lib
|
81
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
83
|
requirements:
|
83
|
-
- -
|
84
|
+
- - '>='
|
84
85
|
- !ruby/object:Gem::Version
|
85
86
|
version: '0'
|
86
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
88
|
requirements:
|
88
|
-
- -
|
89
|
+
- - '>='
|
89
90
|
- !ruby/object:Gem::Version
|
90
91
|
version: '0'
|
91
92
|
requirements: []
|
92
93
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.
|
94
|
+
rubygems_version: 2.0.3
|
94
95
|
signing_key:
|
95
96
|
specification_version: 4
|
96
97
|
summary: StrictStruct is an immutable, named parameters based, replacement for Struct
|
97
98
|
test_files:
|
98
99
|
- spec/spec_helper.rb
|
99
100
|
- spec/strict_struct_spec.rb
|
101
|
+
has_rdoc:
|