attr_init 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: 0f8d8370936afc741606695fc33b963636f01dfe
4
+ data.tar.gz: 8174381ab0003632aa7feb366a778d2d6be342dd
5
+ SHA512:
6
+ metadata.gz: 6c8ebc6254c78d3d872cce690b20bb5ac571dd4644385a39406aebffa001546cfa8ac03658bf9618d58c1ceab339b452cbe243c5433e01f973e8fd510e8e4c78
7
+ data.tar.gz: 51690168d15f9179dc8ee5d6087784d6743521b1b13f5c8af27d9c535e127cb7f44e3713a7b987f0539536fea77f5b4a9e7d89d7cf5f48335b816fc75e9d51ec
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.0
5
+
6
+ gemfile:
7
+ - Gemfile
8
+
9
+ cache: bundler
10
+
11
+ env:
12
+ matrix:
13
+ - TEST_SUITE="spec"
14
+
15
+ before_script: "bundle update"
16
+
17
+ script:
18
+ - bundle exec rake $TEST_SUITE
19
+
20
+ notifications:
21
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attr_init.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 johnmcconnell
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,100 @@
1
+ # AttrInit
2
+
3
+ [![Coverage
4
+ Status](https://coveralls.io/repos/johnmcconnell/attr_init/badge.png)](https://coveralls.io/r/johnmcconnell/attr_init)
5
+
6
+ So ruby has `Struct` but I never use it because:
7
+ 1. I have to extend the class with `Struct`
8
+ 2. It makes your instance_variables public
9
+ 3. It does not use hash initialization
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'attr_init'
17
+ ```
18
+
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install attr_init
27
+
28
+ ## Usage
29
+
30
+ ### Initializer
31
+
32
+ ```ruby
33
+ class Foo
34
+ attr_init :a, :b
35
+ end
36
+
37
+ f = Foo.new(a: 0, b: 1)
38
+ => #<Foo:0xb811601c @a=0, @b=1>
39
+
40
+ f.a
41
+ NoMethodError: undefined method: a' for #<Bar:0xb811601c @a=0, @b=1>
42
+ ```
43
+ If you want to you can override it:
44
+
45
+ ```ruby
46
+
47
+ class Foo
48
+ attr_init :a, :b
49
+ def initialize(params, opt=nil)
50
+ if opt.nil?
51
+ do_other_initializer
52
+ else
53
+ attr_init(params)
54
+ end
55
+ end
56
+ end
57
+
58
+ ```
59
+
60
+ ### Initializer with readers
61
+
62
+ ```ruby
63
+ class Bar
64
+ reader_struct :a, :b
65
+ end
66
+
67
+ b = Bar.new(a: 0, b: 1)
68
+ => #<Bar:0xaf11321c @a=0, @b=1>
69
+
70
+ b.a
71
+ => 0
72
+
73
+ b.a = 2
74
+ NoMethodError: undefined method: a=' for #<Bar:0xb931e250 @a=0, @b=1>
75
+ ```
76
+
77
+ ### Initializer with accessors
78
+
79
+ ```ruby
80
+ class Dah
81
+ accessor_struct :a, :b
82
+ end
83
+
84
+ d = Dah.new(a: 0, b: 1)
85
+ => #<Dah:0x81ad601c @a=0, @b=1>
86
+
87
+ d.a
88
+ => 0
89
+
90
+ d.a = 2
91
+ => 2
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it ( https://github.com/[johnmcconnell]/attr_init/fork )
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task default: :spec
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+
data/attr_init.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attr_init/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attr_init"
8
+ spec.version = AttrInit::VERSION
9
+ spec.authors = ["johnmcconnell"]
10
+ spec.email = ["johnnyillinois@gmail.com"]
11
+ spec.summary = %q{Reader structs for ruby finally!.}
12
+ spec.description = %q{Reader structs for ruby finally!.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 "simplecov", "~> 0.8.0"
22
+ spec.add_development_dependency "coveralls", "~> 0.7.0"
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,3 @@
1
+ module AttrInit
2
+ VERSION = "0.0.1"
3
+ end
data/lib/attr_init.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "attr_init/version"
2
+
3
+ def attr_init(*attrs)
4
+ define_method(:attr_init) do |params|
5
+ attrs.each do |a|
6
+ instance_variable_set "@#{a}", params[a]
7
+ end
8
+ end
9
+
10
+ protected :attr_init
11
+
12
+ define_method(:initialize) do |params|
13
+ attr_init(params)
14
+ end
15
+ end
16
+
17
+ def attr_hash(*attrs)
18
+ define_method(:to_h) do
19
+ attrs.each_with_object({}) do |attr, hash|
20
+ hash[attr] = send(attr)
21
+ end
22
+ end
23
+ end
24
+
25
+ def reader_struct(*attrs)
26
+ attr_reader *attrs
27
+ attr_init *attrs
28
+ attr_hash *attrs
29
+ end
30
+
31
+ def accessor_struct(*attrs)
32
+ attr_accessor *attrs
33
+ attr_init *attrs
34
+ attr_hash *attrs
35
+ end
36
+
37
+ module AttrInit
38
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'attr_init'
3
+
4
+ describe AttrInit do
5
+ let(:symbols) do
6
+ [:a, :b, :c]
7
+ end
8
+
9
+ let(:hash_params) do
10
+ Hash[symbols.each_with_index.to_a]
11
+ end
12
+
13
+ let(:reader_struct) do
14
+ class Foo
15
+ reader_struct :a, :b, :c
16
+ end
17
+ Foo
18
+ end
19
+
20
+ let(:accessor_struct) do
21
+ class Bar
22
+ accessor_struct :a, :b, :c
23
+ end
24
+ Bar
25
+ end
26
+
27
+ describe '::new' do
28
+ context 'reader_struct' do
29
+ it 'initializes readers with hash initializer' do
30
+ object = reader_struct.new(hash_params)
31
+
32
+ expect(object.a).to eq 0
33
+ expect(object.b).to eq 1
34
+ expect(object.c).to eq 2
35
+
36
+ expect { object.a = 3 }.to raise_error
37
+ end
38
+ end
39
+
40
+ context 'accessor_struct' do
41
+ it 'initializes accessor with hash initializer' do
42
+ object = accessor_struct.new(hash_params)
43
+
44
+ expect(object.a).to eq 0
45
+ expect(object.b).to eq 1
46
+ expect(object.c).to eq 2
47
+
48
+ object.a = 'hello'
49
+
50
+ expect(object.a).to eq 'hello'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ require 'attr_init'
2
+ require 'vcr'
3
+ require 'coveralls'
4
+
5
+ Coveralls.wear!
6
+
7
+ class SpecHelper
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_init
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - johnmcconnell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simplecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.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.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Reader structs for ruby finally!.
84
+ email:
85
+ - johnnyillinois@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - attr_init.gemspec
97
+ - lib/attr_init.rb
98
+ - lib/attr_init/version.rb
99
+ - spec/attr_init_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Reader structs for ruby finally!.
125
+ test_files:
126
+ - spec/attr_init_spec.rb
127
+ - spec/spec_helper.rb