finer_struct 0.0.7 → 0.0.8

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: efc9874761281c4def8fcadbb25882b1c80946d0
4
- data.tar.gz: f2b19060bc14eb3af7c2b8d021c52f061245d141
3
+ metadata.gz: 3e77234d562a38294c85ca067153e4e7fadc076d
4
+ data.tar.gz: b143677a32cd55a07a1c14d709923f754102c20f
5
5
  SHA512:
6
- metadata.gz: c8d08f594eccc29b4ecd5a12caa9fc2c4501ca25ea2360988e059053617e554f3cc8650796b7c7ef596bab2f78ea2e6c28b5d51f08e9bdfdfa53e40678beaeb2
7
- data.tar.gz: b7aaae5aae267c3499dba6a47e21a92794d29957db92bd47382dc435775d778bdcc5b55e5fec538387fd173e32c677fa111f6928089359296d935858b1bfbd5e
6
+ metadata.gz: 2f5acd6d885fd1f0347277d6653b7365c20bb56dbf87448e15e1e486af76c394fc24001932e9ec9a7e7d1ffae1d81ff740fc51b7a8d61cb52efc7ab91272d2e6
7
+ data.tar.gz: cd328a7638545efd05cb5f353f14465ba714b8f3c4e9f9a0d089885f6fcb6fd5aae67835e3d9f48c6f951be8b6ba4f1b313f608551097ef440c777638648bf32
data/lib/finer_struct.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require "finer_struct/version"
2
- require "finer_struct/immutable"
3
- require "finer_struct/mutable"
2
+ require "finer_struct/anonymous_immutable"
3
+ require "finer_struct/anonymous_mutable"
4
+ require "finer_struct/named_immutable"
5
+ require "finer_struct/named_mutable"
4
6
 
@@ -1,8 +1,6 @@
1
- require 'finer_struct/named'
2
-
3
1
  module FinerStruct
4
-
5
2
  class Immutable
3
+
6
4
  def initialize(attributes = {})
7
5
  @attributes = attributes.dup.freeze
8
6
  freeze
@@ -23,10 +21,10 @@ module FinerStruct
23
21
  def to_hash
24
22
  @attributes
25
23
  end
26
- end
27
24
 
28
- def self.Immutable(*attribute_names)
29
- Named.build_class(Immutable, attribute_names)
30
- end
25
+ def ==(other)
26
+ other.class == self.class && other.to_hash == to_hash
27
+ end
31
28
 
29
+ end
32
30
  end
@@ -1,9 +1,6 @@
1
- require 'finer_struct/immutable'
2
- require 'finer_struct/named'
3
-
4
1
  module FinerStruct
5
-
6
2
  class Mutable
3
+
7
4
  def initialize(attributes)
8
5
  @attributes = attributes.dup
9
6
  end
@@ -26,6 +23,10 @@ module FinerStruct
26
23
  @attributes.dup
27
24
  end
28
25
 
26
+ def ==(other)
27
+ other.class == self.class && other.to_hash == to_hash
28
+ end
29
+
29
30
  private
30
31
 
31
32
  def is_assigment?(method)
@@ -35,11 +36,7 @@ module FinerStruct
35
36
  def key_for_assignment(method)
36
37
  method.to_s[0..-2].to_sym
37
38
  end
38
- end
39
39
 
40
- def self.Mutable(*attribute_names)
41
- Named.build_class(Mutable, attribute_names)
42
40
  end
43
-
44
41
  end
45
42
 
@@ -1,5 +1,4 @@
1
1
  module FinerStruct
2
-
3
2
  module Named
4
3
 
5
4
  def initialize(attributes = {})
@@ -8,17 +7,22 @@ module FinerStruct
8
7
  raise(ArgumentError, "unknown attributes: #{unknown_attributes.join(', ')}")
9
8
  end
10
9
 
11
- nil_attributes = Hash[attribute_names.zip()]
12
- super(nil_attributes.merge!(attributes))
10
+ super
13
11
  end
14
12
 
15
- def self.build_class(superclass, attribute_names)
13
+ def self.build_class(superclass, attribute_names, &block)
16
14
  Class.new(superclass) do
15
+ include Named
16
+
17
17
  define_method(:attribute_names, -> { attribute_names })
18
18
 
19
- include Named
19
+ attribute_names.each do |name|
20
+ define_method(name) { @attributes[name] }
21
+ end
22
+
23
+ class_eval(&block) if block_given?
20
24
  end
21
25
  end
22
- end
23
26
 
27
+ end
24
28
  end
