forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rb
78 lines (70 loc) · 1.94 KB
/
util.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'shoulda/matchers/util/word_wrap'
module Shoulda
module Matchers
# @private
module Util
def self.deconstantize(path)
if defined?(ActiveSupport::Inflector) &&
ActiveSupport::Inflector.respond_to?(:deconstantize)
ActiveSupport::Inflector.deconstantize(path)
else
path.to_s[0...(path.to_s.rindex('::') || 0)]
end
end
def self.safe_constantize(camel_cased_word)
if defined?(ActiveSupport::Inflector) &&
ActiveSupport::Inflector.respond_to?(:safe_constantize)
ActiveSupport::Inflector.safe_constantize(camel_cased_word)
else
begin
camel_cased_word.constantize
rescue NameError
nil
end
end
end
def self.indent(string, width)
indentation = ' ' * width
string.split(/[\n\r]/).map { |line| indentation + line }.join("\n")
end
def self.a_or_an(next_word)
if next_word =~ /\A[aeiou]/i
"an #{next_word}"
else
"a #{next_word}"
end
end
def self.inspect_value(value)
"‹#{value.inspect}›"
end
def self.inspect_values(values)
values.map { |value| inspect_value(value) }
end
def self.inspect_range(range)
"#{inspect_value(range.first)} to #{inspect_value(range.last)}"
end
def self.dummy_value_for(column_type, array: false)
if array
[dummy_value_for(column_type, array: false)]
else
case column_type
when :integer
0
when :date
Date.new(2100, 1, 1)
when :datetime, :timestamp
DateTime.new(2100, 1, 1)
when :time
Time.new(2100, 1, 1)
when :uuid
SecureRandom.uuid
when :boolean
true
else
'dummy value'
end
end
end
end
end
end