Module: Shoulda::Matchers::Util
- Defined in:
- lib/shoulda/matchers/util.rb
Constant Summary collapse
- MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY =
500
Class Method Summary collapse
- .a_or_an(next_word) ⇒ Object
- .deconstantize(path) ⇒ Object
- .dummy_value_for(column_type, array: false) ⇒ Object
- .indent(string, width) ⇒ Object
- .inspect_hash(hash) ⇒ Object
- .inspect_range(range) ⇒ Object
- .inspect_value(value) ⇒ Object
- .inspect_values(values) ⇒ Object
- .safe_constantize(camel_cased_word) ⇒ Object
Class Method Details
.a_or_an(next_word) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/shoulda/matchers/util.rb', line 42 def self.a_or_an(next_word) if next_word =~ /\A[aeiou]/i && next_word != 'unique' "an #{next_word}" else "a #{next_word}" end end |
.deconstantize(path) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/shoulda/matchers/util.rb', line 9 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 |
.dummy_value_for(column_type, array: false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/shoulda/matchers/util.rb', line 88 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(2000, 1, 1) when :uuid SecureRandom.uuid when :boolean true else 'dummy value' end end end |
.indent(string, width) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/shoulda/matchers/util.rb', line 35 def self.indent(string, width) return if !string indentation = ' ' * width string.split(/[\n\r]/).map { |line| indentation + line }.join("\n") end |
.inspect_hash(hash) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/shoulda/matchers/util.rb', line 74 def self.inspect_hash(hash) output = '‹{' output << hash.map { |key, value| if key.is_a?(Symbol) "#{key}: #{value.inspect}" else "#{key.inspect} => #{value.inspect}" end }.join(', ') output << '}›' end |
.inspect_range(range) ⇒ Object
70 71 72 |
# File 'lib/shoulda/matchers/util.rb', line 70 def self.inspect_range(range) "#{inspect_value(range.first)} to #{inspect_value(range.last)}" end |
.inspect_value(value) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/shoulda/matchers/util.rb', line 50 def self.inspect_value(value) case value when Hash inspect_hash(value) when Range inspect_range(value) else inspected_value = value.inspect if inspected_value.length > MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY "‹#{inspected_value[0, MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY]}...›" else "‹#{inspected_value}›" end end end |
.inspect_values(values) ⇒ Object
66 67 68 |
# File 'lib/shoulda/matchers/util.rb', line 66 def self.inspect_values(values) values.map { |value| inspect_value(value) } end |
.safe_constantize(camel_cased_word) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/shoulda/matchers/util.rb', line 20 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 |