@@ -0,0 +1,8 @@
1
+ require 'finer_struct/anonymous_immutable'
2
+ require 'finer_struct/named'
3
+
4
+ module FinerStruct
5
+ def self.Immutable(*attribute_names)
6
+ Named.build_class(Immutable, attribute_names)
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ require 'finer_struct/anonymous_mutable'
2
+ require 'finer_struct/named'
3
+
4
+ module FinerStruct
5
+ def self.Mutable(*attribute_names)
6
+ Named.build_class(Mutable, attribute_names) do
7
+ attribute_names.each do |name|
8
+ define_method(:"#{name}=") {|value| @attributes[name] = value }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module FinerStruct
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,12 @@
1
+ require 'finer_struct'
2
+ require 'shared_examples/struct'
3
+ require 'shared_examples/immutable'
4
+
5
+ describe "an anonymous immutable struct" do
6
+ subject { FinerStruct::Immutable.new(a: 1, b: 2) }
7
+ let(:identical) { FinerStruct::Immutable.new(a: 1, b: 2) }
8
+ let(:different) { FinerStruct::Immutable.new(a: 3, b: 4) }
9
+
10
+ it_behaves_like "a struct"
11
+ it_behaves_like "an immutable struct"
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'finer_struct'
2
+ require 'shared_examples/struct'
3
+ require 'shared_examples/mutable'
4
+
5
+ describe "an anonymous mutable struct" do
6
+ subject { FinerStruct::Mutable.new(a: 1, b: 2) }
7
+ let(:identical) { FinerStruct::Mutable.new(a: 1, b: 2) }
8
+ let(:different) { FinerStruct::Mutable.new(a: 3, b: 4) }
9
+
10
+ it_behaves_like "a struct"
11
+ it_behaves_like "a mutable struct"
12
+ end
@@ -0,0 +1,15 @@
1
+ require 'finer_struct'
2
+ require 'shared_examples/struct'
3
+ require 'shared_examples/named'
4
+ require 'shared_examples/immutable'
5
+
6
+ describe "a named immutable struct" do
7
+ let(:klass) { FinerStruct::Immutable(:a, :b) }
8
+ subject { klass.new(a: 1, b: 2) }
9
+ let(:identical) { klass.new(a: 1, b: 2) }
10
+ let(:different) { klass.new(a: 3, b: 4) }
11
+
12
+ it_behaves_like "a struct"
13
+ it_behaves_like "a named struct"
14
+ it_behaves_like "an immutable struct"
15
+ end
@@ -0,0 +1,24 @@
1
+ require 'finer_struct'
2
+ require 'shared_examples/struct'
3
+ require 'shared_examples/named'
4
+ require 'shared_examples/mutable'
5
+
6
+ describe "a named mutable struct" do
7
+ let(:klass) { FinerStruct::Mutable(:a, :b) }
8
+ subject { klass.new(a: 1, b: 2) }
9
+ let(:identical) { klass.new(a: 1, b: 2) }
10
+ let(:different) { klass.new(a: 3, b: 4) }
11
+
12
+ it_behaves_like "a struct"
13
+ it_behaves_like "a named struct"
14
+ it_behaves_like "a mutable struct"
15
+
16
+ it "allows you alias attribute assignments" do
17
+ subclass = Class.new(klass) do
18
+ alias_method :c=, :a=
19
+ end
20
+ struct = subclass.new(a: 1)
21
+ struct.c = 3
22
+ expect(struct.a).to eq(3)
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ shared_examples "an immutable struct" do
2
+ it "complains if you try to write an attribute" do
3
+ expect { subject.a = 3 }.to raise_error(NoMethodError)
4
+ end
5
+
6
+ it "is frozen" do
7
+ expect(subject).to be_frozen
8
+ end
9
+
10
+ it "converts to a frozen hash" do
11
+ expect(subject.to_hash).to be_frozen
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ shared_examples "a mutable struct" do
2
+ it "lets you write attributes" do
3
+ subject.a = 3
4
+ expect(subject.a).to eq(3)
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ shared_examples "a named struct" do
2
+ it "complains if you set attributes that the struct doesn't have" do
3
+ expect { klass.new(c: 3, d: 4) }.to raise_error(ArgumentError, "unknown attributes: c, d")
4
+ end
5
+
6
+ it "returns nil for attributes that aren't explicitly set" do
7
+ expect(klass.new(a: 1).b).to be_nil
8
+ end
9
+
10
+ it "allows you to alias attributes" do
11
+ subclass = Class.new(klass) do
12
+ alias_method :c, :a
13
+ end
14
+ expect(subclass.new(a: 1).c).to eq(1)
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ shared_examples "a struct" do
2
+ it "lets you read attributes" do
3
+ expect(subject.a).to eq(1)
4
+ expect(subject.b).to eq(2)
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 "doesn't respond to attribute names that the struct doesn't have" do
13
+ expect(subject).not_to respond_to(:c)
14
+ end
15
+
16
+ it "responds to non-attribute methods" do
17
+ expect(subject).to respond_to(:class)
18
+ end
19
+
20
+ it "converts to a hash" do
21
+ expect(subject.to_hash).to eq(a: 1 ,b: 2)
22
+ end
23
+
24
+ it "is equal to an identical struct" do
25
+ expect(subject).to eq(identical)
26
+ end
27
+
28
+ it "is not equal to a different struct" do
29
+ expect(subject).to_not eq(different)
30
+ end
31
+
32
+ it "is not equal to an object of a different class" do
33
+ expect(subject).to_not eq("rhubarb")
34
+ end
35
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finer_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-17 00:00:00.000000000 Z
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,13 +66,20 @@ files:
66
66
  - Rakefile
