value_struct 0.7.0 → 0.8.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b84f12dd920408c7536ca6548bcdc2c74ac3d3cf
4
+ data.tar.gz: fe3f7551490c4263c599e985df0ba799cc8a5027
5
+ SHA512:
6
+ metadata.gz: e05f7c38fd1440a8bcad24e6be6755f0a05dcf1ef6a15ec5a7a4b252101d31abd7767c02ef8264cd24f0046e74e506a2a1d2d0217b2f8df3c748d76c816a47c0
7
+ data.tar.gz: a0be6061cc6ee344aedad39efac95381c3609dd6cd43466910e9263082cd7bc1fb454c13a3bb025d2a434cdaa03550e9a6ec7e78b3eb2361b788916540c1fd70
@@ -0,0 +1,12 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ insert_final_newline = true
8
+ trim_trailing_whitespace = true
9
+
10
+ [*.{md,rdoc,txt}]
11
+ indent_size = 4
12
+
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - ruby-head
5
+ - 2.2.0
6
+ - 2.1.5
7
+ - 2.0.0
8
+ - jruby-head
@@ -1,3 +1,7 @@
1
+ ### 0.8.0
2
+ * Drop support for Ruby 1
3
+ * Remove #to_h mixin
4
+
1
5
  ### 0.7.0
2
6
  * Improve mixin system
3
7
  * More logical class/model/inheritance logic
@@ -7,6 +11,5 @@
7
11
  * Fix specs
8
12
  * Fix #dup
9
13
 
10
- ### 0.5.0 / 2012-11-03
11
-
12
- * Initial puplic release:
14
+ ### 0.5.0
15
+ * Initial puplic release
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Jan Lelis
1
+ Copyright (c) 2012-2015 Jan Lelis
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,53 +1,93 @@
1
- # Value Struct
1
+ # Value Struct [![[travis]](https://travis-ci.org/janlelis/value_struct.png)](https://travis-ci.org/janlelis/value_struct)
2
2
 
