lafcadio 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,56 +0,0 @@
1
- require 'lafcadio/objectField'
2
-
3
- module Lafcadio
4
- class CreateTableStatement #:nodoc:
5
- @@simple_field_clauses = {
6
- FloatField => 'float', DateField => 'date', BooleanField => 'bool',
7
- TimeStampField => 'timestamp', TimeField => 'datetime'
8
- }
9
-
10
- def initialize( domain_class )
11
- @domain_class = domain_class
12
- end
13
-
14
- def definition_terms( field )
15
- definitionTerms = []
16
- definitionTerms << field.db_field_name
17
- definitionTerms << type_clause( field )
18
- definitionTerms << 'not null' if field.not_null
19
- definitionTerms.join( ' ' )
20
- end
21
-
22
- def to_sql
23
- createDefinitions = []
24
- createDefinitions << "#{ @domain_class.sql_primary_key_name } " +
25
- "int not null auto_increment"
26
- createDefinitions << "primary key (#{ @domain_class.sql_primary_key_name })"
27
- @domain_class.class_fields.each { |field|
28
- createDefinitions << definition_terms( field )
29
- }
30
- <<-SQL
31
- create table #{ @domain_class.table_name } (
32
- #{ createDefinitions.join(",\n ") }
33
- );
34
- SQL
35
- end
36
-
37
- def type_clause( field )
38
- if ( type_clause = @@simple_field_clauses[field.class] )
39
- type_clause
40
- elsif ( field.class <= EnumField )
41
- singleQuotedValues = field.enums.keys.collect! { |enumValue|
42
- "'#{ enumValue }'"
43
- }
44
- "enum( #{ singleQuotedValues.join( ', ' ) } )"
45
- elsif ( field.class <= StringField || field.class <= TextListField )
46
- 'varchar(255)'
47
- elsif ( field.class <= DomainObjectField || field.class <= IntegerField )
48
- 'int'
49
- elsif ( field.class <= FloatField )
50
- 'float(10, 2)'
51
- elsif ( field.class <= BlobField )
52
- 'blob'
53
- end
54
- end
55
- end
56
- end
@@ -1,25 +0,0 @@
1
- require 'lafcadio/depend'
2
- require 'lafcadio/mock'
3
- require 'lafcadio/util'
4
- require 'test/unit'
5
-
6
- # A test case that sets up a number of mock services. In writing an application
7
- # that uses Lafcadio you may find it convenient to inherit from this class.
8
- class LafcadioTestCase < Test::Unit::TestCase
9
- include Lafcadio
10
-
11
- def setup
12
- context = Context.instance
13
- context.flush
14
- @mockObjectStore = MockObjectStore.new
15
- ObjectStore.set_object_store @mockObjectStore
16
- LafcadioConfig.set_values(
17
- 'classDefinitionDir' => '../test/testData', 'dbhost' => 'localhost',
18
- 'dbname' => 'test', 'dbpassword' => 'password', 'dbuser' => 'test',
19
- 'domainFiles' => %w( ../test/mock/domain ),
20
- 'logdir' => '../test/testOutput/', 'logSql' => 'n'
21
- )
22
- end
23
-
24
- def default_test; end
25
- end
@@ -1,13 +0,0 @@
1
- dbuser:test
2
- dbpassword:password
3
- dbname:test
4
- dbhost:localhost
5
- adminEmail:john.doe@email.com
6
- siteName:Site Name
7
- url:http://test.url
8
- classpath:lafcadio/
9
- logdir:../test/testOutput/
10
- domainDirs:../test/mock/domain/
11
- domainFiles:../test/mock/domain.rb
12
- logSql:n
13
- classDefinitionDir:../test/testData
@@ -1,196 +0,0 @@
1
- require 'delegate'
2
- require 'lafcadio/depend'
3
- require 'singleton'
4
-
5
- module Lafcadio
6
- # The Context is a singleton object that manages ContextualServices. Each
7
- # ContextualService is a service that connects in some way to external
8
- # resources: ObjectStore connects to the database; Emailer connects to SMTP,
9
- # etc.
10
- #
11
- # Context makes it easy to ensure that each ContextualService is only
12
- # instantiated once, which can be quite useful for services with expensive
13
- # creation.
14
- #
15
- # Furthermore, Context allows you to explicitly set instances for a given
16
- # service, which can be quite useful in testing. For example, once
17
- # LafcadioTestCase#setup has an instance of MockObjectStore, it calls
18
- # context.setObjectStore @mockObjectStore
19
- # which ensures that any future calls to ObjectStore.getObjectStore will
20
- # return @mockObjectStore, instead of an instance of ObjectStore connecting
21
- # test code to a live database.
22
- class Context
23
- include Singleton
24
-
25
- def initialize
26
- flush
27
- @init_procs = {}
28
- end
29
-
30
- def create_instance( service_class, *init_args ) #:nodoc:
31
- if ( proc = @init_procs[service_class] )
32
- proc.call( *init_args )
33
- else
34
- service_class.new( *init_args )
35
- end
36
- end
37
-
38
- # Flushes all cached ContextualServices.
39
- def flush
40
- @resources_by_class = Hash.new { |hash, key| hash[key] = {} }
41
- end
42
-
43
- def get_resource( service_class, *init_args ) #:nodoc:
44
- resource = @resources_by_class[service_class][init_args]
45
- unless resource
46
- resource = create_instance( service_class, *init_args )
47
- set_resource( service_class, resource, *init_args )
48
- end
49
- resource
50
- end
51
-
52
- def set_init_proc( service_class, proc )
53
- @init_procs[service_class] = proc
54
- end
55
-
56
- def set_resource( service_class, resource, *init_args ) #:nodoc:
57
- @resources_by_class[service_class][init_args] = resource
58
- end
59
- end
60
-
61
- # A ContextualService is a service that is managed by the Context.
62
- # ContextualServices are not instantiated normally. Instead, the instance of
63
- # such a service may be retrieved by calling the method
64
- # < class name >.get< class name >
65
- #
66
- # For example: ObjectStore.getObjectStore
67
- class ContextualService
68
- def self.flush; Context.instance.set_resource( self, nil ); end
69
-
70
- def self.method_missing( symbol, *args )
71
- method_name = symbol.id2name
72
- target = nil
73
- if method_name =~ /^get_(.*)/
74
- target = :get_resource if $1.underscore_to_camel_case == basename
75
- elsif method_name =~ /^set_(.*)/
76
- target = :set_resource if $1.underscore_to_camel_case == basename
77
- end
78
- if target
79
- Context.instance.send( target, self, *args )
80
- else
81
- super
82
- end
83
- end
84
-
85
- def self.set_init_proc
86
- proc = proc { yield }
87
- Context.instance.set_init_proc( self, proc )
88
- end
89
-
90
- # ContextualServices can only be initialized through the Context instance.
91
- # Note that if you're writing your own initialize method in a child class,
92
- # you should make sure to call super() or you'll overwrite this behavior.
93
- def initialize
94
- regexp = %r{lafcadio/util\.rb.*create_instance}
95
- unless caller.any? { |line| line =~ regexp }
96
- raise ArgumentError,
97
- "#{ self.class.name.to_s } should be instantiated by calling " +
98
- self.class.name.to_s + ".get_" + self.class.name.camel_case_to_underscore,
99
- caller
100
- end
101
- end
102
- end
103
-
104
- # LafcadioConfig is a Hash that takes its data from the config file. You'll
105
- # have to set the location of that file before using it: Use
106
- # LafcadioConfig.set_filename.
107
- #
108
- # LafcadioConfig expects its data to be colon-delimited, one key-value pair
109
- # to a line. For example:
110
- # dbuser:user
111
- # dbpassword:password
112
- # dbname:lafcadio_test
113
- # dbhost:localhost
114
- class LafcadioConfig < Hash
115
- @@value_hash = nil
116
-
117
- def self.set_filename(filename); @@filename = filename; end
118
-
119
- def self.set_values( value_hash ); @@value_hash = value_hash; end
120
-
121
- def initialize
122
- if @@value_hash
123
- @@value_hash.each { |key, value| self[key] = value }
124
- else
125
- File.new( @@filename ).each_line { |line|
126
- line.chomp =~ /^(.*?):(.*)$/
127
- self[$1] = $2
128
- }
129
- end
130
- end
131
- end
132
-
133
- class MissingError < RuntimeError
134
- end
135
- end
136
-
137
- class String
138
- unless method_defined?( :camel_case_to_underscore )
139
-
140
- # Returns the underscored version of a camel-case string.
141
- def camel_case_to_underscore
142
- ( gsub( /(.)([A-Z])/ ) { $1 + '_' + $2.downcase } ).downcase
143
- end
144
-
145
- end
146
-
147
- # Returns the number of times that <tt>regexp</tt> occurs in the string.
148
- def count_occurrences(regexp)
149
- count = 0
150
- str = self.clone
151
- while str =~ regexp
152
- count += 1
153
- str = $'
154
- end
155
- count
156
- end
157
-
158
- # Decapitalizes the first letter of the string, or decapitalizes the
159
- # entire string if it's all capitals.
160
- #
161
- # 'InternalClient'.decapitalize -> "internalClient"
162
- # 'SKU'.decapitalize -> "sku"
163
- def decapitalize
164
- string = clone
165
- firstLetter = string[0..0].downcase
166
- string = firstLetter + string[1..string.length]
167
- newString = ""
168
- while string =~ /([A-Z])([^a-z]|$)/
169
- newString += $`
170
- newString += $1.downcase
171
- string = $2 + $'
172
- end
173
- newString += string
174
- newString
175
- end
176
-
177
- # Left-pads a string with +fillChar+ up to +size+ size.
178
- #
179
- # "a".pad( 10, "+") -> "+++++++++a"
180
- def pad(size, fillChar)
181
- string = clone
182
- while string.length < size
183
- string = fillChar + string
184
- end
185
- string
186
- end
187
-
188
- unless method_defined?( :underscore_to_camel_case )
189
-
190
- # Returns the camel-case equivalent of an underscore-style string.
191
- def underscore_to_camel_case
192
- capitalize.gsub( /_([a-zA-Z0-9]+)/ ) { |s| s[1,s.size - 1].capitalize }
193
- end
194
-
195
- end
196
- end