mack-data_mapper 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/gems/addressable-2.0.0/lib/addressable/idna.rb +4867 -0
  2. data/lib/gems/addressable-2.0.0/lib/addressable/uri.rb +2469 -0
  3. data/lib/gems/addressable-2.0.0/lib/addressable/version.rb +35 -0
  4. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/adapters/data_objects_adapter.rb +85 -0
  5. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/aggregate_functions.rb +201 -0
  6. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/collection.rb +11 -0
  7. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/model.rb +11 -0
  8. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/repository.rb +7 -0
  9. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/support/symbol.rb +21 -0
  10. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates/version.rb +7 -0
  11. data/lib/gems/dm-aggregates-0.9.7/lib/dm-aggregates.rb +15 -0
  12. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/abstract_adapter.rb +209 -0
  13. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/data_objects_adapter.rb +709 -0
  14. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/in_memory_adapter.rb +87 -0
  15. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/mysql_adapter.rb +136 -0
  16. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/postgres_adapter.rb +188 -0
  17. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters/sqlite3_adapter.rb +105 -0
  18. data/lib/gems/dm-core-0.9.7/lib/dm-core/adapters.rb +22 -0
  19. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/many_to_many.rb +147 -0
  20. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/many_to_one.rb +107 -0
  21. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/one_to_many.rb +318 -0
  22. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/one_to_one.rb +61 -0
  23. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/relationship.rb +223 -0
  24. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations/relationship_chain.rb +81 -0
  25. data/lib/gems/dm-core-0.9.7/lib/dm-core/associations.rb +200 -0
  26. data/lib/gems/dm-core-0.9.7/lib/dm-core/auto_migrations.rb +105 -0
  27. data/lib/gems/dm-core-0.9.7/lib/dm-core/collection.rb +642 -0
  28. data/lib/gems/dm-core-0.9.7/lib/dm-core/dependency_queue.rb +32 -0
  29. data/lib/gems/dm-core-0.9.7/lib/dm-core/hook.rb +11 -0
  30. data/lib/gems/dm-core-0.9.7/lib/dm-core/identity_map.rb +42 -0
  31. data/lib/gems/dm-core-0.9.7/lib/dm-core/is.rb +16 -0
  32. data/lib/gems/dm-core-0.9.7/lib/dm-core/logger.rb +232 -0
  33. data/lib/gems/dm-core-0.9.7/lib/dm-core/migrations/destructive_migrations.rb +17 -0
  34. data/lib/gems/dm-core-0.9.7/lib/dm-core/migrator.rb +29 -0
  35. data/lib/gems/dm-core-0.9.7/lib/dm-core/model.rb +488 -0
  36. data/lib/gems/dm-core-0.9.7/lib/dm-core/naming_conventions.rb +84 -0
  37. data/lib/gems/dm-core-0.9.7/lib/dm-core/property.rb +663 -0
  38. data/lib/gems/dm-core-0.9.7/lib/dm-core/property_set.rb +169 -0
  39. data/lib/gems/dm-core-0.9.7/lib/dm-core/query.rb +628 -0
  40. data/lib/gems/dm-core-0.9.7/lib/dm-core/repository.rb +159 -0
  41. data/lib/gems/dm-core-0.9.7/lib/dm-core/resource.rb +637 -0
  42. data/lib/gems/dm-core-0.9.7/lib/dm-core/scope.rb +58 -0
  43. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/array.rb +13 -0
  44. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/assertions.rb +8 -0
  45. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/errors.rb +23 -0
  46. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/kernel.rb +11 -0
  47. data/lib/gems/dm-core-0.9.7/lib/dm-core/support/symbol.rb +41 -0
  48. data/lib/gems/dm-core-0.9.7/lib/dm-core/support.rb +7 -0
  49. data/lib/gems/dm-core-0.9.7/lib/dm-core/transaction.rb +267 -0
  50. data/lib/gems/dm-core-0.9.7/lib/dm-core/type.rb +160 -0
  51. data/lib/gems/dm-core-0.9.7/lib/dm-core/type_map.rb +80 -0
  52. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/boolean.rb +7 -0
  53. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/discriminator.rb +34 -0
  54. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/object.rb +24 -0
  55. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/paranoid_boolean.rb +34 -0
  56. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/paranoid_datetime.rb +33 -0
  57. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/serial.rb +9 -0
  58. data/lib/gems/dm-core-0.9.7/lib/dm-core/types/text.rb +10 -0
  59. data/lib/gems/dm-core-0.9.7/lib/dm-core/types.rb +19 -0
  60. data/lib/gems/dm-core-0.9.7/lib/dm-core/version.rb +3 -0
  61. data/lib/gems/dm-core-0.9.7/lib/dm-core.rb +217 -0
  62. data/lib/gems/dm-core-0.9.7/script/all +5 -0
  63. data/lib/gems/dm-core-0.9.7/script/performance.rb +284 -0
  64. data/lib/gems/dm-core-0.9.7/script/profile.rb +87 -0
  65. data/lib/gems/dm-migrations-0.9.7/lib/dm-migrations/version.rb +5 -0
  66. data/lib/gems/dm-migrations-0.9.7/lib/dm-migrations.rb +1 -0
  67. data/lib/gems/dm-migrations-0.9.7/lib/migration.rb +215 -0
  68. data/lib/gems/dm-migrations-0.9.7/lib/migration_runner.rb +88 -0
  69. data/lib/gems/dm-migrations-0.9.7/lib/spec/example/migration_example_group.rb +73 -0
  70. data/lib/gems/dm-migrations-0.9.7/lib/spec/matchers/migration_matchers.rb +107 -0
  71. data/lib/gems/dm-migrations-0.9.7/lib/sql/column.rb +9 -0
  72. data/lib/gems/dm-migrations-0.9.7/lib/sql/mysql.rb +52 -0
  73. data/lib/gems/dm-migrations-0.9.7/lib/sql/postgresql.rb +78 -0
  74. data/lib/gems/dm-migrations-0.9.7/lib/sql/sqlite3.rb +43 -0
  75. data/lib/gems/dm-migrations-0.9.7/lib/sql/table.rb +19 -0
  76. data/lib/gems/dm-migrations-0.9.7/lib/sql/table_creator.rb +81 -0
  77. data/lib/gems/dm-migrations-0.9.7/lib/sql/table_modifier.rb +53 -0
  78. data/lib/gems/dm-migrations-0.9.7/lib/sql.rb +10 -0
  79. data/lib/gems/dm-observer-0.9.7/lib/dm-observer/version.rb +5 -0
  80. data/lib/gems/dm-observer-0.9.7/lib/dm-observer.rb +91 -0
  81. data/lib/gems/dm-serializer-0.9.7/lib/dm-serializer/version.rb +5 -0
  82. data/lib/gems/dm-serializer-0.9.7/lib/dm-serializer.rb +183 -0
  83. data/lib/gems/dm-timestamps-0.9.7/lib/dm-timestamps/version.rb +5 -0
  84. data/lib/gems/dm-timestamps-0.9.7/lib/dm-timestamps.rb +57 -0
  85. data/lib/gems/dm-types-0.9.7/lib/dm-types/bcrypt_hash.rb +31 -0
  86. data/lib/gems/dm-types-0.9.7/lib/dm-types/csv.rb +28 -0
  87. data/lib/gems/dm-types-0.9.7/lib/dm-types/enum.rb +70 -0
  88. data/lib/gems/dm-types-0.9.7/lib/dm-types/epoch_time.rb +27 -0
  89. data/lib/gems/dm-types-0.9.7/lib/dm-types/file_path.rb +27 -0
  90. data/lib/gems/dm-types-0.9.7/lib/dm-types/flag.rb +61 -0
  91. data/lib/gems/dm-types-0.9.7/lib/dm-types/ip_address.rb +30 -0
  92. data/lib/gems/dm-types-0.9.7/lib/dm-types/json.rb +40 -0
  93. data/lib/gems/dm-types-0.9.7/lib/dm-types/regexp.rb +20 -0
  94. data/lib/gems/dm-types-0.9.7/lib/dm-types/serial.rb +8 -0
  95. data/lib/gems/dm-types-0.9.7/lib/dm-types/slug.rb +37 -0
  96. data/lib/gems/dm-types-0.9.7/lib/dm-types/uri.rb +29 -0
  97. data/lib/gems/dm-types-0.9.7/lib/dm-types/uuid.rb +64 -0
  98. data/lib/gems/dm-types-0.9.7/lib/dm-types/version.rb +5 -0
  99. data/lib/gems/dm-types-0.9.7/lib/dm-types/yaml.rb +36 -0
  100. data/lib/gems/dm-types-0.9.7/lib/dm-types.rb +28 -0
  101. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/absent_field_validator.rb +60 -0
  102. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/acceptance_validator.rb +76 -0
  103. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/auto_validate.rb +153 -0
  104. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/block_validator.rb +60 -0
  105. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/confirmation_validator.rb +80 -0
  106. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb +56 -0
  107. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/custom_validator.rb +72 -0
  108. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/format_validator.rb +97 -0
  109. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/formats/email.rb +40 -0
  110. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/formats/url.rb +20 -0
  111. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/generic_validator.rb +100 -0
  112. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/length_validator.rb +113 -0
  113. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/method_validator.rb +68 -0
  114. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/numeric_validator.rb +83 -0
  115. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/primitive_validator.rb +60 -0
  116. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/required_field_validator.rb +88 -0
  117. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/support/object.rb +5 -0
  118. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/uniqueness_validator.rb +64 -0
  119. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/validation_errors.rb +63 -0
  120. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/version.rb +5 -0
  121. data/lib/gems/dm-validations-0.9.7/lib/dm-validations/within_validator.rb +53 -0
  122. data/lib/gems/dm-validations-0.9.7/lib/dm-validations.rb +234 -0
  123. data/lib/gems/json_pure-1.1.3/GPL +340 -0
  124. data/lib/gems/json_pure-1.1.3/VERSION +1 -0
  125. data/lib/gems/json_pure-1.1.3/bin/edit_json.rb +10 -0
  126. data/lib/gems/json_pure-1.1.3/bin/prettify_json.rb +76 -0
  127. data/lib/gems/json_pure-1.1.3/lib/json/Array.xpm +21 -0
  128. data/lib/gems/json_pure-1.1.3/lib/json/FalseClass.xpm +21 -0
  129. data/lib/gems/json_pure-1.1.3/lib/json/Hash.xpm +21 -0
  130. data/lib/gems/json_pure-1.1.3/lib/json/Key.xpm +73 -0
  131. data/lib/gems/json_pure-1.1.3/lib/json/NilClass.xpm +21 -0
  132. data/lib/gems/json_pure-1.1.3/lib/json/Numeric.xpm +28 -0
  133. data/lib/gems/json_pure-1.1.3/lib/json/String.xpm +96 -0
  134. data/lib/gems/json_pure-1.1.3/lib/json/TrueClass.xpm +21 -0
  135. data/lib/gems/json_pure-1.1.3/lib/json/add/core.rb +135 -0
  136. data/lib/gems/json_pure-1.1.3/lib/json/add/rails.rb +58 -0
  137. data/lib/gems/json_pure-1.1.3/lib/json/common.rb +354 -0
  138. data/lib/gems/json_pure-1.1.3/lib/json/editor.rb +1362 -0
  139. data/lib/gems/json_pure-1.1.3/lib/json/ext.rb +13 -0
  140. data/lib/gems/json_pure-1.1.3/lib/json/json.xpm +1499 -0
  141. data/lib/gems/json_pure-1.1.3/lib/json/pure/generator.rb +394 -0
  142. data/lib/gems/json_pure-1.1.3/lib/json/pure/parser.rb +259 -0
  143. data/lib/gems/json_pure-1.1.3/lib/json/pure.rb +75 -0
  144. data/lib/gems/json_pure-1.1.3/lib/json/version.rb +9 -0
  145. data/lib/gems/json_pure-1.1.3/lib/json.rb +235 -0
  146. data/lib/gems/launchy-0.3.2/bin/launchy +12 -0
  147. data/lib/gems/launchy-0.3.2/lib/launchy/application.rb +163 -0
  148. data/lib/gems/launchy-0.3.2/lib/launchy/browser.rb +85 -0
  149. data/lib/gems/launchy-0.3.2/lib/launchy/command_line.rb +48 -0
  150. data/lib/gems/launchy-0.3.2/lib/launchy/gemspec.rb +53 -0
  151. data/lib/gems/launchy-0.3.2/lib/launchy/specification.rb +133 -0
  152. data/lib/gems/launchy-0.3.2/lib/launchy/version.rb +18 -0
  153. data/lib/gems/launchy-0.3.2/lib/launchy.rb +58 -0
  154. data/lib/gems/uuidtools-1.0.3/lib/uuidtools/version.rb +32 -0
  155. data/lib/gems/uuidtools-1.0.3/lib/uuidtools.rb +648 -0
  156. data/lib/gems.rb +13 -0
  157. data/lib/mack-data_mapper/migration_generator/migration_generator.rb +5 -0
  158. data/lib/mack-data_mapper/migration_generator/templates/db/migrations/%=@migration_name%.rb.template +1 -1
  159. data/lib/mack-data_mapper/model_generator/manifest.yml +3 -3
  160. data/lib/mack-data_mapper/model_generator/model_generator.rb +8 -1
  161. data/lib/mack-data_mapper/model_generator/templates/model.rb.template +1 -1
  162. data/lib/mack-data_mapper/model_generator/templates/rspec.rb.template +1 -1
  163. data/lib/mack-data_mapper/model_generator/templates/test_case.rb.template +1 -1
  164. data/lib/mack-data_mapper.rb +3 -2
  165. data/lib/mack-data_mapper_tasks.rb +7 -0
  166. metadata +235 -86
