dm-types 0.9.11 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/{History.txt → History.rdoc} +5 -4
  2. data/LICENSE +16 -14
  3. data/Manifest.txt +13 -3
  4. data/{README.txt → README.rdoc} +0 -0
  5. data/Rakefile +3 -4
  6. data/TODO +74 -0
  7. data/lib/dm-types.rb +15 -18
  8. data/lib/dm-types/bcrypt_hash.rb +1 -4
  9. data/lib/dm-types/comma_separated_list.rb +31 -0
  10. data/lib/dm-types/csv.rb +12 -18
  11. data/lib/dm-types/enum.rb +18 -15
  12. data/lib/dm-types/epoch_time.rb +6 -11
  13. data/lib/dm-types/file_path.rb +5 -4
  14. data/lib/dm-types/flag.rb +14 -13
  15. data/lib/dm-types/ip_address.rb +1 -0
  16. data/lib/dm-types/json.rb +5 -11
  17. data/lib/dm-types/regexp.rb +2 -3
  18. data/lib/dm-types/slug.rb +8 -17
  19. data/lib/dm-types/uri.rb +8 -14
  20. data/lib/dm-types/uuid.rb +9 -15
  21. data/lib/dm-types/version.rb +1 -1
  22. data/lib/dm-types/yaml.rb +4 -3
  23. data/spec/fixtures/article.rb +39 -0
  24. data/spec/fixtures/bookmark.rb +27 -0
  25. data/spec/fixtures/invention.rb +9 -0
  26. data/spec/fixtures/network_node.rb +40 -0
  27. data/spec/fixtures/person.rb +29 -0
  28. data/spec/fixtures/software_package.rb +37 -0
  29. data/spec/fixtures/ticket.rb +25 -0
  30. data/spec/fixtures/tshirt.rb +28 -0
  31. data/spec/integration/bcrypt_hash_spec.rb +37 -55
  32. data/spec/integration/comma_separated_list_spec.rb +89 -0
  33. data/spec/integration/enum_spec.rb +62 -42
  34. data/spec/integration/file_path_spec.rb +149 -17
  35. data/spec/integration/flag_spec.rb +56 -25
  36. data/spec/integration/ip_address_spec.rb +142 -17
  37. data/spec/integration/json_spec.rb +60 -17
  38. data/spec/integration/slug_spec.rb +39 -40
  39. data/spec/integration/uri_spec.rb +126 -21
  40. data/spec/integration/uuid_spec.rb +84 -30
  41. data/spec/integration/yaml_spec.rb +55 -25
  42. data/spec/shared/identity_function_group.rb +5 -0
  43. data/spec/spec.opts +1 -0
  44. data/spec/spec_helper.rb +49 -17
  45. data/spec/unit/bcrypt_hash_spec.rb +107 -64
  46. data/spec/unit/csv_spec.rb +111 -27
  47. data/spec/unit/enum_spec.rb +128 -87
  48. data/spec/unit/epoch_time_spec.rb +57 -32
  49. data/spec/unit/file_path_spec.rb +55 -34
  50. data/spec/unit/flag_spec.rb +102 -65
  51. data/spec/unit/ip_address_spec.rb +90 -40
  52. data/spec/unit/json_spec.rb +108 -41
  53. data/spec/unit/regexp_spec.rb +47 -17
  54. data/spec/unit/uri_spec.rb +57 -46
  55. data/spec/unit/yaml_spec.rb +91 -45
  56. data/tasks/install.rb +1 -1
  57. data/tasks/spec.rb +4 -4
  58. metadata +25 -32
  59. data/lib/dm-types/serial.rb +0 -8
data/lib/dm-types/flag.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  module DataMapper
2
2
  module Types
3
- class Flag < DataMapper::Type(Integer)
3
+ class Flag < Type
4
+ primitive Integer
5
+
4
6
  def self.inherited(target)
5
- target.instance_variable_set("@primitive", self.primitive)
7
+ target.instance_variable_set('@primitive', self.primitive)
6
8
  end
7
9
 
8
10
  def self.flag_map
