datamapper 0.3.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/Manifest.txt +5 -0
- data/README.txt +11 -0
- data/Rakefile +70 -0
- data/lib/datamapper.rb +8 -0
- metadata +152 -319
- data/CHANGELOG +0 -145
- data/FAQ +0 -96
- data/MIT-LICENSE +0 -22
- data/QUICKLINKS +0 -12
- data/README +0 -105
- data/environment.rb +0 -62
- data/example.rb +0 -156
- data/lib/data_mapper.rb +0 -88
- data/lib/data_mapper/adapters/abstract_adapter.rb +0 -43
- data/lib/data_mapper/adapters/data_object_adapter.rb +0 -480
- data/lib/data_mapper/adapters/mysql_adapter.rb +0 -72
- data/lib/data_mapper/adapters/postgresql_adapter.rb +0 -258
- data/lib/data_mapper/adapters/sql/coersion.rb +0 -134
- data/lib/data_mapper/adapters/sql/commands/load_command.rb +0 -545
- data/lib/data_mapper/adapters/sql/mappings/associations_set.rb +0 -34
- data/lib/data_mapper/adapters/sql/mappings/column.rb +0 -279
- data/lib/data_mapper/adapters/sql/mappings/conditions.rb +0 -172
- data/lib/data_mapper/adapters/sql/mappings/schema.rb +0 -60
- data/lib/data_mapper/adapters/sql/mappings/table.rb +0 -459
- data/lib/data_mapper/adapters/sql/quoting.rb +0 -24
- data/lib/data_mapper/adapters/sqlite3_adapter.rb +0 -159
- data/lib/data_mapper/associations.rb +0 -106
- data/lib/data_mapper/associations/belongs_to_association.rb +0 -160
- data/lib/data_mapper/associations/has_and_belongs_to_many_association.rb +0 -437
- data/lib/data_mapper/associations/has_many_association.rb +0 -283
- data/lib/data_mapper/associations/has_n_association.rb +0 -143
- data/lib/data_mapper/associations/reference.rb +0 -47
- data/lib/data_mapper/attributes.rb +0 -73
- data/lib/data_mapper/auto_migrations.rb +0 -36
- data/lib/data_mapper/base.rb +0 -17
- data/lib/data_mapper/callbacks.rb +0 -107
- data/lib/data_mapper/context.rb +0 -112
- data/lib/data_mapper/database.rb +0 -234
- data/lib/data_mapper/dependency_queue.rb +0 -28
- data/lib/data_mapper/embedded_value.rb +0 -145
- data/lib/data_mapper/identity_map.rb +0 -47
- data/lib/data_mapper/is/tree.rb +0 -121
- data/lib/data_mapper/migration.rb +0 -155
- data/lib/data_mapper/persistence.rb +0 -852
- data/lib/data_mapper/property.rb +0 -310
- data/lib/data_mapper/query.rb +0 -164
- data/lib/data_mapper/support/blank.rb +0 -35
- data/lib/data_mapper/support/connection_pool.rb +0 -117
- data/lib/data_mapper/support/enumerable.rb +0 -35
- data/lib/data_mapper/support/errors.rb +0 -16
- data/lib/data_mapper/support/inflector.rb +0 -265
- data/lib/data_mapper/support/object.rb +0 -54
- data/lib/data_mapper/support/serialization.rb +0 -96
- data/lib/data_mapper/support/silence.rb +0 -10
- data/lib/data_mapper/support/string.rb +0 -72
- data/lib/data_mapper/support/struct.rb +0 -7
- data/lib/data_mapper/support/symbol.rb +0 -82
- data/lib/data_mapper/support/typed_set.rb +0 -65
- data/lib/data_mapper/types/base.rb +0 -44
- data/lib/data_mapper/types/string.rb +0 -34
- data/lib/data_mapper/validatable_extensions/errors.rb +0 -12
- data/lib/data_mapper/validatable_extensions/macros.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validatable_instance_methods.rb +0 -62
- data/lib/data_mapper/validatable_extensions/validation_base.rb +0 -18
- data/lib/data_mapper/validatable_extensions/validations/formats/email.rb +0 -43
- data/lib/data_mapper/validatable_extensions/validations/validates_acceptance_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_confirmation_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_each.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_format_of.rb +0 -28
- data/lib/data_mapper/validatable_extensions/validations/validates_length_of.rb +0 -15
- data/lib/data_mapper/validatable_extensions/validations/validates_numericality_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_presence_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_true_for.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_uniqueness_of.rb +0 -40
- data/lib/data_mapper/validations.rb +0 -20
- data/lib/data_mapper/validations/number_validator.rb +0 -40
- data/lib/data_mapper/validations/string_validator.rb +0 -20
- data/lib/data_mapper/validations/validator.rb +0 -13
- data/performance.rb +0 -307
- data/plugins/can_has_sphinx/LICENSE +0 -23
- data/plugins/can_has_sphinx/README +0 -4
- data/plugins/can_has_sphinx/REVISION +0 -1
- data/plugins/can_has_sphinx/Rakefile +0 -22
- data/plugins/can_has_sphinx/init.rb +0 -1
- data/plugins/can_has_sphinx/install.rb +0 -1
- data/plugins/can_has_sphinx/lib/acts_as_sphinx.rb +0 -123
- data/plugins/can_has_sphinx/lib/sphinx.rb +0 -460
- data/plugins/can_has_sphinx/scripts/sphinx.sh +0 -47
- data/plugins/can_has_sphinx/tasks/acts_as_sphinx_tasks.rake +0 -41
- data/profile_data_mapper.rb +0 -40
- data/rakefile.rb +0 -159
- data/spec/acts_as_tree_spec.rb +0 -67
- data/spec/adapters/data_object_adapter_spec.rb +0 -31
- data/spec/associations/belongs_to_association_spec.rb +0 -98
- data/spec/associations/has_and_belongs_to_many_association_spec.rb +0 -377
- data/spec/associations/has_many_association_spec.rb +0 -337
- data/spec/attributes_spec.rb +0 -52
- data/spec/auto_migrations_spec.rb +0 -101
- data/spec/callbacks_spec.rb +0 -186
- data/spec/can_has_sphinx.rb +0 -5
- data/spec/coersion_spec.rb +0 -41
- data/spec/column_spec.rb +0 -114
- data/spec/count_command_spec.rb +0 -45
- data/spec/database_spec.rb +0 -18
- data/spec/dataobjects_spec.rb +0 -27
- data/spec/delete_command_spec.rb +0 -11
- data/spec/dependency_spec.rb +0 -29
- data/spec/embedded_value_spec.rb +0 -161
- data/spec/fixtures/animals.yaml +0 -33
- data/spec/fixtures/animals_exhibits.yaml +0 -2
- data/spec/fixtures/careers.yaml +0 -5
- data/spec/fixtures/comments.yaml +0 -1
- data/spec/fixtures/exhibits.yaml +0 -90
- data/spec/fixtures/fruit.yaml +0 -6
- data/spec/fixtures/people.yaml +0 -37
- data/spec/fixtures/posts.yaml +0 -3
- data/spec/fixtures/projects.yaml +0 -13
- data/spec/fixtures/sections.yaml +0 -5
- data/spec/fixtures/serializers.yaml +0 -6
- data/spec/fixtures/tasks.yaml +0 -6
- data/spec/fixtures/tasks_tasks.yaml +0 -2
- data/spec/fixtures/tomatoes.yaml +0 -1
- data/spec/fixtures/users.yaml +0 -1
- data/spec/fixtures/zoos.yaml +0 -24
- data/spec/is_a_tree_spec.rb +0 -149
- data/spec/legacy_spec.rb +0 -16
- data/spec/load_command_spec.rb +0 -322
- data/spec/magic_columns_spec.rb +0 -26
- data/spec/migration_spec.rb +0 -267
- data/spec/mock_adapter.rb +0 -20
- data/spec/models/animal.rb +0 -12
- data/spec/models/candidate.rb +0 -8
- data/spec/models/career.rb +0 -7
- data/spec/models/chain.rb +0 -8
- data/spec/models/comment.rb +0 -6
- data/spec/models/exhibit.rb +0 -14
- data/spec/models/fence.rb +0 -7
- data/spec/models/fruit.rb +0 -8
- data/spec/models/job.rb +0 -8
- data/spec/models/person.rb +0 -30
- data/spec/models/post.rb +0 -14
- data/spec/models/project.rb +0 -41
- data/spec/models/sales_person.rb +0 -5
- data/spec/models/section.rb +0 -8
- data/spec/models/serializer.rb +0 -5
- data/spec/models/task.rb +0 -9
- data/spec/models/tomato.rb +0 -27
- data/spec/models/user.rb +0 -12
- data/spec/models/zoo.rb +0 -13
- data/spec/natural_key_spec.rb +0 -36
- data/spec/paranoia_spec.rb +0 -38
- data/spec/persistence_spec.rb +0 -479
- data/spec/postgres_spec.rb +0 -96
- data/spec/property_spec.rb +0 -151
- data/spec/query_spec.rb +0 -77
- data/spec/save_command_spec.rb +0 -94
- data/spec/schema_spec.rb +0 -8
- data/spec/serialize_spec.rb +0 -19
- data/spec/single_table_inheritance_spec.rb +0 -43
- data/spec/spec_helper.rb +0 -45
- data/spec/support/blank_spec.rb +0 -8
- data/spec/support/inflector_spec.rb +0 -41
- data/spec/support/object_spec.rb +0 -9
- data/spec/support/serialization_spec.rb +0 -61
- data/spec/support/silence_spec.rb +0 -15
- data/spec/support/string_spec.rb +0 -7
- data/spec/support/struct_spec.rb +0 -12
- data/spec/support/typed_set_spec.rb +0 -66
- data/spec/symbolic_operators_spec.rb +0 -27
- data/spec/table_spec.rb +0 -79
- data/spec/types/string.rb +0 -81
- data/spec/validates_confirmation_of_spec.rb +0 -55
- data/spec/validates_format_of_spec.rb +0 -78
- data/spec/validates_length_of_spec.rb +0 -117
- data/spec/validates_uniqueness_of_spec.rb +0 -92
- data/spec/validations/number_validator.rb +0 -59
- data/spec/validations/string_validator.rb +0 -14
- data/spec/validations_spec.rb +0 -141
- data/tasks/fixtures.rb +0 -53
@@ -1,72 +0,0 @@
|
|
1
|
-
require 'data_mapper/adapters/data_object_adapter'
|
2
|
-
begin
|
3
|
-
gem 'do_mysql', '<=0.2.4'
|
4
|
-
require 'do_mysql'
|
5
|
-
rescue LoadError
|
6
|
-
STDERR.puts <<-EOS
|
7
|
-
You must install the DataObjects::Mysql driver.
|
8
|
-
gem install do_mysql
|
9
|
-
EOS
|
10
|
-
exit
|
11
|
-
end
|
12
|
-
|
13
|
-
module DataMapper
|
14
|
-
module Adapters
|
15
|
-
|
16
|
-
class MysqlAdapter < DataObjectAdapter
|
17
|
-
|
18
|
-
TRUE_ALIASES << "T".freeze << "\004\bT".freeze
|
19
|
-
FALSE_ALIASES << "F".freeze << "\004\bF".freeze
|
20
|
-
|
21
|
-
def empty_insert_sql
|
22
|
-
"() VALUES ()"
|
23
|
-
end
|
24
|
-
|
25
|
-
def create_connection
|
26
|
-
|
27
|
-
connection_string = ""
|
28
|
-
builder = lambda { |k,v| connection_string << "#{k}=#{@configuration.send(v)} " unless @configuration.send(v).blank? }
|
29
|
-
|
30
|
-
builder['host', :host]
|
31
|
-
builder['user', :username]
|
32
|
-
builder['password', :password]
|
33
|
-
builder['dbname', :database]
|
34
|
-
builder['socket', :socket]
|
35
|
-
builder['port', :port]
|
36
|
-
|
37
|
-
logger.debug { connection_string.strip }
|
38
|
-
|
39
|
-
conn = DataObject::Mysql::Connection.new(connection_string.strip)
|
40
|
-
conn.logger = self.logger
|
41
|
-
conn.open
|
42
|
-
cmd = conn.create_command("SET NAMES UTF8")
|
43
|
-
cmd.execute_non_query
|
44
|
-
return conn
|
45
|
-
end
|
46
|
-
|
47
|
-
def database_column_name
|
48
|
-
"TABLE_SCHEMA"
|
49
|
-
end
|
50
|
-
|
51
|
-
module Mappings
|
52
|
-
|
53
|
-
def to_create_table_sql
|
54
|
-
@to_create_table_sql || @to_create_table_sql = begin
|
55
|
-
"CREATE TABLE #{to_sql} (#{columns.map { |c| c.to_long_form }.join(', ')}) Type=MyISAM CHARACTER SET utf8"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class Schema
|
60
|
-
|
61
|
-
def database_tables
|
62
|
-
get_database_tables(@adapter.schema.name)
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
|
68
|
-
end # module Mappings
|
69
|
-
end # class MysqlAdapter
|
70
|
-
|
71
|
-
end # module Adapters
|
72
|
-
end # module DataMapper
|
@@ -1,258 +0,0 @@
|
|
1
|
-
require 'data_mapper/adapters/data_object_adapter'
|
2
|
-
begin
|
3
|
-
gem 'do_postgres', '<=0.2.4'
|
4
|
-
require 'do_postgres'
|
5
|
-
rescue LoadError
|
6
|
-
STDERR.puts <<-EOS
|
7
|
-
You must install the DataObjects::Postgres driver.
|
8
|
-
gem install do_postgres
|
9
|
-
EOS
|
10
|
-
exit
|
11
|
-
end
|
12
|
-
|
13
|
-
module DataMapper
|
14
|
-
module Adapters
|
15
|
-
|
16
|
-
class PostgresqlAdapter < DataObjectAdapter
|
17
|
-
|
18
|
-
def schema_search_path
|
19
|
-
@schema_search_path || @schema_search_path = begin
|
20
|
-
if @configuration.schema_search_path
|
21
|
-
@configuration.schema_search_path.split(',').map do |part|
|
22
|
-
part.blank? ? nil : part.strip.ensure_wrapped_with("'")
|
23
|
-
end.compact
|
24
|
-
else
|
25
|
-
[]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def create_connection
|
31
|
-
connection_string = ""
|
32
|
-
builder = lambda do |k,v|
|
33
|
-
connection_string << "#{k}=#{@configuration.send(v)} " unless
|
34
|
-
@configuration.send(v).blank?
|
35
|
-
end
|
36
|
-
builder['host', :host]
|
37
|
-
builder['user', :username]
|
38
|
-
builder['password', :password]
|
39
|
-
builder['dbname', :database]
|
40
|
-
builder['socket', :socket]
|
41
|
-
builder['port', :port]
|
42
|
-
conn = DataObject::Postgres::Connection.new(connection_string.strip)
|
43
|
-
conn.logger = self.logger
|
44
|
-
conn.open
|
45
|
-
|
46
|
-
unless schema_search_path.empty?
|
47
|
-
execute("SET search_path TO #{schema_search_path}")
|
48
|
-
end
|
49
|
-
|
50
|
-
return conn
|
51
|
-
end
|
52
|
-
|
53
|
-
def database_column_name
|
54
|
-
"TABLE_CATALOG"
|
55
|
-
end
|
56
|
-
|
57
|
-
TABLE_QUOTING_CHARACTER = '"'.freeze
|
58
|
-
COLUMN_QUOTING_CHARACTER = '"'.freeze
|
59
|
-
|
60
|
-
TYPES.merge!({
|
61
|
-
:integer => 'integer'.freeze,
|
62
|
-
:datetime => 'timestamp with time zone'.freeze
|
63
|
-
})
|
64
|
-
|
65
|
-
module Mappings
|
66
|
-
class Table
|
67
|
-
def sequence_sql
|
68
|
-
@sequence_sql ||= quote_table("_id_seq").freeze
|
69
|
-
end
|
70
|
-
|
71
|
-
def to_create_table_sql
|
72
|
-
schema_name = name.index('.') ? name.split('.').first : nil
|
73
|
-
schema_list = @adapter.query('SELECT nspname FROM pg_namespace').join(',')
|
74
|
-
|
75
|
-
sql = if schema_name and !schema_list.include?(schema_name)
|
76
|
-
"CREATE SCHEMA #{@adapter.quote_table_name(schema_name)}; "
|
77
|
-
else
|
78
|
-
''
|
79
|
-
end
|
80
|
-
|
81
|
-
sql << "CREATE TABLE " << to_sql
|
82
|
-
|
83
|
-
sql << " (" << columns.map do |column|
|
84
|
-
column.to_long_form
|
85
|
-
end.join(', ') << ")"
|
86
|
-
|
87
|
-
return sql
|
88
|
-
end
|
89
|
-
|
90
|
-
# The logic of this comes from AR; it was modified for smarter typecasting
|
91
|
-
def unquote_default(default)
|
92
|
-
# Boolean types
|
93
|
-
return true if default =~ /true/i
|
94
|
-
return false if default =~ /false/i
|
95
|
-
|
96
|
-
# Char/String/Bytea type values
|
97
|
-
return $1 if default =~ /^'(.*)'::(bpchar|text|character varying|bytea)$/
|
98
|
-
|
99
|
-
# Numeric values
|
100
|
-
return value.to_f if default =~ /^-?[0-9]+(\.[0-9]*)/
|
101
|
-
return value.to_i if default =~ /^-?[0-9]+/
|
102
|
-
|
103
|
-
# Fixed dates / times
|
104
|
-
return Date.parse($1) if default =~ /^'(.+)'::date/
|
105
|
-
return DateTime.parse($1) if default =~ /^'(.+)'::timestamp/
|
106
|
-
|
107
|
-
# Anything else is blank, some user type, or some function
|
108
|
-
# and we can't know the value of that, so return nil.
|
109
|
-
return nil
|
110
|
-
end
|
111
|
-
|
112
|
-
# def to_exists_sql
|
113
|
-
# @to_exists_sql || @to_exists_sql = <<-EOS.compress_lines
|
114
|
-
# SELECT TABLE_NAME
|
115
|
-
# FROM INFORMATION_SCHEMA.TABLES
|
116
|
-
# WHERE TABLE_NAME = ?
|
117
|
-
# AND TABLE_CATALOG = ?
|
118
|
-
# EOS
|
119
|
-
# end
|
120
|
-
#
|
121
|
-
# def to_column_exists_sql
|
122
|
-
# @to_column_exists_sql || @to_column_exists_sql = <<-EOS.compress_lines
|
123
|
-
# SELECT TABLE_NAME, COLUMN_NAME
|
124
|
-
# FROM INFORMATION_SCHEMA.COLUMNS
|
125
|
-
# WHERE TABLE_NAME = ?
|
126
|
-
# AND COLUMN_NAME = ?
|
127
|
-
# AND TABLE_CATALOG = ?
|
128
|
-
# EOS
|
129
|
-
# end
|
130
|
-
|
131
|
-
private
|
132
|
-
|
133
|
-
def quote_table(table_suffix = nil)
|
134
|
-
parts = name.split('.')
|
135
|
-
parts.last << table_suffix if table_suffix
|
136
|
-
parts.map { |part|
|
137
|
-
@adapter.quote_table_name(part) }.join('.')
|
138
|
-
end
|
139
|
-
end # class Table
|
140
|
-
|
141
|
-
class Schema
|
142
|
-
|
143
|
-
def database_tables
|
144
|
-
get_database_tables("public")
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
class Column
|
150
|
-
def serial_declaration
|
151
|
-
"SERIAL"
|
152
|
-
end
|
153
|
-
|
154
|
-
def check_declaration
|
155
|
-
"CHECK (" << check << ")"
|
156
|
-
end
|
157
|
-
|
158
|
-
def to_alter_sql
|
159
|
-
"ALTER TABLE " << table.to_sql << " ALTER COLUMN " << to_alter_form
|
160
|
-
end
|
161
|
-
|
162
|
-
def alter!
|
163
|
-
result = super
|
164
|
-
reset_alter_state!
|
165
|
-
result
|
166
|
-
end
|
167
|
-
|
168
|
-
def reset_alter_state!
|
169
|
-
@type_changed = false
|
170
|
-
@default_changed = false
|
171
|
-
end
|
172
|
-
|
173
|
-
def default=(value)
|
174
|
-
@default_changed = true
|
175
|
-
super
|
176
|
-
end
|
177
|
-
|
178
|
-
def type=(value)
|
179
|
-
@type_changed = true
|
180
|
-
super
|
181
|
-
end
|
182
|
-
|
183
|
-
def to_alter_form
|
184
|
-
sql = to_sql.dup
|
185
|
-
|
186
|
-
changes = 0
|
187
|
-
|
188
|
-
if @type_changed
|
189
|
-
changes += 1
|
190
|
-
sql << " TYPE " << type_declaration
|
191
|
-
case self.type
|
192
|
-
when :integer then sql << " USING #{to_sql}::integer"
|
193
|
-
when :datetime then
|
194
|
-
sql << " USING timestamp with time zone"
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
if @default_changed
|
199
|
-
sql << ", " if changes += 1 > 1
|
200
|
-
|
201
|
-
if default.blank? || default_declaration.blank?
|
202
|
-
sql << " DROP DEFAULT"
|
203
|
-
else
|
204
|
-
sql << " " << default_declaration
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
sql
|
209
|
-
end
|
210
|
-
|
211
|
-
def to_long_form
|
212
|
-
@to_long_form || begin
|
213
|
-
@to_long_form = "#{to_sql}"
|
214
|
-
|
215
|
-
if serial? && !serial_declaration.blank?
|
216
|
-
@to_long_form << " #{serial_declaration}"
|
217
|
-
if key? && !primary_key_declaration.blank?
|
218
|
-
@to_long_form << " #{primary_key_declaration}"
|
219
|
-
end
|
220
|
-
else
|
221
|
-
@to_long_form << " #{type_declaration}"
|
222
|
-
|
223
|
-
unless nullable? || not_null_declaration.blank?
|
224
|
-
@to_long_form << " #{not_null_declaration}"
|
225
|
-
end
|
226
|
-
|
227
|
-
if default && !default_declaration.blank?
|
228
|
-
@to_long_form << " #{default_declaration}"
|
229
|
-
end
|
230
|
-
|
231
|
-
if unique? && !unique_declaration.blank?
|
232
|
-
@to_long_form << " #{unique_declaration}"
|
233
|
-
end
|
234
|
-
|
235
|
-
if check && !check_declaration.blank?
|
236
|
-
@to_long_form << " #{check_declaration}"
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
@to_long_form
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
# size is still required, as length in postgres behaves slightly differently
|
245
|
-
def size
|
246
|
-
case self.type
|
247
|
-
#strings in postgres can be unlimited length
|
248
|
-
when :string then return (@options.has_key?(:length) || @options.has_key?(:size) ? @size : nil)
|
249
|
-
else nil
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end # class Column
|
253
|
-
end # module Mappings
|
254
|
-
|
255
|
-
end # class PostgresqlAdapter
|
256
|
-
|
257
|
-
end # module Adapters
|
258
|
-
end # module DataMapper
|
@@ -1,134 +0,0 @@
|
|
1
|
-
require 'bigdecimal'
|
2
|
-
require 'bigdecimal/util'
|
3
|
-
|
4
|
-
module DataMapper
|
5
|
-
module Adapters
|
6
|
-
module Sql
|
7
|
-
# Coersion is a mixin that allows for coercing database values to Ruby Types.
|
8
|
-
#
|
9
|
-
# DESIGN: Probably should handle the opposite scenario here too. I believe that's
|
10
|
-
# currently in DataMapper::Database, which is obviously not a very good spot for
|
11
|
-
# it.
|
12
|
-
module Coersion
|
13
|
-
|
14
|
-
class CoersionError < StandardError
|
15
|
-
end
|
16
|
-
|
17
|
-
TRUE_ALIASES = ['true'.freeze, 'TRUE'.freeze, '1'.freeze, 1]
|
18
|
-
FALSE_ALIASES = [nil, '0'.freeze, 0]
|
19
|
-
|
20
|
-
def self.included(base)
|
21
|
-
base.const_set('TRUE_ALIASES', TRUE_ALIASES.dup)
|
22
|
-
base.const_set('FALSE_ALIASES', FALSE_ALIASES.dup)
|
23
|
-
end
|
24
|
-
|
25
|
-
def type_cast_boolean(raw_value)
|
26
|
-
return nil if raw_value.nil? || (raw_value.respond_to?(:empty?) && raw_value.empty?)
|
27
|
-
case raw_value
|
28
|
-
when TrueClass, FalseClass then raw_value
|
29
|
-
when *self::class::TRUE_ALIASES then true
|
30
|
-
when *self::class::FALSE_ALIASES then false
|
31
|
-
else raise CoersionError.new("Can't type-cast #{raw_value.inspect} to a boolean")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def type_cast_string(raw_value)
|
36
|
-
return nil if raw_value.blank?
|
37
|
-
# type-cast values should be immutable for memory conservation
|
38
|
-
raw_value
|
39
|
-
end
|
40
|
-
|
41
|
-
def type_cast_text(raw_value)
|
42
|
-
return nil if raw_value.blank?
|
43
|
-
# type-cast values should be immutable for memory conservation
|
44
|
-
raw_value
|
45
|
-
end
|
46
|
-
|
47
|
-
def type_cast_class(raw_value)
|
48
|
-
return nil if raw_value.blank?
|
49
|
-
Object::recursive_const_get(raw_value)
|
50
|
-
end
|
51
|
-
|
52
|
-
def type_cast_integer(raw_value)
|
53
|
-
return nil if raw_value.blank?
|
54
|
-
raw_value.to_i
|
55
|
-
rescue ArgumentError
|
56
|
-
nil
|
57
|
-
end
|
58
|
-
|
59
|
-
def type_cast_decimal(raw_value)
|
60
|
-
return nil if raw_value.blank?
|
61
|
-
raw_value.to_d
|
62
|
-
rescue ArgumentError
|
63
|
-
nil
|
64
|
-
end
|
65
|
-
|
66
|
-
def type_cast_float(raw_value)
|
67
|
-
return nil if raw_value.blank?
|
68
|
-
case raw_value
|
69
|
-
when Float then raw_value
|
70
|
-
when Numeric, String then raw_value.to_f
|
71
|
-
else CoersionError.new("Can't type-cast #{raw_value.inspect} to a float")
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def type_cast_datetime(raw_value)
|
76
|
-
return nil if raw_value.blank?
|
77
|
-
|
78
|
-
case raw_value
|
79
|
-
when "0000-00-00 00:00:00" then nil
|
80
|
-
when DateTime then raw_value
|
81
|
-
when Date then DateTime.new(raw_value) rescue nil
|
82
|
-
when String then DateTime::parse(raw_value) rescue nil
|
83
|
-
else raise CoersionError.new("Can't type-cast #{raw_value.inspect} to a datetime")
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def type_cast_date(raw_value)
|
88
|
-
return nil if raw_value.blank?
|
89
|
-
|
90
|
-
case raw_value
|
91
|
-
when Date then raw_value
|
92
|
-
when DateTime, Time then Date::civil(raw_value.year, raw_value.month, raw_value.day)
|
93
|
-
when String then Date::parse(raw_value)
|
94
|
-
else raise CoersionError.new("Can't type-cast #{raw_value.inspect} to a date")
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def type_cast_object(raw_value)
|
99
|
-
return nil if raw_value.blank?
|
100
|
-
|
101
|
-
begin
|
102
|
-
YAML.load(raw_value)
|
103
|
-
rescue
|
104
|
-
raise CoersionError.new("Can't type-cast #{raw_value.inspect} to an object")
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def type_cast_value(type, raw_value)
|
109
|
-
return nil if raw_value.blank?
|
110
|
-
|
111
|
-
case type
|
112
|
-
when :string then type_cast_string(raw_value)
|
113
|
-
when :text then type_cast_text(raw_value)
|
114
|
-
when :boolean then type_cast_boolean(raw_value)
|
115
|
-
when :class then type_cast_class(raw_value)
|
116
|
-
when :integer then type_cast_integer(raw_value)
|
117
|
-
when :decimal then type_cast_decimal(raw_value)
|
118
|
-
when :float then type_cast_float(raw_value)
|
119
|
-
when :datetime then type_cast_datetime(raw_value)
|
120
|
-
when :date then type_cast_date(raw_value)
|
121
|
-
when :object then type_cast_object(raw_value)
|
122
|
-
else
|
123
|
-
if respond_to?("type_cast_#{type}")
|
124
|
-
send("type_cast_#{type}", raw_value)
|
125
|
-
else
|
126
|
-
raise "Don't know how to type-cast #{{ type => raw_value }.inspect }"
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
end # module Coersion
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|