has-properties 1.0.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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +81 -0
- data/Rakefile +7 -0
- data/has-properties.gemspec +22 -0
- data/lib/has-properties/version.rb +3 -0
- data/lib/has_properties.rb +43 -0
- data/spec/has_properties_spec.rb +50 -0
- data/spec/spec_helper.rb +17 -0
- metadata +90 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Rick Button
|
|
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,81 @@
|
|
|
1
|
+
# has-properties
|
|
2
|
+
|
|
3
|
+
`has_properties` provides an easy way to create basic class definitions with properties.
|
|
4
|
+
It essentially wraps a lot of `attr_(reader/writer/accessor)` calls into a neat package
|
|
5
|
+
with a nifty initializer.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
gem 'has-properties'
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install has-properties
|
|
20
|
+
|
|
21
|
+
## Usage Example
|
|
22
|
+
|
|
23
|
+
1. Include the `HasProperties` module
|
|
24
|
+
2. Define your properties
|
|
25
|
+
3. ??????
|
|
26
|
+
4. Profit?
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
class Example
|
|
30
|
+
include HasProperties
|
|
31
|
+
|
|
32
|
+
has_read_properties :readable, :another_readable
|
|
33
|
+
has_write_properties :writable, :another_writable
|
|
34
|
+
has_properties :readable_and_writable
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
e = Example.new(:readable => 1, :writable => 2, :readable_and_writable => 3)
|
|
38
|
+
```
|
|
39
|
+
```
|
|
40
|
+
e.readable
|
|
41
|
+
=> 1
|
|
42
|
+
```
|
|
43
|
+
```
|
|
44
|
+
e.readable = 2
|
|
45
|
+
=> NoMethodError
|
|
46
|
+
```
|
|
47
|
+
```
|
|
48
|
+
e.writable
|
|
49
|
+
=> NoMethodError
|
|
50
|
+
```
|
|
51
|
+
```
|
|
52
|
+
e.writable = 3
|
|
53
|
+
=> 3
|
|
54
|
+
```
|
|
55
|
+
```
|
|
56
|
+
e.readable_and_writable
|
|
57
|
+
=> 3
|
|
58
|
+
```
|
|
59
|
+
```
|
|
60
|
+
e.readable_and_writable = 4
|
|
61
|
+
=> 4
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Some notes
|
|
65
|
+
|
|
66
|
+
You don't have to include every property in the initializer hash. For example, the example above
|
|
67
|
+
defines the `:another_readable` property, but it was never initialized when the object was created.
|
|
68
|
+
This means that a call to `e.another_readable` would equal `nil`.
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
e.another_readable
|
|
72
|
+
=> nil
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Contributing
|
|
76
|
+
|
|
77
|
+
1. Fork it
|
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
79
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
81
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/has-properties/version', __FILE__)
|
|
3
|
+
require File.expand_path('../lib/has_properties', __FILE__)
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |gem|
|
|
6
|
+
gem.authors = ["Rick Button"]
|
|
7
|
+
gem.email = ["me@rickybutton.com"]
|
|
8
|
+
gem.description = %q{Gem that provides an easy way to create basic objects with properties}
|
|
9
|
+
gem.summary = %q{Gem that provides an easy way to create basic objects with properties}
|
|
10
|
+
gem.homepage = ""
|
|
11
|
+
|
|
12
|
+
gem.files = `git ls-files`.split($\)
|
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
15
|
+
gem.name = "has-properties"
|
|
16
|
+
gem.require_paths = ["lib"]
|
|
17
|
+
gem.version = HasProperties::VERSION
|
|
18
|
+
|
|
19
|
+
%w(rspec rake).each do |g|
|
|
20
|
+
gem.add_development_dependency g
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module HasProperties
|
|
2
|
+
attr_accessor :props
|
|
3
|
+
|
|
4
|
+
def has_read_properties(*args)
|
|
5
|
+
@props ||= []
|
|
6
|
+
check_unique args
|
|
7
|
+
@props += args
|
|
8
|
+
instance_eval { attr_reader *args }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def has_write_properties(*args)
|
|
12
|
+
@props ||= []
|
|
13
|
+
check_unique args
|
|
14
|
+
@props += args
|
|
15
|
+
instance_eval { attr_writer *args }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def has_read_write_properties(*args)
|
|
19
|
+
@props ||= []
|
|
20
|
+
check_unique args
|
|
21
|
+
@props += args
|
|
22
|
+
instance_eval { attr_accessor *args }
|
|
23
|
+
end
|
|
24
|
+
alias_method :has_properties, :has_read_write_properties
|
|
25
|
+
|
|
26
|
+
def self.included base
|
|
27
|
+
base.extend self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(args)
|
|
31
|
+
args.each {|k,v|
|
|
32
|
+
instance_variable_set "@#{k}", v if self.class.props.member?(k)
|
|
33
|
+
} if args.is_a? Hash
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
def check_unique(args)
|
|
38
|
+
common = @props & args
|
|
39
|
+
raise(ArgumentError,
|
|
40
|
+
"You can only define a property once.\n" +
|
|
41
|
+
"These properties were defined more than once: #{common.join(',')}") unless common.empty?
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe HasProperties do
|
|
4
|
+
|
|
5
|
+
class Test
|
|
6
|
+
include HasProperties
|
|
7
|
+
has_read_properties :one, :two
|
|
8
|
+
has_write_properties :three, :four
|
|
9
|
+
has_read_write_properties :five
|
|
10
|
+
has_properties :six
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
before(:each) do
|
|
14
|
+
@test = Test.new(:one => 'one',
|
|
15
|
+
:two => 'two',
|
|
16
|
+
:three => 'three',
|
|
17
|
+
:four => 'four',
|
|
18
|
+
:five => 'five',
|
|
19
|
+
:six => 'six')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'should generate the correct setters' do
|
|
23
|
+
@test.one.should eq 'one'
|
|
24
|
+
@test.two.should eq 'two'
|
|
25
|
+
|
|
26
|
+
@test.respond_to?(:three).should be_false
|
|
27
|
+
@test.respond_to?(:four).should be_false
|
|
28
|
+
|
|
29
|
+
@test.five.should eq 'five'
|
|
30
|
+
@test.six.should eq 'six'
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'should generate the correct setters' do
|
|
34
|
+
@test.respond_to?(:one=).should be_false
|
|
35
|
+
@test.respond_to?(:two=).should be_false
|
|
36
|
+
|
|
37
|
+
lambda { @test.three = 'new_three' }.should_not raise_error
|
|
38
|
+
lambda { @test.four = 'new_four' }.should_not raise_error
|
|
39
|
+
@test.five = 'new_five'
|
|
40
|
+
@test.six = 'new_six'
|
|
41
|
+
|
|
42
|
+
@test.five.should eq 'new_five'
|
|
43
|
+
@test.six.should eq 'new_six'
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'should not throw an error when extra parameters are used' do
|
|
47
|
+
lambda { Test.new(:random => 'oops poop') }.should_not raise_error
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
|
4
|
+
# loaded once.
|
|
5
|
+
#
|
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
7
|
+
RSpec.configure do |config|
|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
|
9
|
+
config.run_all_when_everything_filtered = true
|
|
10
|
+
config.filter_run :focus
|
|
11
|
+
|
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
|
14
|
+
# the seed, which is printed after each run.
|
|
15
|
+
# --seed 1234
|
|
16
|
+
config.order = 'random'
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: has-properties
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Rick Button
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-12-08 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rake
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
description: Gem that provides an easy way to create basic objects with properties
|
|
47
|
+
email:
|
|
48
|
+
- me@rickybutton.com
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- .gitignore
|
|
54
|
+
- .rspec
|
|
55
|
+
- Gemfile
|
|
56
|
+
- LICENSE
|
|
57
|
+
- README.md
|
|
58
|
+
- Rakefile
|
|
59
|
+
- has-properties.gemspec
|
|
60
|
+
- lib/has-properties/version.rb
|
|
61
|
+
- lib/has_properties.rb
|
|
62
|
+
- spec/has_properties_spec.rb
|
|
63
|
+
- spec/spec_helper.rb
|
|
64
|
+
homepage: ''
|
|
65
|
+
licenses: []
|
|
66
|
+
post_install_message:
|
|
67
|
+
rdoc_options: []
|
|
68
|
+
require_paths:
|
|
69
|
+
- lib
|
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
|
+
none: false
|
|
72
|
+
requirements:
|
|
73
|
+
- - ! '>='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
none: false
|
|
78
|
+
requirements:
|
|
79
|
+
- - ! '>='
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 1.8.23
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 3
|
|
87
|
+
summary: Gem that provides an easy way to create basic objects with properties
|
|
88
|
+
test_files:
|
|
89
|
+
- spec/has_properties_spec.rb
|
|
90
|
+
- spec/spec_helper.rb
|