Class: String
Instance Method Summary collapse
-
#as_date ⇒ Object
Parses the string as a date time object.
-
#as_json ⇒ Object
Parses the string as JSON and returns an
OpenStruct. -
#as_timestamp ⇒ Object
Parses the string as a date time object and returns it as a unix timestamp.
-
#content(with: nil) ⇒ Object
Interprets the string as a file path and reads its content.
-
#exists? ⇒ Boolean
Interprets the string as a file path and returns
trueif is exists,falseotherwise. -
#file_size ⇒ Object
Interprets the string as a file path and returns its size.
-
#remove! ⇒ Object
Interprets the string as a file path and removes it.
-
#trim(size = 50) ⇒ Object
Trims the string to the given length and adds
...at the end. -
#with(mapping) ⇒ Object
Replaces placeholder in style of #{placeholder} with the given
Hash.
Instance Method Details
#as_date ⇒ Object
Parses the string as a date time object
17 18 19 |
# File 'lib/spectre/helpers.rb', line 17 def as_date DateTime.parse(self) end |
#as_json ⇒ Object
Parses the string as JSON and returns an OpenStruct
10 11 12 |
# File 'lib/spectre/helpers.rb', line 10 def as_json JSON.parse(self, object_class: OpenStruct) end |
#as_timestamp ⇒ Object
Parses the string as a date time object and returns it as a unix timestamp
24 25 26 |
# File 'lib/spectre/helpers.rb', line 24 def DateTime.parse(self).to_time.to_i end |
#content(with: nil) ⇒ Object
Interprets the string as a file path and reads its content
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/spectre/helpers.rb', line 54 def content with: nil raise "'#{self}' is not a file path, or the file does not exist." unless File.exist? self file_content = File.read(self) if with file_content.with(with) else file_content end end |
#exists? ⇒ Boolean
Interprets the string as a file path and returns true if is exists, false otherwise
78 79 80 |
# File 'lib/spectre/helpers.rb', line 78 def exists? File.exist? self end |
#file_size ⇒ Object
Interprets the string as a file path and returns its size
69 70 71 72 73 |
# File 'lib/spectre/helpers.rb', line 69 def file_size raise "'#{self}' is not a file path, or the file does not exist." unless File.exist? self File.size(self) end |
#remove! ⇒ Object
Interprets the string as a file path and removes it
85 86 87 88 89 |
# File 'lib/spectre/helpers.rb', line 85 def remove! raise "'#{self}' is not a file path, or the file does not exist." unless File.exist? self File.delete self end |
#trim(size = 50) ⇒ Object
Trims the string to the given length and adds ... at the end
45 46 47 48 49 |
# File 'lib/spectre/helpers.rb', line 45 def trim size = 50 return "#{self[0..(size - 4)]}..." if (length + 3) > size self end |
#with(mapping) ⇒ Object
Replaces placeholder in style of #{placeholder} with the given Hash
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/spectre/helpers.rb', line 31 def with mapping return self unless mapping.is_a? Hash new_string = self mapping.each do |key, value| new_string = new_string.gsub('#{' + key.to_s + '}', value.to_s) end new_string end |