ghaki-meta 2011.11.29.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.
- data/LICENSE +19 -0
- data/README +23 -0
- data/VERSION +1 -0
- data/lib/ghaki/meta/builder.rb +63 -0
- data/lib/ghaki/meta/dict/base.rb +29 -0
- data/lib/ghaki/meta/dict/lookup.rb +49 -0
- data/lib/ghaki/meta/dict/storage.rb +67 -0
- data/spec/ghaki/meta/builder_spec.rb +222 -0
- data/spec/ghaki/meta/dict/base_spec.rb +62 -0
- data/spec/ghaki/meta/dict/dict_helper.rb +13 -0
- data/spec/ghaki/meta/dict/lookup_spec.rb +94 -0
- data/spec/ghaki/meta/dict/storage_spec.rb +143 -0
- data/spec/spec_helper.rb +7 -0
- metadata +97 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Gerald Kalafut
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= Ghaki Meta - Meta programming helpers
|
2
|
+
|
3
|
+
Ghaki Meta is a collection of meta programming helpers.
|
4
|
+
|
5
|
+
== Download
|
6
|
+
|
7
|
+
The latest version of Ghaki Meta can be found at
|
8
|
+
|
9
|
+
* git@github.com:ghaki/ghaki-meta.git
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
The preferred method of installing Ghaki Meta is through its GEM file.
|
14
|
+
|
15
|
+
% [sudo] gem install ghaki-meta-1.0.0.gem
|
16
|
+
|
17
|
+
== License
|
18
|
+
|
19
|
+
Ghaki Meta is released under the MIT license.
|
20
|
+
|
21
|
+
== Support
|
22
|
+
|
23
|
+
Contact mailto:gerald@kalafut.org
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2011.11.29.1
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Ghaki #:nodoc:
|
2
|
+
module Meta #:nodoc:
|
3
|
+
|
4
|
+
# Allow meta classes to be specified for later creation.
|
5
|
+
module Builder
|
6
|
+
|
7
|
+
# Generate meta objects using builders.
|
8
|
+
def setup_meta_builders *args
|
9
|
+
self.class.meta_builders.each_key do |token|
|
10
|
+
make_meta_builder token, *args
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Make meta object using builder.
|
15
|
+
def make_meta_builder token, *args
|
16
|
+
klass = self.class.meta_builders[token]
|
17
|
+
raise NotImplementedError, "Meta Builder Not Set: #{token}" if klass.nil?
|
18
|
+
obj = if klass.respond_to?(:new)
|
19
|
+
klass.new( *args )
|
20
|
+
elsif klass.respond_to?(:create)
|
21
|
+
klass.create( *args )
|
22
|
+
else
|
23
|
+
raise NotImplementedError, "Unknown Builder For Class: #{klass}"
|
24
|
+
end
|
25
|
+
self.send( (token.to_s + '=').to_sym, obj )
|
26
|
+
end
|
27
|
+
|
28
|
+
######################################################################
|
29
|
+
module ClassMethods
|
30
|
+
|
31
|
+
# Getter for the meta builder helper object.
|
32
|
+
def meta_builders
|
33
|
+
@meta_builders ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
# Declare meta builder attributes.
|
37
|
+
def attr_meta_builder *tokens
|
38
|
+
@meta_builders ||= {}
|
39
|
+
tokens.each do |token|
|
40
|
+
@meta_builders[token] = nil
|
41
|
+
module_eval <<-"END"
|
42
|
+
def #{token} ; @#{token} end
|
43
|
+
def #{token}= val ; @#{token} = val end
|
44
|
+
END
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Assign classes to meta builder attributes.
|
49
|
+
def set_meta_builder pairs
|
50
|
+
pairs.each_pair do |token,klass|
|
51
|
+
attr_meta_builder(token) unless meta_builders.has_key?(token)
|
52
|
+
self.meta_builders[token] = klass
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.included klass #:nodoc:
|
59
|
+
klass.extend ClassMethods
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'ghaki/meta/dict/storage'
|
2
|
+
require 'ghaki/meta/dict/lookup'
|
3
|
+
|
4
|
+
module Ghaki #:nodoc:
|
5
|
+
module Meta #:nodoc:
|
6
|
+
module Dict #:nodoc:
|
7
|
+
|
8
|
+
class Base
|
9
|
+
|
10
|
+
def self.dict_lookup_rule *words
|
11
|
+
list = []
|
12
|
+
words.each do |word|
|
13
|
+
list.push <<-"END"
|
14
|
+
def #{word}
|
15
|
+
@lookups[ :#{word} ] ||=
|
16
|
+
Lookup.new( :#{word}, :dict_storage => @storage )
|
17
|
+
end
|
18
|
+
END
|
19
|
+
end
|
20
|
+
class_eval list.join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
@storage = Storage.new
|
25
|
+
@lookups = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end end end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'ghaki/meta/dict/storage'
|
2
|
+
|
3
|
+
module Ghaki #:nodoc:
|
4
|
+
module Meta #:nodoc:
|
5
|
+
module Dict #:nodoc:
|
6
|
+
|
7
|
+
class Lookup
|
8
|
+
|
9
|
+
attr_accessor :token, :storage
|
10
|
+
|
11
|
+
def initialize name, opts={}
|
12
|
+
@token = name
|
13
|
+
@storage = opts[:dict_storage] || Storage.new
|
14
|
+
end
|
15
|
+
|
16
|
+
%w{ snake camel title }.each do |rule|
|
17
|
+
list = []
|
18
|
+
list.push <<-"END"
|
19
|
+
|
20
|
+
def #{rule}
|
21
|
+
@storage.get_#{rule}( @token )
|
22
|
+
end
|
23
|
+
|
24
|
+
def #{rule}= val
|
25
|
+
@storage.put_#{rule}( @token, val )
|
26
|
+
end
|
27
|
+
|
28
|
+
END
|
29
|
+
class_eval list.join("\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
@storage.get_snake(@token)
|
34
|
+
end
|
35
|
+
|
36
|
+
def opt_plural opts, get
|
37
|
+
@storage.opt_plural opts, @token, get
|
38
|
+
end
|
39
|
+
|
40
|
+
def opt_alias opts, get
|
41
|
+
@storage.opt_alias opts, @token, get
|
42
|
+
end
|
43
|
+
|
44
|
+
def opt_set opts, value
|
45
|
+
@storage.opt_set opts, @token, value
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end end end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Ghaki #:nodoc:
|
2
|
+
module Meta #:nodoc:
|
3
|
+
module Dict #:nodoc:
|
4
|
+
|
5
|
+
class Storage
|
6
|
+
|
7
|
+
attr_accessor :snake, :title, :camel
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@snake = {}
|
11
|
+
@title = {}
|
12
|
+
@camel = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
%w{ snake title camel }.each do |rule|
|
16
|
+
class_eval <<-"END"
|
17
|
+
|
18
|
+
def put_#{rule} token, value
|
19
|
+
@#{rule}[token] = value
|
20
|
+
end
|
21
|
+
|
22
|
+
END
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_snake token
|
26
|
+
if @snake[token].nil?
|
27
|
+
raise ArgumentError, "Unknown Token: #{token}"
|
28
|
+
end
|
29
|
+
@snake[token]
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_title token
|
33
|
+
if @title[token].nil?
|
34
|
+
put_title token, snake_to_title( get_snake(token) )
|
35
|
+
end
|
36
|
+
@title[token]
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_camel token
|
40
|
+
if @camel[token].nil?
|
41
|
+
put_camel token, title_to_camel( get_title(token) )
|
42
|
+
end
|
43
|
+
@camel[token]
|
44
|
+
end
|
45
|
+
|
46
|
+
def opt_set opts, put, value
|
47
|
+
put_snake put, (opts[put] || value)
|
48
|
+
end
|
49
|
+
def opt_plural opts, put, get
|
50
|
+
put_snake put, (opts[put] || get_snake(get) + 's')
|
51
|
+
end
|
52
|
+
def opt_alias opts, put, get
|
53
|
+
put_snake put, (opts[put] || get_snake(get))
|
54
|
+
end
|
55
|
+
|
56
|
+
def snake_to_title text
|
57
|
+
text.split('_').map do |x| x.capitalize end.join(' ')
|
58
|
+
end
|
59
|
+
def title_to_camel text
|
60
|
+
text.gsub(' ','')
|
61
|
+
end
|
62
|
+
def snake_to_camel text
|
63
|
+
title_to_camel snake_to_title(text)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end end end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'ghaki/meta/builder'
|
2
|
+
|
3
|
+
module Ghaki module Meta module Builder_Testing
|
4
|
+
|
5
|
+
# SUPPORT OBJECTS
|
6
|
+
class UsingImport
|
7
|
+
include Ghaki::Meta::Builder
|
8
|
+
end
|
9
|
+
class UsingAttr < UsingImport
|
10
|
+
attr_meta_builder :child
|
11
|
+
end
|
12
|
+
|
13
|
+
class Helper
|
14
|
+
attr_accessor :word
|
15
|
+
def initialize word
|
16
|
+
@word = word
|
17
|
+
end
|
18
|
+
end
|
19
|
+
class Child < Helper; end
|
20
|
+
class Parent < Helper; end
|
21
|
+
|
22
|
+
class UsingSetup < UsingAttr
|
23
|
+
set_meta_builder :child => Child, :parent => Parent
|
24
|
+
def initialize
|
25
|
+
setup_meta_builders 'together'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
class UsingMake < UsingAttr
|
29
|
+
set_meta_builder :child => Child, :parent => Parent
|
30
|
+
def initialize
|
31
|
+
make_meta_builder :child, 'first'
|
32
|
+
make_meta_builder :parent, 'second'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Worm < Helper ; end
|
37
|
+
class UsingWorm < Helper
|
38
|
+
attr_accessor :dinner
|
39
|
+
end
|
40
|
+
class Bird < UsingWorm; end
|
41
|
+
class Fish < UsingWorm; end
|
42
|
+
class Vole < UsingWorm; end
|
43
|
+
|
44
|
+
class ChainA < UsingImport
|
45
|
+
attr_meta_builder :worm
|
46
|
+
def initialize
|
47
|
+
setup_meta_builders 'together'
|
48
|
+
setup_dinner
|
49
|
+
end
|
50
|
+
def setup_dinner
|
51
|
+
end
|
52
|
+
end
|
53
|
+
class ChainB < ChainA
|
54
|
+
attr_meta_builder :fish, :bird
|
55
|
+
def setup_dinner; super
|
56
|
+
@fish.dinner = @bird.dinner = @worm
|
57
|
+
end
|
58
|
+
end
|
59
|
+
class ChainC < ChainB
|
60
|
+
attr_meta_builder :vole
|
61
|
+
def setup_dinner; super
|
62
|
+
@vole.dinner = @worm
|
63
|
+
end
|
64
|
+
end
|
65
|
+
class ChainD < ChainC
|
66
|
+
set_meta_builder \
|
67
|
+
:worm => Worm,
|
68
|
+
:bird => Bird, :fish => Fish,
|
69
|
+
:vole => Vole
|
70
|
+
end
|
71
|
+
|
72
|
+
class Toy ; end
|
73
|
+
class ToyFactory
|
74
|
+
private_class_method :new
|
75
|
+
def self.create
|
76
|
+
Toy.new
|
77
|
+
end
|
78
|
+
end
|
79
|
+
class UsingFactory < UsingImport
|
80
|
+
set_meta_builder :toy => ToyFactory
|
81
|
+
def initialize
|
82
|
+
setup_meta_builders
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# ACTUAL TESTING
|
87
|
+
describe Ghaki::Meta::Builder do
|
88
|
+
|
89
|
+
describe 'when importing' do
|
90
|
+
context 'meta' do
|
91
|
+
subject { UsingImport }
|
92
|
+
it { should respond_to :attr_meta_builder }
|
93
|
+
it { should respond_to :set_meta_builder }
|
94
|
+
it { should respond_to :meta_builders }
|
95
|
+
end
|
96
|
+
context 'object' do
|
97
|
+
subject { UsingImport.new }
|
98
|
+
it { should respond_to :setup_meta_builders }
|
99
|
+
it { should respond_to :make_meta_builder }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'when calling #attr_meta_builder for child helper' do
|
104
|
+
context '#meta_builders' do
|
105
|
+
subject { UsingAttr }
|
106
|
+
it 'should have placeholder' do
|
107
|
+
subject.meta_builders.has_key?(:child).should be_true
|
108
|
+
subject.meta_builders[:child].should be_nil
|
109
|
+
end
|
110
|
+
end
|
111
|
+
context 'object' do
|
112
|
+
subject { UsingAttr.new }
|
113
|
+
it { should respond_to :child }
|
114
|
+
it { should respond_to :child= }
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'when calling #set_meta_builder for parent helper' do
|
119
|
+
context '#meta_builders' do
|
120
|
+
subject { UsingSetup }
|
121
|
+
it 'should have class for parent helper' do
|
122
|
+
subject.meta_builders[:child].should == Child
|
123
|
+
end
|
124
|
+
it 'should have class for child helper' do
|
125
|
+
subject.meta_builders[:parent].should == Parent
|
126
|
+
end
|
127
|
+
end
|
128
|
+
context 'object' do
|
129
|
+
subject { UsingSetup.new }
|
130
|
+
it { should respond_to :parent }
|
131
|
+
it { should respond_to :parent= }
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
shared_examples_for 'class creations' do
|
136
|
+
it 'should create parent helper' do
|
137
|
+
subject.child.should be_an_instance_of(Child)
|
138
|
+
end
|
139
|
+
it 'should create child helper' do
|
140
|
+
subject.parent.should be_an_instance_of(Parent)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe 'when using #make_meta_builder' do
|
145
|
+
subject { UsingMake.new }
|
146
|
+
it_should_behave_like 'class creations'
|
147
|
+
it 'should set specific args for parent helper' do
|
148
|
+
subject.child.word.should == 'first'
|
149
|
+
end
|
150
|
+
it 'should set specific args for child helper' do
|
151
|
+
subject.parent.word.should == 'second'
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe 'when using #setup_meta_builders' do
|
156
|
+
subject { UsingSetup.new }
|
157
|
+
it_should_behave_like 'class creations'
|
158
|
+
it 'should set common args for parent helper' do
|
159
|
+
subject.child.word.should == 'together'
|
160
|
+
end
|
161
|
+
it 'should set common args for child helper' do
|
162
|
+
subject.parent.word.should == 'together'
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe 'when using factory class' do
|
167
|
+
subject { UsingFactory.new }
|
168
|
+
it 'should create object' do
|
169
|
+
subject.toy.should_not be_nil
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe 'when using chained inheritence' do
|
174
|
+
|
175
|
+
context '#meta_builders' do
|
176
|
+
subject { ChainD }
|
177
|
+
it 'should have full heirarchy of classes' do
|
178
|
+
subject.meta_builders.length.should == 4
|
179
|
+
end
|
180
|
+
it 'should set up worm' do
|
181
|
+
subject.meta_builders[:worm].should == Worm
|
182
|
+
end
|
183
|
+
it 'should set up bird' do
|
184
|
+
subject.meta_builders[:bird].should == Bird
|
185
|
+
end
|
186
|
+
it 'should set up fish' do
|
187
|
+
subject.meta_builders[:fish].should == Fish
|
188
|
+
end
|
189
|
+
it 'should set up vole' do
|
190
|
+
subject.meta_builders[:vole].should == Vole
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'object' do
|
195
|
+
subject { ChainD.new }
|
196
|
+
it 'should have worm' do
|
197
|
+
subject.worm.class == Worm
|
198
|
+
end
|
199
|
+
it 'should have bird' do
|
200
|
+
subject.bird.class == Bird
|
201
|
+
end
|
202
|
+
it 'should have fish' do
|
203
|
+
subject.fish.class == Fish
|
204
|
+
end
|
205
|
+
it 'should have vole' do
|
206
|
+
subject.vole.class == Vole
|
207
|
+
end
|
208
|
+
it 'should set dinner for bird' do
|
209
|
+
subject.bird.dinner.should == subject.worm
|
210
|
+
end
|
211
|
+
it 'should set dinner for fish' do
|
212
|
+
subject.fish.dinner.should == subject.worm
|
213
|
+
end
|
214
|
+
it 'should set dinner for vole' do
|
215
|
+
subject.vole.dinner.should == subject.worm
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
222
|
+
end end end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'ghaki/meta/dict/base'
|
2
|
+
require File.join( File.dirname(__FILE__), 'dict_helper' )
|
3
|
+
|
4
|
+
module Ghaki module Meta module Dict module Base_Test
|
5
|
+
describe Base do
|
6
|
+
include DictHelper
|
7
|
+
|
8
|
+
class Mine < Dict::Base
|
9
|
+
dict_lookup_rule :quack, :quacks
|
10
|
+
end
|
11
|
+
|
12
|
+
before(:all) do
|
13
|
+
setup_dict_names
|
14
|
+
end
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
@dict = Mine.new
|
18
|
+
end
|
19
|
+
subject { @dict }
|
20
|
+
|
21
|
+
it 'makes dict rules' do
|
22
|
+
should respond_to :quack
|
23
|
+
should respond_to :quacks
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'allows get and set' do
|
27
|
+
subject.quack.snake = @snake
|
28
|
+
subject.quack.to_s.should == @snake
|
29
|
+
subject.quack.title.should == @title
|
30
|
+
subject.quack.snake.should == @snake
|
31
|
+
subject.quack.camel.should == @camel
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'using :opt_set' do
|
35
|
+
subject { @dict.quack }
|
36
|
+
it 'accepts option' do
|
37
|
+
@dict.quack.opt_set( { @token => @snake }, 'ignore' )
|
38
|
+
subject.snake.should == @snake
|
39
|
+
end
|
40
|
+
it 'defaults value' do
|
41
|
+
@dict.quack.opt_set( {:ignore => 'bogus'}, @snake )
|
42
|
+
subject.snake.should == @snake
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'using :opt_plural' do
|
47
|
+
before(:each) do
|
48
|
+
@dict.quack.snake = @snake
|
49
|
+
end
|
50
|
+
subject { @dict.quacks }
|
51
|
+
it 'accepts option' do
|
52
|
+
subject.opt_plural( { @tokens => @snakes}, :ignore )
|
53
|
+
subject.snake.should == @snakes
|
54
|
+
end
|
55
|
+
it 'defaults value' do
|
56
|
+
subject.opt_plural( { :ignore => 'bogus' }, @token )
|
57
|
+
subject.snake.should == @snakes
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end end end end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'ghaki/meta/dict/lookup'
|
2
|
+
|
3
|
+
require File.join( File.dirname(__FILE__), 'dict_helper' )
|
4
|
+
|
5
|
+
module Ghaki module Meta module Dict module Lookup_Test
|
6
|
+
describe Lookup do
|
7
|
+
include DictHelper
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
setup_dict_names
|
11
|
+
end
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
@lookup = Lookup.new @token
|
15
|
+
end
|
16
|
+
|
17
|
+
subject { @lookup }
|
18
|
+
|
19
|
+
describe '#initialize' do
|
20
|
+
it 'accepts option :dict_storage' do
|
21
|
+
@storage = Storage.new
|
22
|
+
@lookup = Lookup.new( @token, :dict_storage => @storage )
|
23
|
+
subject.storage.should == @storage
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#snake' do
|
28
|
+
it 'get snake value' do
|
29
|
+
subject.storage.put_snake @token, @snake
|
30
|
+
subject.snake.should == @snake
|
31
|
+
end
|
32
|
+
end
|
33
|
+
describe '#snake=' do
|
34
|
+
it 'put snake value' do
|
35
|
+
subject.snake = @snake
|
36
|
+
subject.storage.get_snake(@token).should == @snake
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#camel' do
|
41
|
+
it 'get camel value' do
|
42
|
+
subject.storage.put_camel @token, @camel
|
43
|
+
subject.camel.should == @camel
|
44
|
+
end
|
45
|
+
end
|
46
|
+
describe '#camel=' do
|
47
|
+
it 'put camel value' do
|
48
|
+
subject.camel = @camel
|
49
|
+
subject.storage.get_camel(@token).should == @camel
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#title' do
|
54
|
+
it 'get title value' do
|
55
|
+
subject.storage.put_title @token, @title
|
56
|
+
subject.title.should == @title
|
57
|
+
end
|
58
|
+
end
|
59
|
+
describe '#title=' do
|
60
|
+
it 'put title value' do
|
61
|
+
subject.title = @title
|
62
|
+
subject.storage.get_title(@token).should == @title
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#opt_set' do
|
67
|
+
it 'delegates option setting' do
|
68
|
+
@opts = { :bogus => 'fake' }
|
69
|
+
subject.storage.expects(:opt_set).
|
70
|
+
with(@opts,@token,@snake).once
|
71
|
+
subject.opt_set @opts, @snake
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#opt_plural' do
|
76
|
+
it 'delegates option setting' do
|
77
|
+
@opts = { :bogus => 'fake' }
|
78
|
+
subject.storage.expects(:opt_plural).
|
79
|
+
with(@opts,@token,@tokens).once
|
80
|
+
subject.opt_plural @opts, @tokens
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#opt_alias' do
|
85
|
+
it 'delegates option setting' do
|
86
|
+
@opts = { :bogus => 'fake' }
|
87
|
+
subject.storage.expects(:opt_alias).
|
88
|
+
with(@opts,@token,@aka).once
|
89
|
+
subject.opt_alias @opts, @aka
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end end end end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'ghaki/meta/dict/storage'
|
2
|
+
require File.join( File.dirname(__FILE__), 'dict_helper' )
|
3
|
+
|
4
|
+
module Ghaki module Meta module Dict module Storage_Test
|
5
|
+
describe Storage do
|
6
|
+
include DictHelper
|
7
|
+
|
8
|
+
before(:all) do
|
9
|
+
setup_dict_names
|
10
|
+
end
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
@store = Storage.new
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { @store }
|
17
|
+
|
18
|
+
describe '#initialize' do
|
19
|
+
it 'makes empty snake' do
|
20
|
+
subject.snake.should be_empty
|
21
|
+
end
|
22
|
+
it 'makes empty camel' do
|
23
|
+
subject.camel.should be_empty
|
24
|
+
end
|
25
|
+
it 'makes empty title' do
|
26
|
+
subject.title.should be_empty
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it { should respond_to :snake }
|
31
|
+
describe '#put_snake' do
|
32
|
+
it 'stores value' do
|
33
|
+
subject.put_snake @token, @snake
|
34
|
+
subject.snake.has_key?(@token).should be_true
|
35
|
+
subject.snake[@token].should == @snake
|
36
|
+
end
|
37
|
+
end
|
38
|
+
describe '#get_snake' do
|
39
|
+
it 'returns value when present' do
|
40
|
+
subject.snake[@token] = @snake
|
41
|
+
subject.get_snake(@token).should == @snake
|
42
|
+
end
|
43
|
+
it 'fails when missing' do
|
44
|
+
lambda do
|
45
|
+
subject.get_snake(@token)
|
46
|
+
end.should raise_error(ArgumentError,"Unknown Token: #{@token}")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it { should respond_to :title }
|
51
|
+
describe '#put_title' do
|
52
|
+
it 'stores value' do
|
53
|
+
subject.put_title @token, @title
|
54
|
+
subject.title.has_key?(@token).should be_true
|
55
|
+
subject.title[@token].should == @title
|
56
|
+
end
|
57
|
+
end
|
58
|
+
describe '#get_title' do
|
59
|
+
it 'returns value when present' do
|
60
|
+
subject.title[@token] = @title
|
61
|
+
subject.get_title(@token).should == @title
|
62
|
+
end
|
63
|
+
it 'defaults when missing' do
|
64
|
+
subject.snake[@token] = @snake
|
65
|
+
subject.get_title(@token).should == @title
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it { should respond_to :camel }
|
70
|
+
describe '#put_camel' do
|
71
|
+
it 'stores value' do
|
72
|
+
subject.put_camel @token, @camel
|
73
|
+
subject.camel.has_key?(@token).should be_true
|
74
|
+
subject.camel[@token].should == @camel
|
75
|
+
end
|
76
|
+
end
|
77
|
+
describe '#get_camel' do
|
78
|
+
it 'returns value when present' do
|
79
|
+
subject.camel[@token] = @camel
|
80
|
+
subject.get_camel(@token).should == @camel
|
81
|
+
end
|
82
|
+
it 'defaults when missing' do
|
83
|
+
subject.snake[@token] = @snake
|
84
|
+
subject.get_camel(@token).should == @camel
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe '#opt_set' do
|
89
|
+
it 'stores option when present' do
|
90
|
+
subject.opt_set( { @token => @snake }, @token, 'ignore' )
|
91
|
+
subject.get_snake(@token).should == @snake
|
92
|
+
end
|
93
|
+
it 'stores default when missing' do
|
94
|
+
subject.opt_set( {}, @token, @snake )
|
95
|
+
subject.get_snake(@token).should == @snake
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#opt_plural' do
|
100
|
+
it 'stores option when present' do
|
101
|
+
subject.opt_plural( { @tokens => @snakes }, @tokens, :ignore )
|
102
|
+
subject.get_snake(@tokens).should == @snakes
|
103
|
+
end
|
104
|
+
it 'stores default when missing' do
|
105
|
+
subject.put_snake @token, @snake
|
106
|
+
subject.opt_plural( {}, @tokens, @token )
|
107
|
+
subject.get_snake(@tokens).should == @snakes
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#opt_alias' do
|
112
|
+
it 'stores option when present' do
|
113
|
+
subject.put_snake @token, 'ignored'
|
114
|
+
subject.opt_alias( { @aka => @snake }, @aka, @token )
|
115
|
+
subject.get_snake(@aka).should == @snake
|
116
|
+
end
|
117
|
+
it 'stores default when missing' do
|
118
|
+
subject.put_snake @token, @snake
|
119
|
+
subject.opt_alias( {}, @aka, @token )
|
120
|
+
subject.get_snake(@aka).should == @snake
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe '#snake_to_title' do
|
125
|
+
it 'translates snake case into title case' do
|
126
|
+
subject.snake_to_title(@snake).should == @title
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '#title_to_camel' do
|
131
|
+
it 'translates title case into camel case' do
|
132
|
+
subject.title_to_camel(@title).should == @camel
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe '#snake_to_camel' do
|
137
|
+
it 'translates snake case into camel case' do
|
138
|
+
subject.snake_to_camel(@snake).should == @camel
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
end end end end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ghaki-meta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2011.11.29.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gerald Kalafut
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-30 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70995750 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.4.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70995750
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
requirement: &70995520 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.9.12
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70995520
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rdoc
|
38
|
+
requirement: &71251820 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.9.4
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *71251820
|
47
|
+
description: Collection of meta programming helpers.
|
48
|
+
email: gerald@kalafut.org
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README
|
53
|
+
files:
|
54
|
+
- lib/ghaki/meta/builder.rb
|
55
|
+
- lib/ghaki/meta/dict/storage.rb
|
56
|
+
- lib/ghaki/meta/dict/lookup.rb
|
57
|
+
- lib/ghaki/meta/dict/base.rb
|
58
|
+
- README
|
59
|
+
- LICENSE
|
60
|
+
- VERSION
|
61
|
+
- spec/ghaki/meta/builder_spec.rb
|
62
|
+
- spec/ghaki/meta/dict/storage_spec.rb
|
63
|
+
- spec/ghaki/meta/dict/base_spec.rb
|
64
|
+
- spec/ghaki/meta/dict/lookup_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
- spec/ghaki/meta/dict/dict_helper.rb
|
67
|
+
homepage: http://github.com/ghaki
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 1.3.6
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project: ghaki-meta
|
87
|
+
rubygems_version: 1.8.10
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Meta programming helpers
|
91
|
+
test_files:
|
92
|
+
- spec/ghaki/meta/builder_spec.rb
|
93
|
+
- spec/ghaki/meta/dict/storage_spec.rb
|
94
|
+
- spec/ghaki/meta/dict/base_spec.rb
|
95
|
+
- spec/ghaki/meta/dict/lookup_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
- spec/ghaki/meta/dict/dict_helper.rb
|