finer_struct 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -2
- data/README.md +30 -16
- data/lib/finer_struct/immutable.rb +9 -8
- data/lib/finer_struct/mutable.rb +9 -3
- data/lib/finer_struct/named.rb +7 -9
- data/lib/finer_struct/version.rb +1 -1
- data/spec/finer_struct/immutable_spec.rb +5 -1
- data/spec/finer_struct/shared_examples.rb +19 -2
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80dfaa74d9276f522ca1cf4e39175f42b8e679bc
|
4
|
+
data.tar.gz: 78595752aa946668b311fced58235745a0d0025e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef9af062326f3afb4bd18b9fd20eaea8472ef7062518eaf010a8616b0e9f77742c4bcc1f1a68e36e6cdd63940642371e403cac86f9b8aa79d950b3f3b2c861c4
|
7
|
+
data.tar.gz: 1d8d5dbb7c2ffbb279e92dfe829d7c598ad3d878b259bbd8514ac8b973ed30095287c0e820fae5bc391f05855714461d14c9c27b270f42ace0301ad72212fb46
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
A nicer replacement for Ruby's Struct and OpenStruct.
|
4
4
|
|
5
|
-
Isn't it annoying how `OpenStruct.new` takes a hash, but the
|
5
|
+
Isn't it annoying how `OpenStruct.new` takes a hash, but the arguments to `Struct.new` have to be ordered?
|
6
6
|
|
7
7
|
Do you find you always use an `OpenStruct` when you don't really care if it's open, you just want an easy way to create an object from a hash?
|
8
8
|
|
@@ -16,15 +16,21 @@ Have you ever wished you could create a `Struct` or `OpenStruct`, but make it im
|
|
16
16
|
|
17
17
|
Add this line to your application's Gemfile:
|
18
18
|
|
19
|
-
|
19
|
+
```ruby
|
20
|
+
gem 'finer_struct'
|
21
|
+
```
|
20
22
|
|
21
23
|
And then execute:
|
22
24
|
|
23
|
-
|
25
|
+
```bash
|
26
|
+
$ bundle
|
27
|
+
```
|
24
28
|
|
25
29
|
Or install it yourself as:
|
26
30
|
|
27
|
-
|
31
|
+
```bash
|
32
|
+
$ gem install finer_struct
|
33
|
+
```
|
28
34
|
|
29
35
|
## Usage
|
30
36
|
|
@@ -34,18 +40,22 @@ Immutable structs can't have their attributes changed after creation.
|
|
34
40
|
|
35
41
|
#### Anonymous immutable structs
|
36
42
|
|
37
|
-
|
43
|
+
```ruby
|
44
|
+
struct = FinerStruct::Immutable.new(a: 1, b: 2)
|
38
45
|
|
39
|
-
|
40
|
-
|
41
|
-
|
46
|
+
struct.a # => 1
|
47
|
+
struct.b # => 2
|
48
|
+
struct.a = 3 # Exception!
|
49
|
+
```
|
42
50
|
|
43
51
|
#### Named immutable structs
|
44
52
|
|
45
|
-
|
46
|
-
|
53
|
+
```ruby
|
54
|
+
class MyStruct < FinerStruct::Immutable(:a, :b); end
|
55
|
+
struct = MyStruct.new(a: 1, b: 2)
|
47
56
|
|
48
|
-
|
57
|
+
MyStruct.new(a: 1, b: 2, c: 3) # Exception!
|
58
|
+
```
|
49
59
|
|
50
60
|
### Mutable Structs
|
51
61
|
|
@@ -53,15 +63,19 @@ Mutable structs let you assign attributes at any time.
|
|
53
63
|
|
54
64
|
#### Anonymous mutable structs
|
55
65
|
|
56
|
-
|
66
|
+
```ruby
|
67
|
+
struct = FinerStruct::Mutable.new(a: 1, b: 2)
|
57
68
|
|
58
|
-
|
59
|
-
|
69
|
+
struct.a = 3
|
70
|
+
struct.a # => 3
|
71
|
+
```
|
60
72
|
|
61
73
|
#### Named mutable structs
|
62
74
|
|
63
|
-
|
64
|
-
|
75
|
+
```ruby
|
76
|
+
class MyStruct < FinerStruct::Mutable(:a, :b); end
|
77
|
+
struct = MyStruct.new(a: 1, b: 2)
|
78
|
+
```
|
65
79
|
|
66
80
|
## Contributing
|
67
81
|
|
@@ -15,17 +15,18 @@ module FinerStruct
|
|
15
15
|
super
|
16
16
|
end
|
17
17
|
end
|
18
|
-
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def respond_to?(method)
|
20
|
+
@attributes.has_key?(method) || super
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
freeze
|
27
|
-
end
|
23
|
+
def to_hash
|
24
|
+
@attributes
|
28
25
|
end
|
29
26
|
end
|
30
27
|
|
28
|
+
def self.Immutable(*attribute_names)
|
29
|
+
Named.build_class(Immutable, attribute_names)
|
30
|
+
end
|
31
|
+
|
31
32
|
end
|
data/lib/finer_struct/mutable.rb
CHANGED
@@ -18,6 +18,14 @@ module FinerStruct
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
def respond_to?(method)
|
22
|
+
@attributes.has_key?(method) || super
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_hash
|
26
|
+
@attributes.dup
|
27
|
+
end
|
28
|
+
|
21
29
|
private
|
22
30
|
|
23
31
|
def is_assigment?(method)
|
@@ -30,9 +38,7 @@ module FinerStruct
|
|
30
38
|
end
|
31
39
|
|
32
40
|
def self.Mutable(*attribute_names)
|
33
|
-
Named.build_class(attribute_names)
|
34
|
-
attr_accessor(*attribute_names)
|
35
|
-
end
|
41
|
+
Named.build_class(Mutable, attribute_names)
|
36
42
|
end
|
37
43
|
|
38
44
|
end
|
data/lib/finer_struct/named.rb
CHANGED
@@ -3,21 +3,19 @@ module FinerStruct
|
|
3
3
|
module Named
|
4
4
|
|
5
5
|
def initialize(attributes = {})
|
6
|
-
attributes.
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
instance_variable_set("@#{name}", value)
|
6
|
+
unknown_attributes = attributes.keys - attribute_names
|
7
|
+
unless unknown_attributes.empty?
|
8
|
+
raise(ArgumentError, "unknown attributes: #{unknown_attributes.join(', ')}")
|
11
9
|
end
|
10
|
+
|
11
|
+
super(attributes)
|
12
12
|
end
|
13
13
|
|
14
|
-
def self.build_class(
|
15
|
-
Class.new do
|
14
|
+
def self.build_class(superclass, attribute_names)
|
15
|
+
Class.new(superclass) do
|
16
16
|
define_method(:attribute_names, -> { attribute_names })
|
17
17
|
|
18
18
|
include Named
|
19
|
-
|
20
|
-
class_eval(&block)
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
data/lib/finer_struct/version.rb
CHANGED
@@ -3,10 +3,27 @@ shared_examples "a struct" do
|
|
3
3
|
expect(subject.a).to eq(1)
|
4
4
|
expect(subject.b).to eq(2)
|
5
5
|
end
|
6
|
+
|
7
|
+
it "responds to attribute names" do
|
8
|
+
expect(subject).to respond_to :a
|
9
|
+
expect(subject).to respond_to :b
|
10
|
+
end
|
11
|
+
|
12
|
+
it "responds to :class" do
|
13
|
+
expect(subject).to respond_to :class
|
14
|
+
end
|
15
|
+
|
16
|
+
it "doesn't respond to attribute names that the struct doesn't have" do
|
17
|
+
expect(subject).not_to respond_to :c
|
18
|
+
end
|
19
|
+
|
20
|
+
it "converts to a hash" do
|
21
|
+
expect(subject.to_hash).to eq(a: 1 ,b: 2)
|
22
|
+
end
|
6
23
|
end
|
7
24
|
|
8
25
|
shared_examples "a named struct" do
|
9
|
-
it "complains if you set
|
10
|
-
expect { klass.new(c: 3) }.to raise_error(ArgumentError, "
|
26
|
+
it "complains if you set attributes that the struct doesn't have" do
|
27
|
+
expect { klass.new(c: 3, d: 4) }.to raise_error(ArgumentError, "unknown attributes: c, d")
|
11
28
|
end
|
12
29
|
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finer_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Yandell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-17 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: 10.0.4
|
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: 10.0.4
|
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: 2.13.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: 2.13.0
|
55
55
|
description: A nicer replacement for Ruby's Struct and OpenStruct
|
@@ -59,7 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
62
|
+
- ".gitignore"
|
63
63
|
- Gemfile
|
64
64
|
- LICENSE.txt
|
65
65
|
- README.md
|
@@ -83,17 +83,17 @@ require_paths:
|
|
83
83
|
- lib
|
84
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- -
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
|
-
- -
|
91
|
+
- - ">="
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.4.5
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: A nicer replacement for Ruby's Struct and OpenStruct
|