attr_setting 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/README.md +150 -0
- data/Rakefile +5 -0
- data/attr_setting.gemspec +24 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/attr_setting.rb +30 -0
- data/lib/attr_setting/core_ext/module.rb +5 -0
- data/lib/attr_setting/version.rb +3 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2c8c832906e002f56e11bc11e2934695992b19d3
|
4
|
+
data.tar.gz: f985eabc281c6ac709bff5456f4eb1fc10565240
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6fb3f10f5e11baf2c709f2ef1cba86fc8ff60346f703df727fb49fb5815a398e9c9d1fb22f64c53725f7e58df89fe27a22d1d377384510794b149e97d3e641a
|
7
|
+
data.tar.gz: a1f4e767a71e071d76ab90dc5c6d4fc9ebb092f5834209a45ad9004d372da4430e787a59ca4c42659382961111fdcb5250a044861a1814fb17f5b789102e8c67
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
# AttrSetting
|
2
|
+
|
3
|
+
[](https://travis-ci.org/merhard/attr_setting)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'attr_setting'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install attr_setting
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`attr_setting` provides enhanced functionality to Ruby's `attr_accessor`.
|
24
|
+
|
25
|
+
To use, require the library and extend the module:
|
26
|
+
```ruby
|
27
|
+
require 'attr_setting'
|
28
|
+
|
29
|
+
class Config
|
30
|
+
extend AttrSetting
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
A monkeypatch is available to auto-extend `AttrSetting` for use in all Module and Class definitions:
|
35
|
+
```ruby
|
36
|
+
require 'attr_setting/core_ext/module'
|
37
|
+
```
|
38
|
+
|
39
|
+
`attr_setting` adds default values to `attr_accessor` in the form of a second argument or block:
|
40
|
+
```ruby
|
41
|
+
require 'attr_setting'
|
42
|
+
|
43
|
+
class Config
|
44
|
+
extend AttrSetting
|
45
|
+
|
46
|
+
attr_setting :foo, 'Second argument'
|
47
|
+
attr_setting(:bar) { 'Block' }
|
48
|
+
end
|
49
|
+
|
50
|
+
config = Config.new
|
51
|
+
config.foo # => 'Second argument'
|
52
|
+
config.bar # => 'Block'
|
53
|
+
```
|
54
|
+
|
55
|
+
The block is evaluated in the context of the object:
|
56
|
+
```ruby
|
57
|
+
require 'attr_setting'
|
58
|
+
|
59
|
+
class Config
|
60
|
+
extend AttrSetting
|
61
|
+
|
62
|
+
attr_setting :foo, 'Foo value'
|
63
|
+
attr_setting(:bar) { foo }
|
64
|
+
end
|
65
|
+
|
66
|
+
config = Config.new
|
67
|
+
config.foo # => 'Foo value'
|
68
|
+
config.bar # => 'Foo value'
|
69
|
+
```
|
70
|
+
|
71
|
+
The block is lazily evaluated:
|
72
|
+
```ruby
|
73
|
+
require 'attr_setting'
|
74
|
+
|
75
|
+
class Config
|
76
|
+
extend AttrSetting
|
77
|
+
|
78
|
+
attr_setting :foo
|
79
|
+
attr_setting(:bar) { foo }
|
80
|
+
end
|
81
|
+
|
82
|
+
config = Config.new
|
83
|
+
config.foo = 'New value'
|
84
|
+
config.bar # => 'New value'
|
85
|
+
```
|
86
|
+
|
87
|
+
`attr_setting` also adds a couple other features besides default values for `attr_accessor`.
|
88
|
+
|
89
|
+
It adds a predicate method:
|
90
|
+
```ruby
|
91
|
+
require 'attr_setting'
|
92
|
+
|
93
|
+
class Config
|
94
|
+
extend AttrSetting
|
95
|
+
|
96
|
+
attr_setting :foo
|
97
|
+
end
|
98
|
+
|
99
|
+
config = Config.new
|
100
|
+
config.foo? # => false
|
101
|
+
config.foo = :something
|
102
|
+
config.foo? # => true
|
103
|
+
```
|
104
|
+
|
105
|
+
It adds a bang method to reset values to their defaults:
|
106
|
+
```ruby
|
107
|
+
require 'attr_setting'
|
108
|
+
|
109
|
+
class Config
|
110
|
+
extend AttrSetting
|
111
|
+
|
112
|
+
attr_setting :foo, 'Default'
|
113
|
+
end
|
114
|
+
|
115
|
+
config = Config.new
|
116
|
+
config.foo # => 'Default'
|
117
|
+
config.foo = 'New value'
|
118
|
+
config.foo # => 'New value'
|
119
|
+
config.foo!
|
120
|
+
config.foo # => 'Default'
|
121
|
+
```
|
122
|
+
|
123
|
+
It treats getters with an argument as setters:
|
124
|
+
```ruby
|
125
|
+
require 'attr_setting'
|
126
|
+
|
127
|
+
class Config
|
128
|
+
extend AttrSetting
|
129
|
+
|
130
|
+
attr_setting :foo
|
131
|
+
end
|
132
|
+
|
133
|
+
config = Config.new
|
134
|
+
config.foo('New value')
|
135
|
+
config.foo # => 'New value'
|
136
|
+
```
|
137
|
+
|
138
|
+
## Development
|
139
|
+
|
140
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
141
|
+
|
142
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
143
|
+
|
144
|
+
## Contributing
|
145
|
+
|
146
|
+
1. Fork it ( https://github.com/[my-github-username]/attr_setting/fork )
|
147
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
148
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
149
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
150
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -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 'attr_setting/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "attr_setting"
|
8
|
+
spec.version = AttrSetting::VERSION
|
9
|
+
spec.authors = ["Matthew Erhard"]
|
10
|
+
spec.email = ["merhard@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enhanced attr_accessor}
|
13
|
+
spec.description = %q{Supplies macro similar to attr_accessor that adds default values, predicate methods, and other enhancements}
|
14
|
+
spec.homepage = "https://github.com/merhard/attr_setting"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
24
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "attr_setting"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/attr_setting.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "attr_setting/version"
|
2
|
+
|
3
|
+
module AttrSetting
|
4
|
+
private
|
5
|
+
|
6
|
+
def attr_setting(setting, default = nil)
|
7
|
+
ivar = "@#{setting}"
|
8
|
+
|
9
|
+
attr_writer setting
|
10
|
+
|
11
|
+
define_method(setting) do |value = :_not_supplied|
|
12
|
+
return instance_variable_set(ivar, value) unless value == :_not_supplied
|
13
|
+
|
14
|
+
if instance_variable_defined?(ivar)
|
15
|
+
instance_variable_get(ivar)
|
16
|
+
else
|
17
|
+
send("#{setting}!")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
define_method("#{setting}?") do
|
22
|
+
!!send(setting)
|
23
|
+
end
|
24
|
+
|
25
|
+
define_method("#{setting}!") do
|
26
|
+
default = instance_eval(&Proc.new) if block_given?
|
27
|
+
instance_variable_set(ivar, default)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_setting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Erhard
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-20 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: '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'
|
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: 3.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
description: Supplies macro similar to attr_accessor that adds default values, predicate
|
56
|
+
methods, and other enhancements
|
57
|
+
email:
|
58
|
+
- merhard@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- attr_setting.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/attr_setting.rb
|
73
|
+
- lib/attr_setting/core_ext/module.rb
|
74
|
+
- lib/attr_setting/version.rb
|
75
|
+
homepage: https://github.com/merhard/attr_setting
|
76
|
+
licenses: []
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.6
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Enhanced attr_accessor
|
98
|
+
test_files: []
|