finer_struct 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41866e67364e4283444fa977e0190237116d1d9f
4
- data.tar.gz: ea6c8c2260274e79516dcb6f3e868fb742db3a56
3
+ metadata.gz: 3491e5caa09c1057794a39b3ba8f0a95b2744229
4
+ data.tar.gz: ea0c2d42894da066df1d2128385be6b8ab2ecf82
5
5
  SHA512:
6
- metadata.gz: 12643d4ab72a6c743d260b84be4672af83c0a9385ecd8d03313a49c186326b0750d4031fe441c461b684757a44ad35877102804852b4b8b3a0a27a403b7b5d36
7
- data.tar.gz: d780dfc6a0bf60336090ade9705e7049dd712555322402d36df72fddd617f6aa536f4f4014e0e041f6489bc77da202426915360d6ea25ef3dcb473a4ea685a19
6
+ metadata.gz: 61eee0dcdc83567e87785bbf9c6a6b513ca1770c4d21189c7e793442afc0bbbd0d22713c9fcbde3b51816c9106fa470a830986482740dc6137cfacb508308835
7
+ data.tar.gz: 08cf4a47fb86f206f0915568830e2678ec7321923ad78814866860824a9211310e30a558ff97f5d54420e22a7757fff620a60c2003e988ed208e9874551d08bd
@@ -1,3 +1,5 @@
1
+ require 'finer_struct/named'
2
+
1
3
  module FinerStruct
2
4
 
3
5
  class Immutable
@@ -15,23 +17,8 @@ module FinerStruct
15
17
  end
16
18
 
17
19
  def self.Immutable(*attribute_names)
18
- Class.new do
19
- @attribute_names = attribute_names
20
-
21
- class << self
22
- attr_reader :attribute_names
23
- end
24
-
20
+ Named.build_class(attribute_names) do
25
21
  attr_reader(*attribute_names)
26
-
27
- def initialize(attributes = {})
28
- attributes.each_pair do |key, value|
29
- unless self.class.attribute_names.include?(key)
30
- raise(ArgumentError, "no such attribute: #{key}")
31
- end
32
- instance_variable_set("@#{key}", value)
33
- end
34
- end
35
22
  end
36
23
  end
37
24
 
@@ -1,4 +1,5 @@
1
1
  require 'finer_struct/immutable'
2
+ require 'finer_struct/named'
2
3
 
3
4
  module FinerStruct
4
5
 
@@ -23,23 +24,8 @@ module FinerStruct
23
24
  end
24
25
 
25
26
  def self.Mutable(*attribute_names)
26
- Class.new do
27
- @attribute_names = attribute_names
28
-
29
- class << self
30
- attr_reader :attribute_names
31
- end
32
-
27
+ Named.build_class(attribute_names) do
33
28
  attr_accessor(*attribute_names)
34
-
35
- def initialize(attributes = {})
36
- attributes.each_pair do |key, value|
37
- unless self.class.attribute_names.include?(key)
38
- raise(ArgumentError, "no such attribute: #{key}")
39
- end
40
- instance_variable_set("@#{key}", value)
41
- end
42
- end
43
29
  end
44
30
  end
45
31
 
@@ -0,0 +1,26 @@
1
+ module FinerStruct
2
+
3
+ module Named
4
+ def self.build_class(attribute_names, &block)
5
+ Class.new do
6
+ @attribute_names = attribute_names
7
+
8
+ class << self
9
+ attr_reader :attribute_names
10
+ end
11
+
12
+ def initialize(attributes = {})
13
+ attributes.each_pair do |key, value|
14
+ unless self.class.attribute_names.include?(key)
15
+ raise(ArgumentError, "no such attribute: #{key}")
16
+ end
17
+ instance_variable_set("@#{key}", value)
18
+ end
19
+ end
20
+
21
+ instance_eval(&block)
22
+ end
23
+ end
24
+ end
25
+
26
+ end
@@ -1,3 +1,3 @@
1
1
  module FinerStruct
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finer_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell
@@ -68,6 +68,7 @@ files:
68
68
  - lib/finer_struct.rb
69
69
  - lib/finer_struct/immutable.rb
70
70
  - lib/finer_struct/mutable.rb
71
+ - lib/finer_struct/named.rb
71
72
  - lib/finer_struct/version.rb
72
73
  - spec/finer_struct/immutable_spec.rb
73
74
  - spec/finer_struct/mutable_spec.rb