@@ -18,7 +20,7 @@ module DataMapper
18
20
  type.flag_map = {}
19
21
 
20
22
  flags.each_with_index do |flag, i|
21
- type.flag_map[2 ** i] = flag
23
+ type.flag_map[i] = flag
22
24
  end
23
25
 
24
26
  type
@@ -29,13 +31,13 @@ module DataMapper
29
31
  end
30
32
 
31
33
  def self.load(value, property)
34
+ return [] if value.nil? || value <= 0
35
+
32
36
  begin
33
37
  matches = []
34
38
 
35
- return [] if value.nil? || (value <= 0)
36
- 0.upto((Math.log(value) / Math.log(2)).ceil) do |i|
37
- pow = 2 ** i
38
- matches << flag_map[pow] if value & pow == pow
39
+ 0.upto(flag_map.size - 1) do |i|
40
+ matches << flag_map[i] if value[i] == 1
39
41
  end
40
42
 
41
43
  matches.compact
@@ -46,16 +48,15 @@ module DataMapper
46
48
 
47
49
  def self.dump(value, property)
48
50
  return if value.nil?
49
- flags = value.is_a?(Array) ? value : [value]
50
- flags.map!{ |f| f.to_sym }
51
- flag_map.invert.values_at(*flags.flatten).compact.inject(0) { |sum, i| sum + i }
51
+ flags = Array(value).map { |flag| flag.to_sym }.flatten
52
+ flag_map.invert.values_at(*flags).compact.inject(0) { |sum, i| sum += 1 << i }
52
53
  end
53
54
 
54
55
  def self.typecast(value, property)
55
56
  case value
56
- when nil then nil
57
- when Array then value.map {|v| v.to_sym}
58
- else value.to_sym
57
+ when nil then nil
58
+ when Array then value.map {|v| v.to_sym}
59
+ else value.to_sym
59
60
  end
60
61
  end
61
62
  end # class Flag
@@ -4,6 +4,7 @@ module DataMapper
4
4
  module Types
5
5
  class IPAddress < DataMapper::Type
6
6
  primitive String
7
+ length 16
7
8
 
8
9
  def self.load(value, property)
9
10
  if value.nil?
data/lib/dm-types/json.rb CHANGED
@@ -1,17 +1,11 @@
1
- begin
2
- gem "json"
3
- require "json/ext"
4
- rescue LoadError
5
- gem "json_pure"
6
- require "json/pure"
7
- end
8
-
1
+ require 'json'
9
2
 
10
3
  module DataMapper
11
4
  module Types
12
5
  class Json < DataMapper::Type
13
- primitive Text
14
- lazy true
6
+ primitive String
7
+ length 65535
8
+ lazy true
15
9
 
16
10
  def self.load(value, property)
17
11
  if value.nil?
@@ -19,7 +13,7 @@ module DataMapper
19
13
  elsif value.is_a?(String)
20
14
  ::JSON.load(value)
21
15
  else
22
- raise ArgumentError, '+value+ must be nil or a String'
16
+ raise ArgumentError.new("+value+ of a property of JSON type must be nil or a String")
23
17
  end
24
18
  end
25
19
 
@@ -8,12 +8,11 @@ module DataMapper
8
8
  end
9
9
 
10
10
  def self.dump(value, property)
11
- return nil if value.nil?
12
- value.source
11
+ value.source unless value.nil?
13
12
  end
14
13
 
15
14
  def self.typecast(value, property)
16
- value.kind_of?(::Regexp) ? value : load(value, property)
15
+ load(value, property)
17
16
  end
18
17
  end
19
18
  end
data/lib/dm-types/slug.rb CHANGED
@@ -1,37 +1,28 @@
1
- require 'iconv'
1
+ require 'stringex'
2
2
 
3
3
  module DataMapper
4
4
  module Types
5
5
  class Slug < DataMapper::Type
6
6
  primitive String
7
- size 65535
7
+ length 65535
8
8
 
9
9
  def self.load(value, property)
10
10
  value
11
11
  end
12
12
 
