finer_struct 0.0.5 → 0.0.6

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: e04806147484f9a88c442aeae1b5d916ab32ec5e
4
- data.tar.gz: f3897c599510a3e49b79d9269b9fc4189b6ca69e
3
+ metadata.gz: 80dfaa74d9276f522ca1cf4e39175f42b8e679bc
4
+ data.tar.gz: 78595752aa946668b311fced58235745a0d0025e
5
5
  SHA512:
6
- metadata.gz: 3299f59235ac4f062bd14d422ceb6a3558fd81946d9f53500f88b47e63528630184090e6cd605f76abef079b40263e82989814b74545daec1b0189d3cd00ba80
7
- data.tar.gz: dc8f6fa1d98584317d1f4a069043d6c156daa03ae46b78ef321d4988e281833a8bed8ea7cd5d9c67ee70bf168f5dd0a2ed28e661b8e5509296f2ccb7b3f58346
6
+ metadata.gz: ef9af062326f3afb4bd18b9fd20eaea8472ef7062518eaf010a8616b0e9f77742c4bcc1f1a68e36e6cdd63940642371e403cac86f9b8aa79d950b3f3b2c861c4
7
+ data.tar.gz: 1d8d5dbb7c2ffbb279e92dfe829d7c598ad3d878b259bbd8514ac8b973ed30095287c0e820fae5bc391f05855714461d14c9c27b270f42ace0301ad72212fb46
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
- #source 'https://rubygems.org'
2
- source 'http://rubygems.railscamp.org'
1
+ source 'https://rubygems.org'
3
2
 
4
3
  # Specify your gem's dependencies in finer_struct.gemspec
5
4
  gemspec
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 the arguments to `Struct.new` have to be ordered?
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
- gem 'finer_struct'
19
+ ```ruby
20
+ gem 'finer_struct'
21
+ ```
20
22
 
21
23
  And then execute:
22
24
 
23
- $ bundle
25
+ ```bash
26
+ $ bundle
27
+ ```
24
28
 
25
29
  Or install it yourself as:
26
30
 
27
- $ gem install finer_struct
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
- struct = FinerStruct::Immutable.new(a: 1, b: 2)
43
+ ```ruby
44
+ struct = FinerStruct::Immutable.new(a: 1, b: 2)
38
45
 
39
- struct.a # => 1
40
- struct.b # => 2
41
- struct.a = 3 # Exception!
46
+ struct.a # => 1
47
+ struct.b # => 2
48
+ struct.a = 3 # Exception!
49
+ ```
42
50
 
43
51
  #### Named immutable structs
44
52
 
45
- class MyStruct < FinerStruct::Immutable(:a, :b); end
46
- struct = MyStruct.new(a: 1, b: 2)
53
+ ```ruby
54
+ class MyStruct < FinerStruct::Immutable(:a, :b); end
55
+ struct = MyStruct.new(a: 1, b: 2)
47
56
 
48
- MyStruct.new(a: 1, b: 2, c: 3) # Exception!
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
- struct = FinerStruct::Mutable.new(a: 1, b: 2)
66
+ ```ruby
67
+ struct = FinerStruct::Mutable.new(a: 1, b: 2)
57
68
 
58
- struct.a = 3
59
- struct.a # => 3
69
+ struct.a = 3
70
+ struct.a # => 3
71
+ ```
60
72
 
61
73
  #### Named mutable structs
62
74
 
63
- class MyStruct < FinerStruct::Mutable(:a, :b); end
64
- struct = MyStruct.new(a: 1, b: 2)
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
- def self.Immutable(*attribute_names)
21
- Named.build_class(attribute_names) do
22
- attr_reader(*attribute_names)
19
+ def respond_to?(method)
20
+ @attributes.has_key?(method) || super
21
+ end
23
22
 
24
- def initialize(*)
25
- super
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
@@ -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) do
34
- attr_accessor(*attribute_names)
35
- end
41
+ Named.build_class(Mutable, attribute_names)
36
42
  end
37
43
 
38
44
  end
@@ -3,21 +3,19 @@ module FinerStruct
3
3
  module Named
4
4
 
5
5
  def initialize(attributes = {})
6
- attributes.each_pair do |name, value|
7
- unless attribute_names.include?(name)
8
- raise(ArgumentError, "no such attribute: #{name}")
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(attribute_names, &block)
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
@@ -1,3 +1,3 @@
1
1
  module FinerStruct
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -9,7 +9,11 @@ shared_examples "an immutable struct" do
9
9
  end
10
10
 
11
11
  it "is frozen" do
12
- subject.should be_frozen
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
13
17
  end
14
18
  end
15
19
 
@@ -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 an attribute that the struct doesn't have" do
10
- expect { klass.new(c: 3) }.to raise_error(ArgumentError, "no such attribute: c")
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.5
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: 2013-06-24 00:00:00.000000000 Z
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.0.3
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