hamsterdam 1.0.2 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ Hamsterdam v1.0.5
2
+ * Setters return the same instance of the object if the value being set is unchanged
3
+ * Optimized creation of a new struct caused by a setter modification by skipping validation
4
+ * (v1.0.3 and 1.0.4 skipped because of github repo tagging conflict)
5
+
1
6
  Hamsterdam v1.0.2
2
7
  * Field name defs, constructor keys and merge keys are now stringified internally to prevent string/symbol mismatching
3
8
  * Added syntax helper for this kind of thing: class Castle < Hamsterdam::Struct(:gate, :walls)
data/lib/hamsterdam.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'hamster'
2
2
 
3
3
  module Hamsterdam
4
- VERSION = "1.0.2"
5
4
 
6
5
  def self.Struct(*field_names)
7
6
  Hamsterdam::Struct.define(*field_names)
@@ -13,11 +12,15 @@ module Hamsterdam
13
12
  field_names = field_names.map &:to_sym
14
13
  field_names.each do |fname|
15
14
  define_method fname do
16
- return @data[fname]
15
+ @data[fname]
17
16
  end
18
17
 
19
18
  define_method "set_#{fname}" do |value|
20
- self.class.new(@data.put(fname, value))
19
+ if @data[fname] == value
20
+ self
21
+ else
22
+ self.class.new(@data.put(fname, value), false)
23
+ end
21
24
  end
22
25
  end
23
26
 
@@ -44,9 +47,13 @@ module Hamsterdam
44
47
  struct_class
45
48
  end
46
49
 
47
- def initialize(values=Hamster.hash)
48
- @data = flesh_out(ensure_hamster_hash(values))
49
- validate_keys(@data)
50
+ def initialize(values=Hamster.hash, validate=true)
51
+ if validate
52
+ @data = flesh_out(ensure_hamster_hash(values))
53
+ validate_keys(@data)
54
+ else
55
+ @data = values
56
+ end
50
57
  end
51
58
 
52
59
  def merge(values)
@@ -74,7 +81,7 @@ module Hamsterdam
74
81
  end
75
82
 
76
83
  def to_s
77
- name = self.class.name.split(/::/).last
84
+ name = self.class.name ? self.class.name.split(/::/).last : self.class.to_s
78
85
  data = to_hamster_hash
79
86
  fields = self.class.field_names_list.map { |fname| "#{fname}: #{data[fname].inspect}" }
80
87
  "<#{([name]+fields).join(" ")}>"
@@ -0,0 +1,3 @@
1
+ module Hamsterdam
2
+ VERSION = "1.0.5"
3
+ end
@@ -121,6 +121,11 @@ describe "Hamsterdam structures" do
121
121
  struct.bottom.should == 1
122
122
  end
123
123
 
124
+ it "returns the same struct if a set does not change the value" do
125
+ struct = struct_class.new(top: 25, bottom: 2)
126
+ struct.set_top(25).set_bottom(2).should equal(struct)
127
+ end
128
+
124
129
  it "provides a merge function" do
125
130
  struct = struct_class.new(top: 10, bottom: 1)
126
131
 
@@ -146,6 +151,7 @@ describe "Hamsterdam structures" do
146
151
  struct.top.should == 10
147
152
  struct.bottom.should == 1
148
153
  end
154
+
149
155
  end
150
156
 
151
157
  describe "inspect and to_s" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamsterdam
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-18 00:00:00.000000000 Z
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hamster
@@ -98,14 +98,10 @@ executables: []
98
98
  extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
- - CHANGELOG
102
- - Gemfile
103
101
  - README.md
104
- - Rakefile
105
- - TODO.txt
106
- - hamsterdam.gemspec
102
+ - CHANGELOG
103
+ - lib/hamsterdam/version.rb
107
104
  - lib/hamsterdam.rb
108
- - rake_tasks/rspec.rake
109
105
  - spec/hamsterdam_spec.rb
110
106
  - spec/spec_helper.rb
111
107
  - spec/support/hamsterdam_helpers.rb
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Deps are in hamsterdam.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
3
- HERE = File.dirname(__FILE__)
4
-
5
- Dir[File.expand_path(HERE) + "/rake_tasks/*.rake"].each do |rake_file|
6
- import rake_file
7
- end
8
-
9
- # desc 'Default: run specs and cucumber features'
10
- # task :default => [ "spec", "cuc:features" ]
11
-
12
- task :default => [ "spec" ]
data/TODO.txt DELETED
@@ -1,4 +0,0 @@
1
- - Add commas in to_s
2
- - .set_defaults({}) to provide default values for constructors
3
- - Want to use fun "Status < Hamsterdam::Struct(:one, :two)" but something is wrong with inheritance of field_names that blows up the flesh_out func.
4
- - FIXED: Symbol/string insensitivity (use symbols internally and when listing field names, but accept :foo and 'foo' when constructing and merging
data/hamsterdam.gemspec DELETED
@@ -1,24 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/lib")
3
- require File.expand_path('../lib/hamsterdam', __FILE__)
4
-
5
- Gem::Specification.new do |gem|
6
- gem.authors = ["David Crosby"]
7
- gem.email = ["david.crosby@atomicobject.com"]
8
- gem.description = %q{Immutable Struct-like record structures based on Hamster.}
9
- gem.summary = %q{Immutable Struct-like record structures based on Hamster.}
10
- gem.homepage = "https://github.com/atomicobject/hamsterdam"
11
-
12
- gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
- gem.files = `git ls-files`.split("\n") - [".gitignore", ".rspec", ".rvmrc", "NOTES.txt", "TODO"]
14
- gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
- gem.name = "hamsterdam"
16
- gem.require_paths = ["lib"]
17
- gem.version = Hamsterdam::VERSION
18
-
19
- gem.add_dependency "hamster"
20
- gem.add_development_dependency "rake"
21
- gem.add_development_dependency "rspec"
22
- gem.add_development_dependency "simplecov"
23
- gem.add_development_dependency "pry"
24
- end
@@ -1,25 +0,0 @@
1
- begin
2
- require 'rspec/core/rake_task'
3
- rescue Exception => e
4
- "RSpec is not available. 'spec' task will not be defined."
5
- end
6
-
7
- begin # protect from missing rspec
8
- desc "Run specs"
9
- RSpec::Core::RakeTask.new do |t|
10
- if ENV["file"]
11
- t.pattern = ENV["file"]
12
- end
13
- t.rspec_opts = "--color --format documentation" # --tty
14
- end
15
-
16
- desc "Run individual spec"
17
- task "spec:just" do
18
- RSpec::Core::RakeTask.new("_tmp_rspec") do |t|
19
- t.pattern = ENV["file"] || raise("Please supply 'file' argument")
20
- t.rspec_opts = "--color"
21
- end
22
- Rake::Task["_tmp_rspec"].invoke
23
- end
24
- rescue Exception => e
25
- end