3
- A value struct is a subclass of normal [Ruby struct](http://blog.grayproductions.net/articles/all_about_struct) that behaves almost the same. However, it has a major difference:
3
+ A value struct is a subclass of the normal [Ruby struct](http://blog.grayproductions.net/articles/all_about_struct) that behaves almost the same. However, it has a major difference:
4
4
 
5
5
  __Value structs are immutable, i.e. they don't have setters (although, not recursively*)__
6
6
 
7
7
  Additionally, this gem provides the following optional mixins to make life easier when using immutable structs:
8
8
 
9
- * __:dup_with_changes__ #dup takes a optional hash for setting new values in the duplicate
10
- * __:to_h__ #to_h for converting into a valuestruct into a hash (if Ruby version below < 2.0)
11
-
9
+ * __:dup_with_changes__ Extends `#dup` to take a optional hash for setting new values in the duplicate
12
10
  * __:strict_arguments__ Value structs need to be initialized with the exact amount of arguments
13
11
  * __:freeze__ Automatically freezes new instances
14
- * __:no_clone__ #clone does not clone, but return the same object
15
-
16
- By default, only :dup_with_changes and :to_h get included.
12
+ * __:no_clone__ Alters `#clone` to return the same object
17
13
 
18
- ## Why?
14
+ By default, only __:dup_with_changes__ will be included.
19
15
 
20
- Sometimes you want to eliminate state. See [this blog article] for more information.
16
+ Without mixins, ValueStructs are almost as fast as normal structs. Some mixins add noticable overhead, e.g. strict_arguments
21
17
 
22
- ## Performance
18
+ ## Why?
23
19
 
24
- Without mixins, ValueStructs are as fast as normal structs. Some (optional) mixins add noticable overhead, e.g. strict_arguments
20
+ See [this blog article](http://ruby.janlelis.de/65-value_struct-read-only-structs-in-ruby) for more information.
25
21
 
26
- ## Example
22
+ ## Example 1
27
23
 
28
24
  require 'value_struct'
29
25
 
30
- Point = ValueStruct.new(:x, :y) do # methods defined in the block will be available in your new value struct class
31
- # ...
32
- end
26
+ SimplePoint = ValueStruct.new(:x, :y)
27
+
33
28
 
34
- Please refer to the [documentation of Ruby's struct] for more details on usage.
29
+ Please refer to the [documentation of Ruby's struct](http://ruby-doc.org/core-2.2.0/Struct.html) for more details on general struct usage.
35
30
 
36
31
  ## How to use structs with mixins
37
32
 
38
33
  Point = ValueStruct.new_with_mixins :x, :y, [
39
- ValueStruct::ToH,
40
- ValueStruct::Freeze,
41
- ValueStruct::DupWithChanges,
42
- ValueStruct::StrictArguments,
34
+ :freeze,
35
+ :dup_with_changes,
36
+ :strict_arguments,
43
37
  ]
44
38
 
45
39
  p = Point.new(1,2)
46
- p.to_h #=> { :x => 1, :y => 2 }
47
40
  p.frozen? #=> true
48
41
  p.dup(x: 0) #=> #<ValueStruct Point x=0, y=2>
49
42
  Point.new(1) # ArgumentError
50
43
 
44
+ Alternatively, you can put custom modules in the mixin array.
45
+
46
+ ## Example 2
47
+
48
+ require 'value_struct'
49
+
50
+ Point = ValueStruct.new_with_mixins(
51
+ :x,
52
+ :y,
53
+ [:dup_with_changes, :freeze, :no_clone],
54
+ ) do
55
+
56
+ def initialize(x,y)
57
+ raise ArgumentError, 'points must be initilized with two numerics' unless x.is_a?(Numeric) && y.is_a?(Numeric)
58
+ super(x,y)
59
+ end
60
+
61
+ def abs
62
+ ( x**2 + y**2 ) ** 0.5
63
+ end
64
+
65
+ def +(o)
66
+ dup(x: x + o.x, y: y + o.y)
67
+ end
68
+
69
+ def -(o)
70
+ dup(x: x - o.x, y: y - o.y)
71
+ end
72
+
73
+ def +@
74
+ self
75
+ end
76
+
77
+ def -@
78
+ dup(x: -x, y: -o.y)
79
+ end
80
+
81
+ def to_c
82
+ Complex(x,y)
83
+ end
84
+
85
+ def to_s
86
+ "(#{x},#{y})"
87
+ end
88
+ alias inspect to_s
89
+ end
90
+
51
91
  ## *
52
92
 
53
93
  Because of the nature of Ruby, most things are not really immutable. So if you have an attribute `:by` and initialize it with an array, you cannot change the value struct anymore, but still the array:
@@ -69,4 +109,6 @@ Because of the nature of Ruby, most things are not really immutable. So if you h
69
109
  * Theo Hultberg: [ImmutableStruct](https://github.com/iconara/immutable_struct)
70
110
  * Ruby Rogues
71
111
 
72
- ## J-_-L
112
+ ## J-_-L
113
+
114
+ Copyright © 2012-2015 Jan Lelis, janlelis.com, released under the MIT license.
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'rubygems'
4
-
5
3
  begin
6
4
  require 'bundler'
7
5
  rescue LoadError => e
@@ -20,15 +18,28 @@ end
20
18
 
21
19
  require 'rake'
22
20
 
21
+ # - - -
22
+
23
23
  require 'rubygems/tasks'
24
24
  Gem::Tasks.new
25
25
 
26
+ # - - -
27
+
26
28
  require 'rspec/core/rake_task'
27
29
  RSpec::Core::RakeTask.new
28
30
 
29
31
  task :test => :spec
30
32
  task :default => :spec
31
33
 
34
+ # - - -
35
+
32
36
  require 'yard'
33
- YARD::Rake::YardocTask.new
37
+ YARD::Rake::YardocTask.new
34
38
  task :doc => :yard
39
+
40
+ # - - -
41
+
42
+ desc 'compare ValueStruct with similar implementations'
43
+ task :benchmark do
44
+ ruby 'spec/benchmark.rb'
45
+ end
@@ -5,14 +5,14 @@ authors: Jan Lelis
5
5
  email: mail@janlelis.de
6
6
  homepage: https://github.com/janlelis/value_struct
7
7
 
8
+ required_ruby_version: ~> 2.0
9
+
8
10
  development_dependencies:
9
11
  bundler: ~> 1.0
10
12
  rake: ~> 0.8
11
13
  rspec: ~> 2.4
12
14
  rubygems-tasks: ~> 0.2
13
15
  yard: ~> 0.8
14
- kramdown: ~> 0.14
15
- debugger: ~> 1.2.1
16
- values: ~> 1.2.1
17
- immutable_struct: ~> 1.0.2
18
- hamster: ~> 0.4.3
16
+ kramdown: ~> 0.14
17
+ values: ~> 1.5.0
18
+ immutable_struct: ~> 1.1.1
@@ -1,7 +1,6 @@
1
1
  require_relative 'value_struct/version'
2
2
 
3
3
  require_relative 'value_struct/immutable'
4
- require_relative 'value_struct/to_h'
5
4
  require_relative 'value_struct/dup_with_changes'
6
5
  require_relative 'value_struct/strict_arguments'
7
6
  require_relative 'value_struct/no_clone'
@@ -29,7 +28,6 @@ class ValueStruct < Struct
29
28
 
30
29
  def new(*args, &block)
31
30
  mixins = [ValueStruct::DupWithChanges]
32
- mixins.unshift(ValueStruct::ToH) if RUBY_VERSION < "2.0"
33
31
  new_with_mixins(*args, mixins, &block)
34
32
  end
35
33
  end
@@ -37,4 +35,4 @@ class ValueStruct < Struct
37
35
  def inspect
38
36
  super.to_s.sub('struct', 'ValueStruct')
39
37
  end
40
- end
38
+ end
@@ -1,3 +1,3 @@
1
1
  class ValueStruct < Struct
2
- VERSION = '0.7.0'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -3,7 +3,7 @@ require 'benchmark'
3
3
  require 'immutable_struct'
4
4
  require 'values'
5
5
 
6
- COUNT = 200_000
6
+ COUNT = 1_000_000
7
7
 
8
8
  def benchmark_for(struct_class)
9
9
  puts "%20s: %s" % [
@@ -13,7 +13,9 @@ describe ValueStruct do
13
13
  describe '.new' do
14
14
  it 'calls .new_with_mixins and adds default mixins' do
15
15
  subject.should_receive(:new_with_mixins).with(
16
- :x, :y, [ValueStruct::ToH, ValueStruct::DupWithChanges]
16
+ :x, :y,[
17
+ ValueStruct::DupWithChanges,
18
+ ].compact
17
19
  )
18
20
  subject.new(:x, :y)
19
21
  end
@@ -21,12 +23,6 @@ describe ValueStruct do
21
23
  it 'adds the ValueStruct::DupWithChanges mixin' do
22
24
  subject.new(:x, :y).new(1,2).should be_a ValueStruct::DupWithChanges
23
25
  end
24
-
25
- it 'adds ValueStruct::ToH if ruby version is below 2.0' do
26
- if RUBY_VERSION < "2.0"
27
- subject.new(:x, :y).new(1,2).should be_a ValueStruct::ToH
28
- end
29
- end
30
26
  end
31
27
 
32
28
  describe '.new_with_mixins' do
@@ -52,7 +48,6 @@ describe ValueStruct do
52
48
  it 'includes all mixins that are given in the last paramater array' do
53
49
  mixins = [
54
50
  ValueStruct::DupWithChanges,
55
- ValueStruct::ToH,
56
51
  ValueStruct::StrictArguments,
57
52
  ValueStruct::Freeze,
58
53
  ]
metadata CHANGED
@@ -1,192 +1,145 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: value_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
5
- prerelease:
4
+ version: 0.8.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jan Lelis
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-04 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0.8'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0.8'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '2.4'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '2.4'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rubygems-tasks
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ~>
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0.2'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ~>
66
+ - - "~>"
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0.2'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: yard
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ~>
73
+ - - "~>"
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0.8'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ~>
80
+ - - "~>"
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0.8'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: kramdown
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ~>
87
+ - - "~>"
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0.14'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ~>
94
+ - - "~>"
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0.14'
110
- - !ruby/object:Gem::Dependency
111
- name: debugger
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 1.2.1
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: 1.2.1
126
97
  - !ruby/object:Gem::Dependency
127
98
  name: values
128
99
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
100
  requirements:
131
- - - ~>
101
+ - - "~>"
132
102
  - !ruby/object:Gem::Version
133
- version: 1.2.1
103
+ version: 1.5.0
134
104
  type: :development
135
105
  prerelease: false
136
106
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
107
  requirements:
139
- - - ~>
108
+ - - "~>"
140
109
  - !ruby/object:Gem::Version
141
- version: 1.2.1
110
+ version: 1.5.0
142
111
  - !ruby/object:Gem::Dependency
143
112
  name: immutable_struct
144
113
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
114
  requirements:
147
- - - ~>
115
+ - - "~>"
148
116
  - !ruby/object:Gem::Version
149
- version: 1.0.2
117
+ version: 1.1.1
150
118
  type: :development
151
119
  prerelease: false
152
120
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
121
  requirements:
155
- - - ~>
122
+ - - "~>"
156
123
  - !ruby/object:Gem::Version
157
- version: 1.0.2
158
- - !ruby/object:Gem::Dependency
159
- name: hamster
160
- requirement: !ruby/object:Gem::Requirement
161
- none: false
162
- requirements:
163
- - - ~>
164
- - !ruby/object:Gem::Version
165
- version: 0.4.3
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
171
- - - ~>
172
- - !ruby/object:Gem::Version
173
- version: 0.4.3
124
+ version: 1.1.1
174
125
  description: ''
175
126
  email: mail@janlelis.de
176
127
  executables: []
177
128
  extensions: []
178
129
  extra_rdoc_files:
179
130
  - ChangeLog.md
180
- - LICENSE.txt
131
+ - MIT-LICENSE.txt
181
132
  - README.md
182
133
  files:
183
- - .document
184
- - .gitignore
185
- - .rspec
186
- - .yardopts
134
+ - ".document"
135
+ - ".editorconfig"
136
+ - ".gitignore"
137
+ - ".rspec"
138
+ - ".travis.yml"
139
+ - ".yardopts"
187
140
  - ChangeLog.md
188
141
  - Gemfile
189
- - LICENSE.txt
142
+ - MIT-LICENSE.txt
190
143
  - README.md
191
144
  - Rakefile
192
145
  - gemspec.yml
@@ -196,7 +149,6 @@ files:
196
149
  - lib/value_struct/immutable.rb
197
150
  - lib/value_struct/no_clone.rb
198
151
  - lib/value_struct/strict_arguments.rb
199
- - lib/value_struct/to_h.rb
200
152
  - lib/value_struct/version.rb
201
153
  - spec/benchmark.rb
202
154
  - spec/dup_with_changes_spec.rb
@@ -204,32 +156,30 @@ files:
204
156
  - spec/no_clone_spec.rb
205
157
  - spec/spec_helper.rb
206
158
  - spec/strict_arguments_spec.rb
207
- - spec/to_h_spec.rb
208
159
  - spec/value_struct_spec.rb
209
160
  - value_struct.gemspec
210
161
  homepage: https://github.com/janlelis/value_struct
211
162
  licenses:
212
163
  - MIT
164
+ metadata: {}
213
165
  post_install_message:
214
166
  rdoc_options: []
215
167
  require_paths:
216
168
  - lib
217
169
  required_ruby_version: !ruby/object:Gem::Requirement
218
- none: false
219
170
  requirements:
220
- - - ! '>='
171
+ - - "~>"
221
172
  - !ruby/object:Gem::Version
222
- version: '0'
173
+ version: '2.0'
223
174
  required_rubygems_version: !ruby/object:Gem::Requirement
224
- none: false
225
175
  requirements:
226
- - - ! '>='
176
+ - - ">="
227
177
  - !ruby/object:Gem::Version
228
178
  version: '0'
229
179
  requirements: []
230
180
  rubyforge_project:
231
- rubygems_version: 1.8.24
181
+ rubygems_version: 2.4.5
232
182
  signing_key:
233
- specification_version: 3
183
+ specification_version: 4
234
184
  summary: Struct-like value objects
235
185
  test_files: []
@@ -1,5 +0,0 @@
1
- module ValueStruct::ToH
2
- def to_h
3
- Hash[members.zip(values)]
4
- end
5
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ValueStruct::ToH do
4
- subject do
5
- ValueStruct.new_with_mixins(:x, :y, [ValueStruct::ToH]).new(1,2)
6
- end
7
-
8
- its(:to_h) do
9
- { x: 1, y: 2 }
10
- end
11
- end