67
67
  - finer_struct.gemspec
68
68
  - lib/finer_struct.rb
69
- - lib/finer_struct/immutable.rb
70
- - lib/finer_struct/mutable.rb
69
+ - lib/finer_struct/anonymous_immutable.rb
70
+ - lib/finer_struct/anonymous_mutable.rb
71
71
  - lib/finer_struct/named.rb
72
+ - lib/finer_struct/named_immutable.rb
73
+ - lib/finer_struct/named_mutable.rb
72
74
  - lib/finer_struct/version.rb
73
- - spec/finer_struct/immutable_spec.rb
74
- - spec/finer_struct/mutable_spec.rb
75
- - spec/finer_struct/shared_examples.rb
75
+ - spec/anonymous_immutable_spec.rb
76
+ - spec/anonymous_mutable_spec.rb
77
+ - spec/named_immutable_spec.rb
78
+ - spec/named_mutable_spec.rb
79
+ - spec/shared_examples/immutable.rb
80
+ - spec/shared_examples/mutable.rb
81
+ - spec/shared_examples/named.rb
82
+ - spec/shared_examples/struct.rb
76
83
  homepage: https://github.com/notahat/finer_struct
77
84
  licenses:
78
85
  - MIT
@@ -98,6 +105,11 @@ signing_key:
98
105
  specification_version: 4
99
106
  summary: A nicer replacement for Ruby's Struct and OpenStruct
100
107
  test_files:
101
- - spec/finer_struct/immutable_spec.rb
102
- - spec/finer_struct/mutable_spec.rb
103
- - spec/finer_struct/shared_examples.rb
108
+ - spec/anonymous_immutable_spec.rb
109
+ - spec/anonymous_mutable_spec.rb
110
+ - spec/named_immutable_spec.rb
111
+ - spec/named_mutable_spec.rb
112
+ - spec/shared_examples/immutable.rb
113
+ - spec/shared_examples/mutable.rb
114
+ - spec/shared_examples/named.rb
115
+ - spec/shared_examples/struct.rb
@@ -1,36 +0,0 @@
1
- require 'finer_struct/immutable'
2
- require 'finer_struct/shared_examples'
3
-
4
- shared_examples "an immutable struct" do
5
- it_behaves_like "a struct"
6
-
7
- it "complains if you try to write an attribute" do
8
- expect { subject.a = 3 }.to raise_error(NoMethodError)
9
- end
10
-
11
- it "is frozen" do
12
- expect(subject).to be_frozen
13
- end
14
-
15
- it "converts to a frozen hash" do
16
- expect(subject.to_hash).to be_frozen
17
- end
18
- end
19
-
20
- module FinerStruct
21
-
22
- describe "an anonymous immutable struct" do
23
- subject { FinerStruct::Immutable.new(a: 1, b: 2) }
24
-
25
- it_behaves_like "an immutable struct"
26
- end
27
-
28
- describe "a named immutable struct" do
29
- let(:klass) { Class.new(FinerStruct::Immutable(:a, :b)) }
30
- subject { klass.new(a: 1, b: 2) }
31
-
32
- it_behaves_like "an immutable struct"
33
- it_behaves_like "a named struct"
34
- end
35
-
36
- end
@@ -1,29 +0,0 @@
1
- require 'finer_struct/mutable'
2
- require 'finer_struct/shared_examples'
3
-
4
- shared_examples "a mutable struct" do
5
- it_behaves_like "a struct"
6
-
7
- it "lets you write attributes" do
8
- subject.a = 3
9
- expect(subject.a).to eq(3)
10
- end
11
- end
12
-
13
- module FinerStruct
14
-
15
- describe "an anonymous mutable struct" do
16
- subject { FinerStruct::Mutable.new(a: 1, b: 2) }
17
-
18
- it_behaves_like "a mutable struct"
19
- end
20
-
21
- describe "a named mutable struct" do
22
- let(:klass) { Class.new(FinerStruct::Mutable(:a, :b)) }
23
- subject { klass.new(a: 1, b: 2) }
24
-
25
- it_behaves_like "a mutable struct"
26
- it_behaves_like "a named struct"
27
- end
28
-
29
- end
@@ -1,33 +0,0 @@
1
- shared_examples "a struct" do
2
- it "lets you read attributes" do
3
- expect(subject.a).to eq(1)
4
- expect(subject.b).to eq(2)
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
23
- end
24
-
25
- shared_examples "a named struct" do
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")
28
- end
29
-
30
- it "returns nil for attributes that aren't explicitly set" do
31
- expect(klass.new(a: 1).b).to be_nil
32
- end
33
- end