13
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)
14
+ return if value.nil?
15
+
16
+ if value.respond_to?(:to_str)
17
+ escape(value.to_str)
19
18
  else
20
- raise ArgumentError.new("+value+ must be nil or a String")
19
+ raise ArgumentError, '+value+ must be nil or respond to #to_str'
21
20
  end
22
21
  end
23
22
 
24
- # Hugs and kisses to Rick Olsons permalink_fu for the escape method
25
23
  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
24
+ string.to_url
33
25
  end
34
-
35
26
  end # class Slug
36
27
  end # module Types
37
28
  end # module DataMapper
data/lib/dm-types/uri.rb CHANGED
@@ -1,31 +1,25 @@
1
- require 'rubygems'
2
-
3
- gem 'addressable', '~>2.0.2'
4
1
  require 'addressable/uri'
5
2
 
6
3
  module DataMapper
7
4
  module Types
8
5
  class URI < DataMapper::Type
9
6
  primitive String
7
+ length 2000
8
+
9
+ # Maximum length chosen based on recommendation:
10
+ # http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-an-url
10
11
 
11
12
  def self.load(value, property)
12
13
  Addressable::URI.parse(value)
13
14
  end
14
15
 
15
16
  def self.dump(value, property)
16
- return nil if value.nil?
17
- value.to_s
17
+ value.to_s unless value.nil?
18
18
  end
19
19
 
20
20
  def self.typecast(value, property)
21
- if value.kind_of?(Addressable::URI)
22
- value
23
- elsif value.nil?
24
- load(nil, property)
25
- else
26
- load(value.to_s, property)
27
- end
21
+ load(value, property)
28
22
  end
29
23
  end # class URI
30
- end # module Types
31
- end # module DataMapper
24
+ end # module Types
25
+ end # module DataMapper
data/lib/dm-types/uuid.rb CHANGED
@@ -1,7 +1,4 @@
1
- require 'rubygems'
2
-
3
- gem 'uuidtools', '~>1.0.7'
4
- require 'uuidtools'
1
+ require 'uuidtools' # must be ~>2.0
5
2
 
6
3
  module DataMapper
7
4
  module Types
@@ -42,26 +39,23 @@ module DataMapper
42
39
  #
43
40
  class UUID < DataMapper::Type
44
41
  primitive String
45
- size 36
42
+ length 36
46
43
 
47
44
  def self.load(value, property)
48
- return nil if value.nil?
49
- ::UUID.parse(value)
45
+ UUIDTools::UUID.parse(value) unless value.nil?
50
46
  end
51
47
 
52
48
  def self.dump(value, property)
53
- return nil if value.nil?
54
- value.to_s
49
+ value.to_s unless value.nil?
55
50
  end
56
51
 
57
52
  def self.typecast(value, property)
58
- value.kind_of?(::UUID) ? value : load(value, property)
53
+ if value.kind_of?(UUIDTools::UUID)
54
+ value
55
+ else
56
+ load(value, property)
57
+ end
59
58
  end
60
-
61
- #::DataMapper::Property::TYPES << self
62
- #if defined? DataMapper::Adapters::PostgresAdapter
63
- # DataMapper::Adapters::PostgresAdapter.type_map.map(self).to('UUID').with(:size => nil)
64
- #end
65
59
  end
66
60
  end
67
61
  end
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Types
3
- VERSION = '0.9.11'
3
+ VERSION = '0.10.0'.freeze
4
4
  end
5
5
  end
data/lib/dm-types/yaml.rb CHANGED
@@ -3,8 +3,9 @@ require 'yaml'
3
3
  module DataMapper
4
4
  module Types
5
5
  class Yaml < DataMapper::Type
6
- primitive Text
7
- lazy true
6
+ primitive String
7
+ length 65535
8
+ lazy true
8
9
 
9
10
  def self.load(value, property)
10
11
  if value.nil?
@@ -12,7 +13,7 @@ module DataMapper
12
13
  elsif value.is_a?(String)
13
14
  ::YAML.load(value)
14
15
  else
15
- raise ArgumentError.new("+value+ must be nil or a String")
16
+ raise ArgumentError.new("+value+ of a property of YAML type must be nil or a String")
16
17
  end