@@ -0,0 +1,61 @@
1
+ module DataMapper
2
+ module Types
3
+ class Flag < DataMapper::Type(Integer)
4
+ def self.inherited(target)
5
+ target.instance_variable_set("@primitive", self.primitive)
6
+ end
7
+
8
+ def self.flag_map
9
+ @flag_map
10
+ end
11
+
12
+ def self.flag_map=(value)
13
+ @flag_map = value
14
+ end
15
+
16
+ def self.new(*flags)
17
+ type = Class.new(Flag)
18
+ type.flag_map = {}
19
+
20
+ flags.each_with_index do |flag, i|
21
+ type.flag_map[2 ** i] = flag
22
+ end
23
+
24
+ type
25
+ end
26
+
27
+ def self.[](*flags)
28
+ new(*flags)
29
+ end
30
+
31
+ def self.load(value, property)
32
+ begin
33
+ matches = []
34
+
35
+ 0.upto((Math.log(value) / Math.log(2)).ceil) do |i|
36
+ pow = 2 ** i
37
+ matches << flag_map[pow] if value & pow == pow
38
+ end
39
+
40
+ matches.compact
41
+ rescue TypeError, Errno::EDOM
42
+ []
43
+ end
44
+ end
45
+
46
+ def self.dump(value, property)
47
+ return if value.nil?
48
+ flags = value.is_a?(Array) ? value : [value]
49
+ flags.map!{ |f| f.to_sym }
50
+ flag_map.invert.values_at(*flags.flatten).compact.inject(0) { |sum, i| sum + i }
51
+ end
52
+
53
+ def self.typecast(value, property)
54
+ case value
55
+ when Array then value.map {|v| v.to_sym}
56
+ else value.to_sym
57
+ end
58
+ end
59
+ end # class Flag
60
+ end # module Types
61
+ end # module DataMapper
@@ -0,0 +1,30 @@
1
+ require 'ipaddr'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class IPAddress < DataMapper::Type
6
+ primitive String
7
+
8
+ def self.load(value, property)
9
+ if value.nil?
10
+ nil
11
+ elsif value.is_a?(String) && !value.empty?
12
+ IPAddr.new(value)
13
+ elsif value.is_a?(String) && value.empty?
14
+ IPAddr.new("0.0.0.0")
15
+ else
16
+ raise ArgumentError.new("+value+ must be nil or a String")
17
+ end
18
+ end
19
+
20
+ def self.dump(value, property)
21
+ return nil if value.nil?
22
+ value.to_s
23
+ end
24
+
25
+ def self.typecast(value, property)
26
+ value.kind_of?(IPAddr) ? value : load(value, property)
27
+ end
28
+ end # class IPAddress
29
+ end # module Types
30
+ end # module DataMapper
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class Json < DataMapper::Type
6
+ primitive String
7
+ size 65535
8
+ lazy true
9
+
10
+ def self.load(value, property)
11
+ if value.nil?
12
+ nil
13
+ elsif value.is_a?(String)
14
+ ::JSON.load(value)
15
+ else
16
+ raise ArgumentError.new("+value+ must be nil or a String")
17
+ end
18
+ end
19
+
20
+ def self.dump(value, property)
21
+ if value.nil?
22
+ nil
23
+ elsif value.is_a?(String)
24
+ value
25
+ else
26
+ ::JSON.dump(value)
27
+ end
28
+ end
29
+
30
+ def self.typecast(value, property)
31
+ # Arrays and hashes are left alone, while strings are parsed as JSON.
32
+ if value.kind_of?(Array) || value.kind_of?(Hash)
33
+ value
34
+ else
35
+ ::JSON.load(value.to_s)
36
+ end
37
+ end
38
+ end # class Json
39
+ end # module Types
40
+ end # module DataMapper
@@ -0,0 +1,20 @@
1
+ module DataMapper
2
+ module Types
3
+ class Regexp < DataMapper::Type
4
+ primitive String
5
+
6
+ def self.load(value, property)
7
+ ::Regexp.new(value) unless value.nil?
8
+ end
9
+
10
+ def self.dump(value, property)
11
+ return nil if value.nil?
12
+ value.source
13
+ end
14
+
15
+ def self.typecast(value, property)
16
+ value.kind_of?(::Regexp) ? value : load(value, property)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ module DataMapper
2
+ module Types
3
+ class Serial < DataMapper::Type
4
+ primitive Integer
5
+ serial true
6
+ end # class Serial
7
+ end # module Types
8
+ end # module DataMapper
@@ -0,0 +1,37 @@
1
+ require 'iconv'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class Slug < DataMapper::Type
6
+ primitive String
7
+ size 65535
8
+
9
+ def self.load(value, property)
10
+ value
11
+ end
12
+
13
+ def self.dump(value, property)
14
+ if value.nil?
15
+ nil
16
+ elsif value.is_a?(String)
17
+ #Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv(value.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase).to_s
18
+ escape(value)
19
+ else
20
+ raise ArgumentError.new("+value+ must be nil or a String")
21
+ end
22
+ end
23
+
24
+ # Hugs and kisses to Rick Olsons permalink_fu for the escape method
25
+ def self.escape(string)
26
+ result = Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s
27
+ result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
28
+ result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
29
+ result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
30
+ result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
31
+ result.downcase!
32
+ result
33
+ end
34
+
35
+ end # class Slug
36
+ end # module Types
37
+ end # module DataMapper
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'addressable/uri'
3
+
4
+ module DataMapper
5
+ module Types
6
+ class URI < DataMapper::Type
7
+ primitive String
8
+
9
+ def self.load(value, property)
10
+ Addressable::URI.parse(value)
11
+ end
12
+
13
+ def self.dump(value, property)
14
+ return nil if value.nil?
15
+ value.to_s
16
+ end
17
+
18
+ def self.typecast(value, property)
19
+ if value.kind_of?(Addressable::URI)
20
+ value
21
+ elsif value.nil?
22
+ load(nil, property)
23
+ else
24
+ load(value.to_s, property)
25
+ end
26
+ end
27
+ end # class URI
28
+ end # module Types
29
+ end # module DataMapper
@@ -0,0 +1,64 @@
1
+ require 'uuidtools'
2
+
3
+ module DataMapper
4
+ module Types
5
+ # UUID Type
6
+ # First run at this, because I need it. A few caveats:
7
+ # * Only works on postgres, using the built-in native uuid type.
8
+ # To make it work in mysql, you'll have to add a typemap entry to
9
+ # the mysql_adapter. I think. I don't have mysql handy, so I'm
10
+ # not going to try. For SQLite, this will have to inherit from the
11
+ # String primitive
12
+ # * Won't accept a random default, because of the namespace clash
13
+ # between this and the UUIDtools gem. Also can't set the default
14
+ # type to UUID() (postgres-contrib's native generator) and
15
+ # automigrate, because auto_migrate! tries to make it a string "UUID()"
16
+ # Feel free to enchance this, and delete these caveats when they're fixed.
17
+ #
18
+ # -- Rando Sept 25, 08
19
+ #
20
+ # Actually, setting the primitive to "UUID" is not neccessary and causes
21
+ # a segfault when trying to query uuid's from the database. The primitive
22
+ # should be a class which has been added to the do driver you are using.
23
+ # Also, it's only neccessary to add a class to the do drivers to use as a
24
+ # primitive when a value cannot be represented as a string. A uuid can be
25
+ # represented as a string, so setting the primitive to String ensures that
26
+ # the value argument is a String containing the uuid in string form.
27
+ #
28
+ # <strike>It is still neccessary to add the UUID entry to the type map for
29
+ # each different adapter with their respective database primitive.</strike>
30
+ #
31
+ # The method that generates the SQL schema from the typemap currently
32
+ # ignores the size attribute from the type map if the primitive type
33
+ # is String. The causes the generated SQL statement to contain a size for
34
+ # a UUID column (e.g. id UUID(50)), which causes a syntax error in postgres.
35
+ # Until this is resolved, you will have to manually change the column type
36
+ # to UUID in a migration, if you want to use postgres' built in UUID type.
37
+ #
38
+ # -- benburkert Nov 15, 08
39
+ #
40
+ class UUID < DataMapper::Type
41
+ primitive String
42
+ size 36
43
+
44
+ def self.load(value, property)
45
+ return nil if value.nil?
46
+ ::UUID.parse(value)
47
+ end
48
+
49
+ def self.dump(value, property)
50
+ return nil if value.nil?
51
+ value.to_s
52
+ end
53
+
54
+ def self.typecast(value, property)
55
+ value.kind_of?(::UUID) ? value : load(value, property)
56
+ end
57
+
58
+ #::DataMapper::Property::TYPES << self
59
+ #if defined? DataMapper::Adapters::PostgresAdapter
60
+ # DataMapper::Adapters::PostgresAdapter.type_map.map(self).to('UUID').with(:size => nil)
61
+ #end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module DataMapper
2
+ module Types
3
+ VERSION = "0.9.7"
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class Yaml < DataMapper::Type
6
+ primitive String
7
+ size 65535
8
+ lazy true
9
+
10
+ def self.load(value, property)
11
+ if value.nil?
12
+ nil
13
+ elsif value.is_a?(String)
14
+ ::YAML.load(value)
15
+ else
16
+ raise ArgumentError.new("+value+ must be nil or a String")
17
+ end
18
+ end
19
+
20
+ def self.dump(value, property)
21
+ if value.nil?
22
+ nil
23
+ elsif value.is_a?(String) && value =~ /^---/
24
+ value
25
+ else
26
+ ::YAML.dump(value)
27
+ end
28
+ end
29
+
30
+ def self.typecast(value, property)
31
+ # No typecasting; leave values exactly as they're provided.
32
+ value
33
+ end
34
+ end # class Yaml
35
+ end # module Types
36
+ end # module DataMapper
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ gem 'dm-core', '~>0.9.7'
5
+ require 'dm-core'
6
+
7
+ dir = Pathname(__FILE__).dirname.expand_path / 'dm-types'
8
+
9
+ require dir / 'csv'
10
+ require dir / 'enum'
11
+ require dir / 'epoch_time'
12
+ require dir / 'file_path'
13
+ require dir / 'flag'
14
+ require dir / 'ip_address'
15
+ require dir / "json"""
16
+ require dir / 'uri'
17
+ require dir / 'uuid'
18
+ require dir / 'yaml'
19
+ require dir / 'serial'
20
+ require dir / 'regexp'
21
+ require dir / 'slug'
22
+
23
+ # this looks a little ugly, but everyone who uses dm-types shouldn't have to have ruby-bcrypt installed
24
+ module DataMapper
25
+ module Types
26
+ autoload(:BCryptHash, File.join(Pathname(__FILE__).dirname.expand_path, 'dm-types', 'bcrypt_hash'))
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ module DataMapper
2
+ module Validate
3
+
4
+ ##
5
+ #
6
+ # @author Guy van den Berg
7
+ # @since 0.9
8
+ class AbsentFieldValidator < GenericValidator
9
+
10
+ def initialize(field_name, options={})
11
+ super
12
+ @field_name, @options = field_name, options
13
+ end
14
+
15
+ def call(target)
16
+ field_value = target.send(field_name).blank?
17
+ return true if field_value
18
+
19
+ error_message = @options[:message] || "%s must be absent".t(Extlib::Inflection.humanize(@field_name))
20
+ add_error(target, error_message , @field_name)
21
+
22
+ return false
23
+ end
24
+ end # class AbsentFieldValidator
25
+
26
+ module ValidatesAbsent
27
+
28
+ ##
29
+ # Validates that the specified attribute is "blank" via the attribute's
30
+ # #blank? method.
31
+ #
32
+ # @note
33
+ # dm-core's support lib adds the #blank? method to many classes,
34
+ # @see lib/dm-core/support/blank.rb (dm-core) for more information.
35
+ #
36
+ # @example [Usage]
37
+ # require 'dm-validations'
38
+ #
39
+ # class Page
40
+ # include DataMapper::Resource
41
+ #
42
+ # property :unwanted_attribute, String
43
+ # property :another_unwanted, String
44
+ # property :yet_again, String
45
+ #
46
+ # validates_absent :unwanted_attribute
47
+ # validates_absent :another_unwanted, :yet_again
48
+ #
49
+ # # a call to valid? will return false unless
50
+ # # all three attributes are blank
51
+ # end
52
+ #
53
+ def validates_absent(*fields)
54
+ opts = opts_from_validator_args(fields)
55
+ add_validator_to_context(opts, fields, DataMapper::Validate::AbsentFieldValidator)
56
+ end
57
+
58
+ end # module ValidatesAbsent
59
+ end # module Validate
60
+ end # module DataMapper
@@ -0,0 +1,76 @@
1
+ module DataMapper
2
+ module Validate
3
+
4
+ ##
5
+ #
6
+ # @author Martin Kihlgren
7
+ # @since 0.9
8
+ class AcceptanceValidator < GenericValidator
9
+
10
+ def self.default_message_for_field(field_name)
11
+ '%s is not accepted'.t(Extlib::Inflection.humanize(field_name))
12
+ end
13
+
14
+ def initialize(field_name, options = {})
15
+ super
16
+ @options = options
17
+ @field_name = field_name
18
+ @options[:allow_nil] = true unless @options.include?(:allow_nil)
19
+ @options[:accept] ||= ["1",1,"true",true,"t"]
20
+ @options[:accept] = Array(@options[:accept])
21
+ end
22
+
23
+ def call(target)
24
+ unless valid?(target)
25
+ error_message = @options[:message] || DataMapper::Validate::AcceptanceValidator.default_message_for_field(@field_name)
26
+ add_error(target, error_message , @field_name)
27
+ return false
28
+ end
29
+
30
+ return true
31
+ end
32
+
33
+ def valid?(target)
34
+ field_value = target.instance_variable_get("@#{@field_name}")
35
+ return true if @options[:allow_nil] && field_value.nil?
36
+ return false if !@options[:allow_nil] && field_value.nil?
37
+
38
+ @options[:accept].include?(field_value)
39
+ end
40
+
41
+ end # class AcceptanceValidator
42
+
43
+ module ValidatesIsAccepted
44
+
45
+ ##
46
+ # Validates that the attributes's value is in the set of accepted values.
47
+ #
48
+ # @option :allow_nil<Boolean> true if nil is allowed, false if nil is not
49
+ # allowed. Default is true.
50
+ # @option :accept<Array> a list of accepted values.
51
+ # Default are ["1",1,"true",true,"t"]).
52
+ #
53
+ # @example [Usage]
54
+ # require 'dm-validations'
55
+ #
56
+ # class Page
57
+ # include DataMapper::Resource
58
+ #
59
+ # property :license_agreement_accepted, String
60
+ # property :terms_accepted, String
61
+ # validates_is_accepted :license_agreement, :accept => "1"
62
+ # validates_is_accepted :terms_accepted, :allow_nil => false
63
+ #
64
+ # # a call to valid? will return false unless:
65
+ # # license_agreement is nil or "1"
66
+ # # and
67
+ # # terms_accepted is one of ["1",1,"true",true,"t"]
68
+ #
69
+ def validates_is_accepted(*fields)
70
+ opts = opts_from_validator_args(fields)
71
+ add_validator_to_context(opts, fields, DataMapper::Validate::AcceptanceValidator)
72
+ end
73
+
74
+ end # module ValidatesIsAccepted
75
+ end # module Validate
76
+ end # module DataMapper
@@ -0,0 +1,153 @@
1
+ module DataMapper
2
+ class Property
3
+ # for options_with_message
4
+ PROPERTY_OPTIONS << :message << :messages << :set
5
+ end
6
+
7
+ module Validate
8
+ module AutoValidate
9
+ # adds message for validator
10
+ def options_with_message(base_options, property, validator_name)
11
+ options = base_options.clone
12
+ opts = property.options
13
+ options[:message] = if opts[:messages]
14
+ if opts[:messages].is_a?(Hash) and msg = opts[:messages][validator_name]
15
+ msg
16
+ else
17
+ nil
18
+ end
19
+ elsif opts[:message]
20
+ opts[:message]
21
+ else
22
+ nil
23
+ end
24
+ options
25
+ end
26
+
27
+
28
+ ##
29
+ # Auto-generate validations for a given property. This will only occur
30
+ # if the option :auto_validation is either true or left undefined.
31
+ #
32
+ # @details [Triggers]
33
+ # Triggers that generate validator creation
34
+ #
35
+ # :nullable => false
36
+ # Setting the option :nullable to false causes a
37
+ # validates_presence_of validator to be automatically created on
38
+ # the property
39
+ #
40
+ # :size => 20 or :length => 20
41
+ # Setting the option :size or :length causes a validates_length_of
42
+ # validator to be automatically created on the property. If the
43
+ # value is a Integer the validation will set :maximum => value if
44
+ # the value is a Range the validation will set :within => value
45
+ #
46
+ # :format => :predefined / lambda / Proc
47
+ # Setting the :format option causes a validates_format_of
48
+ # validator to be automatically created on the property
49
+ #
50
+ # :set => ["foo", "bar", "baz"]
51
+ # Setting the :set option causes a validates_within
52
+ # validator to be automatically created on the property
53
+ #
54
+ # Integer type
55
+ # Using a Integer type causes a validates_is_number
56
+ # validator to be created for the property. integer_only
57
+ # is set to true
58
+ #
59
+ # BigDecimal or Float type
60
+ # Using a Integer type causes a validates_is_number
61
+ # validator to be created for the property. integer_only
62
+ # is set to false, and precision/scale match the property
63
+ #
64
+ #
65
+ # Messages
66
+ #
67
+ # :messages => {..}
68
+ # Setting :messages hash replaces standard error messages
69
+ # with custom ones. For instance:
70
+ # :messages => {:presence => "Field is required",
71
+ # :format => "Field has invalid format"}
72
+ # Hash keys are: :presence, :format, :length, :is_unique,
73
+ # :is_number, :is_primitive
74
+ #
75
+ # :message => "Some message"
76
+ # It is just shortcut if only one validation option is set
77
+ #
78
+ def auto_generate_validations(property)
79
+ property.options[:auto_validation] = true unless property.options.has_key?(:auto_validation)
80
+ return unless property.options[:auto_validation]
81
+
82
+ # a serial property is allowed to be nil too, because the
83
+ # value is set by the storage system
84
+ opts = { :allow_nil => property.nullable? || property.serial? }
85
+ opts[:context] = property.options[:validates] if property.options.has_key?(:validates)
86
+
87
+ # presence
88
+ unless opts[:allow_nil]
89
+ # validates_present property.name, opts
90
+ validates_present property.name, options_with_message(opts, property, :presence)
91
+ end
92
+
93
+ # length
94
+ if property.type == String
95
+ #len = property.length # XXX: maybe length should always return a Range, with the min defaulting to 1
96
+ len = property.options.fetch(:length, property.options.fetch(:size, DataMapper::Property::DEFAULT_LENGTH))
97
+ if len.is_a?(Range)
98
+ opts[:within] = len
99
+ else
100
+ opts[:maximum] = len
101
+ end
102
+ # validates_length property.name, opts
103
+ validates_length property.name, options_with_message(opts, property, :length)
104
+ end
105
+
106
+ # format
107
+ if property.options.has_key?(:format)
108
+ opts[:with] = property.options[:format]
109
+ # validates_format property.name, opts
110
+ validates_format property.name, options_with_message(opts, property, :format)
111
+ end
112
+
113
+ # uniqueness validator
114
+ if property.options.has_key?(:unique)
115
+ value = property.options[:unique]
116
+ if value.is_a?(Array) || value.is_a?(Symbol)
117
+ # validates_is_unique property.name, :scope => Array(value)
118
+ validates_is_unique property.name, options_with_message({:scope => Array(value)}, property, :is_unique)
119
+ elsif value.is_a?(TrueClass)
120
+ # validates_is_unique property.name
121
+ validates_is_unique property.name, options_with_message({}, property, :is_unique)
122
+ end
123
+ end
124
+
125
+ # within validator
126
+ if property.options.has_key?(:set)
127
+ validates_within property.name, options_with_message({:set => property.options[:set]}, property, :within)
128
+ end
129
+
130
+ # numeric validator
131
+ if Integer == property.type
132
+ opts[:integer_only] = true
133
+ # validates_is_number property.name, opts
134
+ validates_is_number property.name, options_with_message(opts, property, :is_number)
135
+ elsif BigDecimal == property.type || Float == property.type
136
+ opts[:precision] = property.precision
137
+ opts[:scale] = property.scale
138
+ # validates_is_number property.name, opts
139
+ validates_is_number property.name, options_with_message(opts, property, :is_number)
140
+ else
141
+ # We only need this in the case we don't already
142
+ # have a numeric validator, because otherwise
143
+ # it will cause duplicate validation errors
144
+ unless property.custom?
145
+ # validates_is_primitive property.name, opts
146
+ validates_is_primitive property.name, options_with_message(opts, property, :is_primitive)
147
+ end
148
+ end
149
+ end
150
+
151
+ end # module AutoValidate
152
+ end # module Validate
153
+ end # module DataMapper