value-object 0.1.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/value_object.rb +70 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13bfccbeb7b30bf8d13f87c0597c445f939f02c3
4
+ data.tar.gz: 366283b9ea1557919e0bbbec8ac983c609a367d9
5
+ SHA512:
6
+ metadata.gz: 5d956e744a6b4fa43b9a134d9e6fcca9d9450be9c2c7f5005ce8b99b3c72c9468e5ba9724f4febd792a7f16551b1fbcd70814e2b21b904c5f1d301ef9637d50c
7
+ data.tar.gz: db4d7d006abf99c3e0961a6b32df5316fe0db82721c33739de9d69ced6875af88453038646c66f6c4cd94e14ce5d33b7fdaf09302aa57af4a37d8434d499ccd9
@@ -0,0 +1,70 @@
1
+ module ValueObject
2
+ def fields(*names)
3
+ raise NotDeclaredFields.new() if names.empty?()
4
+
5
+ attr_reader(*names)
6
+
7
+ define_method(:check_invariants) do
8
+ end
9
+
10
+ define_method(:initialize) do |*values|
11
+ names.zip(values) do |name, value|
12
+ instance_variable_set(:"@#{name}", value)
13
+ end
14
+ check_invariants()
15
+ end
16
+
17
+ define_method(:values) do
18
+ names.map { |field| send(field) }
19
+ end
20
+ protected(:values)
21
+
22
+ define_method(:eql?) do |other|
23
+ self.class == other.class && values == other.values
24
+ end
25
+
26
+ define_method(:==) do |other|
27
+ eql?(other)
28
+ end
29
+
30
+ define_method(:hash) do
31
+ self.class.hash ^ values.hash
32
+ end
33
+ end
34
+
35
+ def invariants(*predicate_symbols)
36
+ define_method(:check_invariants) do
37
+ predicate_symbols.each do |predicate_symbol|
38
+ valid = invariant_holds?(predicate_symbol)
39
+ raise ViolatedInvariant.new(predicate_symbol, self.values) unless valid
40
+ end
41
+ end
42
+
43
+ define_method(:invariant_holds?) do |predicate_symbol|
44
+ begin
45
+ valid = send(predicate_symbol)
46
+ rescue
47
+ raise NotImplementedInvariant.new(predicate_symbol)
48
+ end
49
+ end
50
+ private(:invariant_holds?)
51
+ end
52
+
53
+ class NotImplementedInvariant < Exception
54
+ def initialize(name)
55
+ super "Invariant #{name} needs to be implemented"
56
+ end
57
+ end
58
+
59
+ class ViolatedInvariant < Exception
60
+ def initialize(name, wrong_values)
61
+ super "Fields values " + wrong_values.to_s + " violate invariant: #{name}"
62
+ end
63
+ end
64
+
65
+ class NotDeclaredFields < Exception
66
+ def initialize()
67
+ super "At least one field must be declared"
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: value-object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - No Flop Squad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: noflopsquad@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/value_object.rb
20
+ homepage: http://rubygems.org/gems/value-object
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A simple module to provide value objects semantics to a class
44
+ test_files: []