classy-adornments 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +56 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/classy-adornments.gemspec +63 -0
- data/lib/classy_adornments.rb +41 -0
- data/spec/classy_adornments_spec.rb +62 -0
- data/spec/spec_helper.rb +12 -0
- metadata +137 -0
data/.document
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Michael Barton
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
= classy-adornments
|
2
|
+
|
3
|
+
More functional versions of the attr, attr\_reader and attr\_writer methods.
|
4
|
+
I often find myself repeating these across projects so I've extracted them into
|
5
|
+
a gem. Inspired by 'Chapter 2 - Designing Beautiful APIs' in the {'Ruby Best
|
6
|
+
Practices' book}[http://rubybestpractices.com/].
|
7
|
+
|
8
|
+
== Simple adornment
|
9
|
+
|
10
|
+
Provides a single method interface to instance variables. Methods calls without
|
11
|
+
an argument return the attribute value. Method calls with an argument set
|
12
|
+
corresponding instance variable to this value.
|
13
|
+
|
14
|
+
class Person
|
15
|
+
include ClassyAdornments
|
16
|
+
|
17
|
+
adorn :partner
|
18
|
+
end
|
19
|
+
|
20
|
+
developer = Person.new
|
21
|
+
|
22
|
+
developer.partner #=> nil
|
23
|
+
developer.partner "Sophie"
|
24
|
+
|
25
|
+
developer.partner #=> "Sophie"
|
26
|
+
|
27
|
+
developer.instance_variable_get("@partner") #=> The instance variable also set
|
28
|
+
developer.partner = "Sophie" #=> Method variable= also usable
|
29
|
+
|
30
|
+
== Array adornment
|
31
|
+
|
32
|
+
Provides an adornment using an array as a base. Method calls with an argument
|
33
|
+
append the argument to the array. Empty method calls return the array of
|
34
|
+
values.
|
35
|
+
|
36
|
+
class Relationship
|
37
|
+
include ClassyAdornments
|
38
|
+
|
39
|
+
adorn :dates, :array => true
|
40
|
+
end
|
41
|
+
|
42
|
+
romance = Relationship.new
|
43
|
+
|
44
|
+
romance.dates #=> [] Initialised with empty array
|
45
|
+
|
46
|
+
romance.dates(:wings) #=> Value appended to array
|
47
|
+
romance.dates #=> [:wings]
|
48
|
+
|
49
|
+
romance.dates([:drive_thru,:tv_night]) #=> Array values also added
|
50
|
+
romance.dates #=> [:wings,:drive_thru,:tv_night]
|
51
|
+
|
52
|
+
= Copyright
|
53
|
+
|
54
|
+
Copyright (c) 2011 Michael Barton. See LICENSE.txt for
|
55
|
+
further details.
|
56
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
gem.name = "classy-adornments"
|
15
|
+
gem.homepage = "http://github.com/michaelbarton/classy-adornments"
|
16
|
+
gem.license = "MIT"
|
17
|
+
gem.summary = %Q{Adorn your classes with fancy attribute methods}
|
18
|
+
gem.description = %Q{Extended class methods for defining attributes.}
|
19
|
+
gem.email = "mail@michaelbarton.me.uk"
|
20
|
+
gem.authors = ["Michael Barton"]
|
21
|
+
end
|
22
|
+
Jeweler::RubygemsDotOrgTasks.new
|
23
|
+
|
24
|
+
require 'rspec/core'
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
27
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :spec
|
31
|
+
|
32
|
+
require 'yard'
|
33
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.2
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{classy-adornments}
|
8
|
+
s.version = "0.1.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Barton"]
|
12
|
+
s.date = %q{2011-04-05}
|
13
|
+
s.description = %q{Extended class methods for defining attributes.}
|
14
|
+
s.email = %q{mail@michaelbarton.me.uk}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"classy-adornments.gemspec",
|
27
|
+
"lib/classy_adornments.rb",
|
28
|
+
"spec/classy_adornments_spec.rb",
|
29
|
+
"spec/spec_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/michaelbarton/classy-adornments}
|
32
|
+
s.licenses = ["MIT"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{Adorn your classes with fancy attribute methods}
|
36
|
+
s.test_files = [
|
37
|
+
"spec/classy_adornments_spec.rb",
|
38
|
+
"spec/spec_helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
47
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5"])
|
48
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.5"])
|
49
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
52
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5"])
|
53
|
+
s.add_dependency(%q<rspec>, ["~> 2.5"])
|
54
|
+
s.add_dependency(%q<yard>, ["~> 0.6"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
58
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5"])
|
59
|
+
s.add_dependency(%q<rspec>, ["~> 2.5"])
|
60
|
+
s.add_dependency(%q<yard>, ["~> 0.6"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ClassyAdornments
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(self)
|
5
|
+
end
|
6
|
+
|
7
|
+
def adorn(name,opts = {})
|
8
|
+
adorn_with name if opts.empty?
|
9
|
+
adorn_with_array name if opts[:array]
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def adorn_with(name)
|
15
|
+
define_method(name) do |*args|
|
16
|
+
var = '@' + name.to_s
|
17
|
+
if args.first
|
18
|
+
return instance_variable_set(var,args.first)
|
19
|
+
else
|
20
|
+
return instance_variable_get(var)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias_method "#{name}=", name
|
24
|
+
end
|
25
|
+
|
26
|
+
def adorn_with_array(name)
|
27
|
+
define_method(name) do |*args|
|
28
|
+
var = '@' + name.to_s
|
29
|
+
instance_variable_set(var,Array.new) if instance_variable_get(var).nil?
|
30
|
+
return case args.first
|
31
|
+
when nil
|
32
|
+
instance_variable_get(var)
|
33
|
+
when Array
|
34
|
+
instance_variable_get(var).concat args.first
|
35
|
+
else
|
36
|
+
instance_variable_get(var) << args.first
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe ClassyAdornments, "#adorn" do
|
4
|
+
|
5
|
+
let(:test_class) do
|
6
|
+
dummy = Class.new
|
7
|
+
dummy.send(:include, ClassyAdornments)
|
8
|
+
dummy
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "with single argument" do
|
12
|
+
|
13
|
+
subject do
|
14
|
+
test_class.adorn :some_attr
|
15
|
+
test_class
|
16
|
+
end
|
17
|
+
|
18
|
+
its(:instance_methods){should include('some_attr') }
|
19
|
+
its(:instance_methods){should include('some_attr=') }
|
20
|
+
|
21
|
+
it "should provide working getter/setter method" do
|
22
|
+
obj = subject.new
|
23
|
+
obj.some_attr.should == nil
|
24
|
+
obj.some_attr(5).should == 5
|
25
|
+
obj.some_attr.should == 5
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should create the an instance variable" do
|
29
|
+
obj = subject.new
|
30
|
+
attribute = Object.new
|
31
|
+
obj.some_attr(attribute)
|
32
|
+
|
33
|
+
obj.instance_variable_get("@some_attr").should equal(attribute)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "with empty array argument" do
|
39
|
+
|
40
|
+
subject do
|
41
|
+
test_class.adorn :array_attr, :array => true
|
42
|
+
test_class.new
|
43
|
+
end
|
44
|
+
|
45
|
+
its("class.instance_methods"){should include('array_attr') }
|
46
|
+
its("class.instance_methods"){should_not include('array_attr=') }
|
47
|
+
|
48
|
+
it "should append single values" do
|
49
|
+
subject.array_attr.should == []
|
50
|
+
subject.array_attr :something
|
51
|
+
subject.array_attr.should == [:something]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should append an array of values" do
|
55
|
+
subject.array_attr.should == []
|
56
|
+
subject.array_attr 1
|
57
|
+
subject.array_attr [2,3]
|
58
|
+
subject.array_attr.should == [1,2,3]
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'classy_adornments'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: classy-adornments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Michael Barton
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-05 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 15
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
version: "1.0"
|
32
|
+
type: :development
|
33
|
+
name: bundler
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 5
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 5
|
46
|
+
version: "1.5"
|
47
|
+
type: :development
|
48
|
+
name: jeweler
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 9
|
58
|
+
segments:
|
59
|
+
- 2
|
60
|
+
- 5
|
61
|
+
version: "2.5"
|
62
|
+
type: :development
|
63
|
+
name: rspec
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ~>
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 7
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
- 6
|
76
|
+
version: "0.6"
|
77
|
+
type: :development
|
78
|
+
name: yard
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *id004
|
81
|
+
description: Extended class methods for defining attributes.
|
82
|
+
email: mail@michaelbarton.me.uk
|
83
|
+
executables: []
|
84
|
+
|
85
|
+
extensions: []
|
86
|
+
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.rdoc
|
90
|
+
files:
|
91
|
+
- .document
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.rdoc
|
95
|
+
- Rakefile
|
96
|
+
- VERSION
|
97
|
+
- classy-adornments.gemspec
|
98
|
+
- lib/classy_adornments.rb
|
99
|
+
- spec/classy_adornments_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: http://github.com/michaelbarton/classy-adornments
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 1.3.7
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Adorn your classes with fancy attribute methods
|
135
|
+
test_files:
|
136
|
+
- spec/classy_adornments_spec.rb
|
137
|
+
- spec/spec_helper.rb
|