17
18
  end
18
19
 
@@ -0,0 +1,39 @@
1
+ module DataMapper
2
+ module Types
3
+ module Fixtures
4
+
5
+ class Article
6
+ #
7
+ # Behaviors
8
+ #
9
+
10
+ include ::DataMapper::Resource
11
+
12
+ #
13
+ # Properties
14
+ #
15
+
16
+ property :id, Serial
17
+
18
+ property :title, String, :length => 255
19
+ property :body, Text
20
+
21
+ property :created_at, DateTime
22
+ property :updated_at, DateTime
23
+ property :published_at, DateTime
24
+
25
+ property :slug, Slug
26
+
27
+ #
28
+ # Hooks
29
+ #
30
+
31
+ before :valid? do
32
+ self.slug = self.title
33
+ end
34
+
35
+ auto_migrate!
36
+ end # Article
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module DataMapper
2
+ module Types
3
+ module Fixtures
4
+
5
+ class Bookmark
6
+ #
7
+ # Behaviors
8
+ #
9
+
10
+ include ::DataMapper::Resource
11
+
12
+ #
13
+ # Properties
14
+ #
15
+
16
+ property :id, Serial
17
+
18
+ property :title, String, :length => 255
19
+ property :shared, Boolean
20
+ property :uri, URI
21
+ property :tags, Yaml
22
+
23
+ auto_migrate!
24
+ end # Bookmark
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module DataMapper
2
+ module Types
3
+ module Fixtures
4
+
5
+ Invention = Struct.new(:name)
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module DataMapper
2
+ module Types
3
+ module Fixtures
4
+
5
+ class NetworkNode
6
+ #
7
+ # Behaviors
8
+ #
9
+
10
+ include ::DataMapper::Resource
11
+
12
+ #
13
+ # Properties
14
+ #
15
+
16
+ property :id, Serial
17
+ property :ip_address, IPAddress
18
+ property :cidr_subnet_bits, Integer
19
+ property :node_uuid, UUID
20
+
21
+ #
22
+ # API
23
+ #
24
+
25
+ alias uuid node_uuid
26
+ alias uuid= node_uuid=
27
+
28
+ def runs_ipv6?
29
+ self.ip_address.ipv6?
30
+ end
31
+
32
+ def runs_ipv4?
33
+ self.ip_address.ipv4?
34
+ end
35
+
36
+ auto_migrate!
37
+ end # NetworkNode
38
+ end # Fixtures
39
+ end # Types
40
+ end # DataMapper
@@ -0,0 +1,29 @@
1
+ module DataMapper
2
+ module Types
3
+ module Fixtures
4
+
5
+ class Person
6
+ #
7
+ # Behaviors
8
+ #
9
+
10
+ include DataMapper::Resource
11
+
12
+ #
13
+ # Properties
14
+ #
15
+
16
+ property :id, Serial
17
+ property :name, String
18
+ property :positions, Json
19
+ property :inventions, Yaml
20
+
21
+ property :interests, CommaSeparatedList
22
+
23
+ property :password, BCryptHash
24
+
25
+ auto_migrate!
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ module DataMapper
2
+ module Types
3
+ module Fixtures
4
+
5
+ class SoftwarePackage
6
+ #
7
+ # Behaviors
8
+ #
9
+
10
+ include ::DataMapper::Resource
11
+
12
+ #
13
+ # Properties
14
+ #
15
+
16
+ property :id, Serial
17
+ without_auto_validations do
18
+ property :node_number, Integer, :index => true
19
+
20
+ property :source_path, FilePath
21
+ property :destination_path, FilePath
22
+
23
+ property :product, String
24
+ property :version, String
25
+ property :released_at, DateTime
26
+
27
+ property :security_update, Boolean
28
+
29
+ property :installed_at, DateTime
30
+ property :installed_by, String
31
+ end
32
+
33
+ auto_migrate!
34
+ end # SoftwarePackage
35
+ end # Fixtures
36
+ end # Types
37
+ end # DataMapper