finer_struct 0.0.4 → 0.0.5
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/lib/finer_struct/immutable.rb +7 -1
- data/lib/finer_struct/mutable.rb +8 -2
- data/lib/finer_struct/named.rb +12 -9
- data/lib/finer_struct/version.rb +1 -1
- data/spec/finer_struct/immutable_spec.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e04806147484f9a88c442aeae1b5d916ab32ec5e
|
4
|
+
data.tar.gz: f3897c599510a3e49b79d9269b9fc4189b6ca69e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3299f59235ac4f062bd14d422ceb6a3558fd81946d9f53500f88b47e63528630184090e6cd605f76abef079b40263e82989814b74545daec1b0189d3cd00ba80
|
7
|
+
data.tar.gz: dc8f6fa1d98584317d1f4a069043d6c156daa03ae46b78ef321d4988e281833a8bed8ea7cd5d9c67ee70bf168f5dd0a2ed28e661b8e5509296f2ccb7b3f58346
|
@@ -4,7 +4,8 @@ module FinerStruct
|
|
4
4
|
|
5
5
|
class Immutable
|
6
6
|
def initialize(attributes = {})
|
7
|
-
@attributes = attributes.dup
|
7
|
+
@attributes = attributes.dup.freeze
|
8
|
+
freeze
|
8
9
|
end
|
9
10
|
|
10
11
|
def method_missing(method, *arguments)
|
@@ -19,6 +20,11 @@ module FinerStruct
|
|
19
20
|
def self.Immutable(*attribute_names)
|
20
21
|
Named.build_class(attribute_names) do
|
21
22
|
attr_reader(*attribute_names)
|
23
|
+
|
24
|
+
def initialize(*)
|
25
|
+
super
|
26
|
+
freeze
|
27
|
+
end
|
22
28
|
end
|
23
29
|
end
|
24
30
|
|
data/lib/finer_struct/mutable.rb
CHANGED
@@ -3,9 +3,15 @@ require 'finer_struct/named'
|
|
3
3
|
|
4
4
|
module FinerStruct
|
5
5
|
|
6
|
-
class Mutable
|
6
|
+
class Mutable
|
7
|
+
def initialize(attributes)
|
8
|
+
@attributes = attributes.dup
|
9
|
+
end
|
10
|
+
|
7
11
|
def method_missing(method, *arguments)
|
8
|
-
if
|
12
|
+
if @attributes.has_key?(method)
|
13
|
+
@attributes[method]
|
14
|
+
elsif is_assigment?(method) && @attributes.has_key?(key_for_assignment(method))
|
9
15
|
@attributes[key_for_assignment(method)] = arguments[0]
|
10
16
|
else
|
11
17
|
super
|
data/lib/finer_struct/named.rb
CHANGED
@@ -1,20 +1,23 @@
|
|
1
1
|
module FinerStruct
|
2
2
|
|
3
3
|
module Named
|
4
|
+
|
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)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
4
14
|
def self.build_class(attribute_names, &block)
|
5
15
|
Class.new do
|
6
16
|
define_method(:attribute_names, -> { attribute_names })
|
7
17
|
|
8
|
-
|
9
|
-
attributes.each_pair do |name, value|
|
10
|
-
unless attribute_names.include?(name)
|
11
|
-
raise(ArgumentError, "no such attribute: #{name}")
|
12
|
-
end
|
13
|
-
instance_variable_set("@#{name}", value)
|
14
|
-
end
|
15
|
-
end
|
18
|
+
include Named
|
16
19
|
|
17
|
-
|
20
|
+
class_eval(&block)
|
18
21
|
end
|
19
22
|
end
|
20
23
|
end
|
data/lib/finer_struct/version.rb
CHANGED