strict_struct 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b6bb2125a155d3e7bdaeeec1911f866b2794a06
4
+ data.tar.gz: b3a9be0fb018a7eb7a44bb51eeee5169a64a29cf
5
+ SHA512:
6
+ metadata.gz: 29092b1f8a93d319377f94bf71c900e28c52e2f30fd1da03a0160c8c90e40656c3432f5b09eeedfea1cc91a3486bb10de2e34064ad78e2747de01e755fef5c02
7
+ data.tar.gz: 1f98e60cec646a207424cd0efda65d2e2cbad1031247ff71c477e346839adbbfa1c47b6096f1bacd74267ffb85377a69c7e0d9a60ac3d1926036333992899ccd
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - ruby-head
7
+ - rbx
8
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in strict_struct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mark IJbema
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
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)
2
+
3
+ This gems aims to provide a modern version of Struct.
4
+ While Struct is a nice easy way to create a light-weight
5
+ value object, it has some drawbacks
6
+
7
+ * You need to remember the order of arguments
8
+ * The object is mutable by default
9
+
10
+ This gem aims to avoid these drawbacks, while providing the
11
+ ease of use of Struct.
12
+
13
+ ## Usage
14
+
15
+ If you want to create a simple object, just declare it like
16
+ you would declare a Struct:
17
+
18
+ ```ruby
19
+ Rectangle = StrictStruct.new(:x, :y) do
20
+ def area
21
+ x * y
22
+ end
23
+ end
24
+ ```
25
+
26
+ This would conceptually create the following object:
27
+
28
+ ```ruby
29
+ class Rectange
30
+ attr_reader :x, :y
31
+
32
+ def initialize(x:, y:)
33
+ end
34
+
35
+ def area
36
+ x * y
37
+ end
38
+ end
39
+ ```
40
+
41
+ ## Installation
42
+
43
+ Add this line to your application's Gemfile:
44
+
45
+ gem 'strict_struct'
46
+
47
+ And then execute:
48
+
49
+ $ bundle
50
+
51
+ Or install it yourself as:
52
+
53
+ $ gem install strict_struct
54
+
55
+
56
+ TODO: Write usage instructions here
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ # Make running the tests the default task.
8
+ task(default: :spec)
@@ -0,0 +1,3 @@
1
+ module StrictStruct
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ #require "strict_struct/version"
2
+
3
+ module StrictStruct
4
+ module Helper
5
+ def self.assert_keyword_error type, keywords
6
+ if keywords.length == 1
7
+ raise ArgumentError, "#{type} keyword: " + keywords.first.to_s
8
+ elsif keywords.length > 1
9
+ raise ArgumentError, "#{type} keywords: " + keywords.join(', ')
10
+ end
11
+ end
12
+
13
+ def self.validate_arguments given, expected
14
+ missing_arguments = expected.reject {|name| given.include? name }
15
+ Helper.assert_keyword_error "missing", missing_arguments
16
+
17
+ extra_arguments = given.reject {|name| expected.include? name }
18
+ Helper.assert_keyword_error "unknown", extra_arguments
19
+ end
20
+ end
21
+
22
+ def self.new(*attributes, &block)
23
+ klass = Class.new do
24
+ @@attributes = attributes
25
+
26
+ define_method :initialize do |hash={}|
27
+ @hash = hash
28
+ Helper.validate_arguments(@hash.keys, @@attributes)
29
+ end
30
+
31
+ attributes.each do |attribute|
32
+ define_method(attribute) {|| @hash[attribute] }
33
+ end
34
+
35
+ def to_h
36
+ @hash
37
+ end
38
+
39
+ def == (other)
40
+ @@attributes.all? {|name| self.send(name) == other.send(name)}
41
+ end
42
+ end
43
+ klass.class_eval(&block) if block_given?
44
+ klass
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+
3
+ $LOAD_PATH << './lib/'
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+ require 'strict_struct'
3
+
4
+ describe 'StrictStruct' do
5
+ describe 'initialization' do
6
+ it "works without parameters" do
7
+ foo = StrictStruct.new
8
+
9
+ a_foo = foo.new
10
+
11
+ a_foo.should_not be_nil
12
+ end
13
+
14
+ it "works with parameters" do
15
+ foo = StrictStruct.new(:bar)
16
+
17
+ a_foo = foo.new(bar: 'baz')
18
+
19
+ a_foo.should_not be_nil
20
+ end
21
+
22
+ it "raises an error when an argument is missing" do
23
+ foo = StrictStruct.new(:x)
24
+
25
+ expect do
26
+ foo.new()
27
+ end.to raise_error ArgumentError, "missing keyword: x"
28
+ end
29
+
30
+ it "raises an error when multiple arguments are missing" do
31
+ foo = StrictStruct.new(:x, :y, :z)
32
+
33
+ expect do
34
+ foo.new(z: 'bar')
35
+ end.to raise_error ArgumentError, "missing keywords: x, y"
36
+ end
37
+
38
+ it "raises an error when an unknown argument is passed in" do
39
+ foo = StrictStruct.new(:x)
40
+
41
+ expect do
42
+ foo.new(x: 'foo', z: 'bar')
43
+ end.to raise_error ArgumentError, "unknown keyword: z"
44
+ end
45
+
46
+ it "raises an error when multiple unknown argument are passed in" do
47
+ foo = StrictStruct.new(:x)
48
+
49
+ expect do
50
+ foo.new(x: 'foo', z: 'bar', w: 'baz')
51
+ end.to raise_error ArgumentError, "unknown keywords: z, w"
52
+ end
53
+
54
+ it "raises a missing error when both missing arguments and unknown arguments are passed in" do
55
+ foo = StrictStruct.new(:x, :y)
56
+
57
+ expect do
58
+ foo.new(x: 'foo', w: 'baz')
59
+ end.to raise_error ArgumentError, "missing keyword: y"
60
+ end
61
+ end
62
+
63
+ describe 'defining one' do
64
+ it "allows to define methods using block" do
65
+ rectangle_class = StrictStruct.new(:x, :y) do
66
+ def area
67
+ x * y
68
+ end
69
+ end
70
+
71
+ rectangle = rectangle_class.new(x: 3, y: 5)
72
+
73
+ rectangle.area.should == 15
74
+ end
75
+ end
76
+
77
+ describe "attribute readers" do
78
+ it "returns the value initialized with" do
79
+ foo = StrictStruct.new(:bar)
80
+
81
+ a_foo = foo.new(bar: 'baz')
82
+
83
+ a_foo.bar.should eq 'baz'
84
+ a_foo.should respond_to 'bar'
85
+ end
86
+
87
+ it "is not defined when the value isn't defined by the class" do
88
+ foo = StrictStruct.new(:bar)
89
+
90
+ a_foo = foo.new(bar: 'baz')
91
+
92
+ a_foo.should_not respond_to 'gerard'
93
+ end
94
+
95
+ it "raises a NoMethodError when called for a value which isn't defined" do
96
+ foo = StrictStruct.new(:x)
97
+ a_foo = foo.new(x: 3)
98
+
99
+ expect do
100
+ a_foo.y
101
+ end.to raise_error NoMethodError, "undefined method `y' for #{a_foo.inspect}"
102
+ end
103
+ end
104
+
105
+
106
+ describe '#to_h' do
107
+ it "returns all the values the object is defined by" do
108
+ foo = StrictStruct.new(:bar, :baz)
109
+
110
+ a_foo = foo.new(bar: 'baz', baz: 3)
111
+
112
+ a_foo.to_h.should eq({bar: 'baz', baz: 3})
113
+ end
114
+ end
115
+
116
+ describe '#==' do
117
+ it "returns true if the values are the same" do
118
+ foo = StrictStruct.new(:bar)
119
+
120
+ a_foo = foo.new(bar: 'baz')
121
+ another_foo = foo.new(bar: 'baz')
122
+
123
+ a_foo.should eq another_foo
124
+ end
125
+ it "returns true if the values are the same" do
126
+ foo = StrictStruct.new(:bar)
127
+
128
+ a_foo = foo.new(bar: 'baz')
129
+ another_foo = foo.new(bar: 'not_baz')
130
+
131
+ a_foo.should_not eq another_foo
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'strict_struct/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strict_struct"
8
+ spec.version = StrictStruct::VERSION
9
+ spec.authors = ["Mark IJbema"]
10
+ spec.email = ["markijbema@gmail.com"]
11
+ spec.description = %q{Extremely simple value objects}
12
+ spec.summary = %q{StrictStruct is an immutable, named parameters based, replacement for Struct}
13
+ spec.homepage = "https://github.com/markijbema/strict_struct"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strict_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark IJbema
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Extremely simple value objects
56
+ email:
57
+ - markijbema@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/strict_struct.rb
69
+ - lib/strict_struct/version.rb
70
+ - spec/spec_helper.rb
71
+ - spec/strict_struct_spec.rb
72
+ - strict_struct.gemspec
73
+ homepage: https://github.com/markijbema/strict_struct
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: StrictStruct is an immutable, named parameters based, replacement for Struct
97
+ test_files:
98
+ - spec/spec_helper.rb
99
+ - spec/strict_struct_spec.rb