finer_struct 0.0.4 → 0.0.5

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: 8fb07bc226c4369e3ea51c098110c69601058731
4
- data.tar.gz: 19913677ea5bffc3174d3730587bc29971525499
3
+ metadata.gz: e04806147484f9a88c442aeae1b5d916ab32ec5e
4
+ data.tar.gz: f3897c599510a3e49b79d9269b9fc4189b6ca69e
5
5
  SHA512:
6
- metadata.gz: f06fa6b4e01a075fe2b2377d7e5df0c5b877a0ea01df1c5d1e223d14effd8670540140f3ad663f0601e9ca04e6638d7353508103b54c3ec29021f4c87661241b
7
- data.tar.gz: f001d24b97f51e1c7f61a6eab3db868c2c151c15155ada1130e4870dea11002f72133a49f83621163912593eee1d8db1a51a993468411850e266e788593949fa
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
 
@@ -3,9 +3,15 @@ require 'finer_struct/named'
3
3
 
4
4
  module FinerStruct
5
5
 
6
- class Mutable < Immutable
6
+ class Mutable
7
+ def initialize(attributes)
8
+ @attributes = attributes.dup
9
+ end
10
+
7
11
  def method_missing(method, *arguments)
8
- if is_assigment?(method) && @attributes.has_key?(key_for_assignment(method))
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
@@ -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
- def initialize(attributes = {})
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
- instance_eval(&block)
20
+ class_eval(&block)
18
21
  end
19
22
  end
20
23
  end
@@ -1,3 +1,3 @@
1
1
  module FinerStruct
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -7,6 +7,10 @@ shared_examples "an immutable struct" do
7
7
  it "complains if you try to write an attribute" do
8
8
  expect { subject.a = 3 }.to raise_error(NoMethodError)
9
9
  end
10
+
11
+ it "is frozen" do
12
+ subject.should be_frozen
13
+ end
10
14
  end
11
15
 
12
16
  module FinerStruct
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.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell