Get_Set 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Get_Set.gemspec +2 -1
- data/README.md +13 -15
- data/lib/Get_Set.rb +25 -22
- data/lib/Get_Set/version.rb +1 -1
- data/spec/main.rb +1 -11
- data/spec/tests/DSL.rb +40 -33
- metadata +4 -4
data/Get_Set.gemspec
CHANGED
@@ -22,8 +22,9 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.add_development_dependency "bacon"
|
23
23
|
s.add_development_dependency "rake"
|
24
24
|
s.add_development_dependency 'Bacon_Colored'
|
25
|
+
s.add_development_dependency 'pry'
|
25
26
|
|
26
27
|
# s.rubyforge_project = "Get_Set"
|
27
28
|
# specify any dependencies here; for example:
|
28
|
-
s.add_runtime_dependency "Checked"
|
29
|
+
# s.add_runtime_dependency "Checked"
|
29
30
|
end
|
data/README.md
CHANGED
@@ -17,29 +17,27 @@ Use:
|
|
17
17
|
require "Get_Set"
|
18
18
|
|
19
19
|
class Uni_Bash
|
20
|
+
|
20
21
|
include Get_Set::DSL
|
21
22
|
|
22
|
-
attr_get_set :name, :age
|
23
|
+
attr_get_set :name, :age, :homes, :var_and_block, :vars_and_block
|
24
|
+
|
23
25
|
end
|
24
26
|
|
25
|
-
|
26
27
|
o = Uni_Bash.new
|
27
|
-
|
28
|
+
|
28
29
|
# --- Setters
|
29
30
|
o.name "Uncle Martin"
|
30
31
|
o.age 190
|
31
|
-
|
32
|
-
|
33
|
-
o.
|
34
|
-
o.age
|
35
|
-
|
32
|
+
o.homes "Mars", "California"
|
33
|
+
o.var_and_block(:a) { :b }
|
34
|
+
o.vars_and_block(:a, :b, :c) { :d }
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
it and use something better.
|
36
|
+
# ---- Getters
|
37
|
+
o.name # --> 'Uncle Martin
|
38
|
+
o.age # --> 190
|
39
|
+
o.homes # --> [ 'Mars', 'California' ]
|
40
|
+
o.var_and_block # --> [ :a, Proc Instance ]
|
41
|
+
o.vars_and_block # --> [ [:a, :b, :c], Proc Instance ]</pre>
|
44
42
|
|
45
43
|
|
data/lib/Get_Set.rb
CHANGED
@@ -1,27 +1,42 @@
|
|
1
1
|
require "Get_Set/version"
|
2
|
-
require 'Checked'
|
3
2
|
|
4
3
|
module Get_Set
|
5
4
|
|
6
5
|
module DSL
|
7
6
|
|
8
|
-
include Checked::DSL
|
9
|
-
|
10
7
|
def self.included klass
|
11
8
|
klass.extend Class_Methods
|
12
9
|
end
|
13
10
|
|
14
11
|
module Class_Methods
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
def attr_get_set *names
|
19
|
-
Array!(names).symbols!
|
13
|
+
def attr_get_set *raw_names
|
14
|
+
names = raw_names.flatten.map(&:to_s).sort.map(&:to_sym)
|
20
15
|
names.each { |n|
|
21
16
|
class_eval %~
|
22
|
-
def #{n} *args
|
23
|
-
|
24
|
-
|
17
|
+
def #{n} *args, &blok
|
18
|
+
|
19
|
+
if !args.empty? || block_given?
|
20
|
+
|
21
|
+
final = case args.size
|
22
|
+
when 1
|
23
|
+
args.first
|
24
|
+
when 0
|
25
|
+
nil
|
26
|
+
else
|
27
|
+
args
|
28
|
+
end
|
29
|
+
|
30
|
+
if block_given?
|
31
|
+
final = [final, blok].compact
|
32
|
+
final = final.first if final.size == 1
|
33
|
+
end
|
34
|
+
|
35
|
+
instance_variable_set( :@#{n}, final )
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
instance_variable_get( :@#{n} )
|
25
40
|
end
|
26
41
|
~, __FILE__, __LINE__
|
27
42
|
}
|
@@ -29,18 +44,6 @@ module Get_Set
|
|
29
44
|
|
30
45
|
end # === module Class_Methods
|
31
46
|
|
32
|
-
def Get_Set! *args
|
33
|
-
Size! args, 1..2
|
34
|
-
name = args.shift
|
35
|
-
raise ArgumentError, "Instance variable #{name.inspect} has not been defined." unless instance_variable_defined?(name)
|
36
|
-
|
37
|
-
if args.empty?
|
38
|
-
instance_variable_get name
|
39
|
-
else
|
40
|
-
instance_variable_set name, args.first
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
47
|
end # === module DSL
|
45
48
|
|
46
49
|
end # === module Get_Set
|
data/lib/Get_Set/version.rb
CHANGED
data/spec/main.rb
CHANGED
@@ -2,17 +2,7 @@
|
|
2
2
|
require File.expand_path('spec/helper')
|
3
3
|
require 'Get_Set'
|
4
4
|
require 'Bacon_Colored'
|
5
|
-
|
6
|
-
Klass = Class.new {
|
7
|
-
|
8
|
-
include Get_Set::DSL
|
9
|
-
attr_get_set :name, :location
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@name = 'Ultra Force'
|
13
|
-
end
|
14
|
-
|
15
|
-
}
|
5
|
+
require 'pry'
|
16
6
|
|
17
7
|
Dir.glob('spec/tests/*.rb').each { |file|
|
18
8
|
require File.expand_path(file.sub('.rb', '')) if File.file?(file)
|
data/spec/tests/DSL.rb
CHANGED
@@ -1,60 +1,67 @@
|
|
1
1
|
|
2
|
+
class Klassy
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
lambda { Get_Set! :@something }
|
9
|
-
.should.raise(ArgumentError)
|
10
|
-
.message.should.match %r!@something has not been defined!
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'gets instance variable if array is empty.' do
|
14
|
-
@server = :hi
|
15
|
-
Get_Set!( :@server ).should == @server
|
4
|
+
include Get_Set::DSL
|
5
|
+
attr_get_set :name, :notify, :location
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@name = 'Ultra Force'
|
16
9
|
end
|
17
10
|
|
18
|
-
end # ===
|
11
|
+
end # === Klassy
|
19
12
|
|
20
|
-
describe "
|
13
|
+
describe "attr_get_set :var" do
|
21
14
|
|
22
|
-
before {
|
15
|
+
before { @klass = Klassy.new }
|
23
16
|
|
24
|
-
it '
|
25
|
-
|
26
|
-
|
27
|
-
.
|
17
|
+
it 'gets instance variable if no arguments given' do
|
18
|
+
t = 'Screaming Viking'
|
19
|
+
o = @klass
|
20
|
+
o.name t
|
21
|
+
o.name.should == t
|
28
22
|
end
|
29
23
|
|
30
24
|
it 'sets instance variable.' do
|
31
|
-
|
32
|
-
|
33
|
-
|
25
|
+
t = 'Quite Viking'
|
26
|
+
o = @klass
|
27
|
+
o.name t
|
28
|
+
o.instance_eval { @name }.should == t
|
34
29
|
end
|
35
30
|
|
36
|
-
it '
|
37
|
-
|
38
|
-
|
31
|
+
it 'saves more than one argument as Array' do
|
32
|
+
t = [ :a, :b, :c]
|
33
|
+
@klass.name *t
|
34
|
+
@klass.name.should == t
|
39
35
|
end
|
40
36
|
|
41
|
-
end # === describe Get_Set!
|
42
|
-
|
43
|
-
describe ":attr_get_set :name" do
|
44
|
-
|
45
|
-
before { @klass = Klass.new }
|
46
|
-
|
47
37
|
it 'defines method to retrieve value' do
|
48
38
|
@klass.name.should == 'Ultra Force'
|
49
39
|
end
|
50
40
|
|
51
41
|
it 'defines method to set value' do
|
52
|
-
@klass.name
|
42
|
+
@klass.name :Ultra
|
53
43
|
@klass.name.should == :Ultra
|
54
44
|
end
|
55
45
|
|
56
46
|
it 'initializes variable to nil if not defined' do
|
57
47
|
@klass.location.should == nil
|
58
48
|
end
|
49
|
+
|
50
|
+
it 'saves proc given given to setter as its value' do
|
51
|
+
@klass.location {}
|
52
|
+
@klass.location.is_a?(Proc).should == true
|
53
|
+
end
|
54
|
+
|
55
|
+
it "saves value and proc given to setter as an array" do
|
56
|
+
b = Proc.new { :b }
|
57
|
+
@klass.location(:a, &b)
|
58
|
+
@klass.location.should == [:a, b]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "saves values and proc given to setter as an array within an array" do
|
62
|
+
c = Proc.new { :c }
|
63
|
+
@klass.location(:a, :b, &c)
|
64
|
+
@klass.location.should == [[:a, :b], c]
|
65
|
+
end
|
59
66
|
|
60
67
|
end # === describe :attr_get_set :name
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Get_Set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bacon
|
@@ -60,14 +60,14 @@ dependencies:
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: pry
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
67
67
|
- - ! '>='
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
|
-
type: :
|
70
|
+
type: :development
|
71
71
|
prerelease: false
|
72
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
73
|
none: false
|