strict_struct 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b6bb2125a155d3e7bdaeeec1911f866b2794a06
4
- data.tar.gz: b3a9be0fb018a7eb7a44bb51eeee5169a64a29cf
3
+ metadata.gz: 52edb2316281bbc0e51cf385831f5e3efd962f88
4
+ data.tar.gz: f98b762c30921c057530eaa6772af5480832b87d
5
5
  SHA512:
6
- metadata.gz: 29092b1f8a93d319377f94bf71c900e28c52e2f30fd1da03a0160c8c90e40656c3432f5b09eeedfea1cc91a3486bb10de2e34064ad78e2747de01e755fef5c02
7
- data.tar.gz: 1f98e60cec646a207424cd0efda65d2e2cbad1031247ff71c477e346839adbbfa1c47b6096f1bacd74267ffb85377a69c7e0d9a60ac3d1926036333992899ccd
6
+ metadata.gz: a95119689d525fc07f9dc5500f3795056abd0094ee23fe29900d228e2948488fb4126ce309231122e73e284c4397fef8075321f79fdf6717d8f037e20463bd88
7
+ data.tar.gz: 90aeb68616e42f04f19f248ac8e22d23bf7945de4d1994b50871e1a559390194d4c2f9a18bcdda63280696a7d4723b51420512052d4a93041fe40c67da174390
data/.travis.yml CHANGED
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
+ gemfile:
3
+ - Gemfile.travis
2
4
  rvm:
3
5
  - 1.9.3
4
6
  - 2.0.0
@@ -6,3 +8,4 @@ rvm:
6
8
  - ruby-head
7
9
  - rbx
8
10
  - jruby-head
11
+ cache: bundler
data/Gemfile.travis ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ platforms :rbx do
4
+ gem 'rubysl', '~> 2.0'
5
+ end
6
+
7
+ eval_gemfile File.expand_path('../Gemfile', __FILE__)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # StrictStruct [![Build Status](https://travis-ci.org/markijbema/strict_struct.png)](https://travis-ci.org/markijbema/strict_struct) [![Code Climate](https://codeclimate.com/github/markijbema/strict_struct.png)](https://codeclimate.com/github/markijbema/strict_struct)
1
+ # StrictStruct [![Build Status](https://travis-ci.org/markijbema/strict_struct.png)](https://travis-ci.org/markijbema/strict_struct) [![Code Climate](https://codeclimate.com/github/markijbema/strict_struct.png)](https://codeclimate.com/github/markijbema/strict_struct) [![Gem Version](https://badge.fury.io/rb/strict_struct.png)](http://badge.fury.io/rb/strict_struct) [![Gem Version](https://badge.fury.io/rb/strict_struct.png)](http://badge.fury.io/rb/strict_struct)
2
2
 
3
3
  This gems aims to provide a modern version of Struct.
4
4
  While Struct is a nice easy way to create a light-weight
@@ -23,13 +23,15 @@ Rectangle = StrictStruct.new(:x, :y) do
23
23
  end
24
24
  ```
25
25
 
26
- This would conceptually create the following object:
26
+ This would conceptually create something like the following object:
27
27
 
28
28
  ```ruby
29
29
  class Rectange
30
30
  attr_reader :x, :y
31
31
 
32
32
  def initialize(x:, y:)
33
+ @x = x
34
+ @y = y
33
35
  end
34
36
 
35
37
  def area
@@ -38,6 +40,9 @@ class Rectange
38
40
  end
39
41
  ```
40
42
 
43
+ Since this is meant to create immutable objects, the values aren't actually assigned to instance variables but safed internally in a hash.
44
+
45
+
41
46
  ## Installation
42
47
 
43
48
  Add this line to your application's Gemfile:
@@ -1,3 +1,3 @@
1
1
  module StrictStruct
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/strict_struct.rb CHANGED
@@ -24,16 +24,18 @@ module StrictStruct
24
24
  @@attributes = attributes
25
25
 
26
26
  define_method :initialize do |hash={}|
27
- @hash = hash
28
- Helper.validate_arguments(@hash.keys, @@attributes)
27
+ Helper.validate_arguments(hash.keys, @@attributes)
28
+ @_strict_struct_hash = Hash[hash.to_a].freeze
29
29
  end
30
30
 
31
31
  attributes.each do |attribute|
32
- define_method(attribute) {|| @hash[attribute] }
32
+ define_method(attribute) do
33
+ @_strict_struct_hash[attribute]
34
+ end
33
35
  end
34
36
 
35
37
  def to_h
36
- @hash
38
+ Hash[@_strict_struct_hash.to_a]
37
39
  end
38
40
 
39
41
  def == (other)
@@ -98,10 +98,29 @@ describe 'StrictStruct' do
98
98
 
99
99
  expect do
100
100
  a_foo.y
101
- end.to raise_error NoMethodError, "undefined method `y' for #{a_foo.inspect}"
101
+ end.to raise_error NoMethodError
102
102
  end
103
103
  end
104
104
 
105
+ describe 'immutability' do
106
+ let(:klass) { StrictStruct.new(:x) }
107
+
108
+ it "isn't broken by modifiying the input hash" do
109
+ params = {x: 'foo'}
110
+ k = klass.new(params)
111
+ params[:x] = 'bar'
112
+
113
+ k.x.should eq 'foo'
114
+ end
115
+
116
+ it "isn't broken by modifying output of to_h" do
117
+ k = klass.new(x: 'foo')
118
+ hash = k.to_h
119
+ hash[:x] = 'bar'
120
+
121
+ k.x.should eq 'foo'
122
+ end
123
+ end
105
124
 
106
125
  describe '#to_h' do
107
126
  it "returns all the values the object is defined by" do
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strict_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark IJbema
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-24 00:00:00.000000000 Z
11
+ date: 2014-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Extremely simple value objects
@@ -59,9 +59,10 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".travis.yml"
62
+ - .gitignore
63
+ - .travis.yml
64
64
  - Gemfile
65
+ - Gemfile.travis
65
66
  - LICENSE.txt
66
67
  - README.md
67
68
  - Rakefile
@@ -80,20 +81,21 @@ require_paths:
80
81
  - lib
81
82
  required_ruby_version: !ruby/object:Gem::Requirement
82
83
  requirements:
83
- - - ">="
84
+ - - '>='
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements:
88
- - - ">="
89
+ - - '>='
89
90
  - !ruby/object:Gem::Version
90
91
  version: '0'
91
92
  requirements: []
92
93
  rubyforge_project:
93
- rubygems_version: 2.2.1
94
+ rubygems_version: 2.0.3
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: StrictStruct is an immutable, named parameters based, replacement for Struct
97
98
  test_files:
98
99
  - spec/spec_helper.rb
99
100
  - spec/strict_struct_spec.rb
101
+ has_rdoc: