pad_utils 1.5.3 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88233a7f2ee90152f287057ea132c79574d034de
4
- data.tar.gz: d4331a94477f596e497ece4fdf4180da70279889
3
+ metadata.gz: 58ba3d3b2eea40a399913261cc63c6fa6673fd95
4
+ data.tar.gz: 9d80db089a6ffbb0f1152abda5f30a027da35ec4
5
5
  SHA512:
6
- metadata.gz: 8a9d3c926a9ecc25aeb2821b3332df9b606b2f419d58d156e20118ba2ef76aa896a93142aa9bab4d86ea675115c37eb6d28e79e96ad7aa6be0fab7ee7966d145
7
- data.tar.gz: 6823c2b5297f753c5ac28f4db637c2fadb80285f3310b8138cdc3401aa669ce8ed924373406542c9bc7c604a3498c0b506954f91226099613ae43fdc0b626f56
6
+ metadata.gz: 4947d63a00f1f86f74a5c6adedc4173d9dfc8d413e8714dd3bcb520077f6363379044156ea5b982db035a18b98f4450e7b92daf6adb64e6b455e25c2b7db41bc
7
+ data.tar.gz: 185c906198551cbe36130f59d604c76227c6119173c1205c11a24691152015867b4474433b1699507f61ca9e925ab7a03dfa93071245f4ff4a865f9737630a4b
data/lib/pad_utils.rb CHANGED
@@ -6,6 +6,7 @@ require_relative "pad_utils/pad_logger"
6
6
  require_relative "pad_utils/pad_menu"
7
7
  require_relative "pad_utils/pad_json"
8
8
  require_relative "pad_utils/pad_color"
9
+ require_relative "pad_utils/pad_code"
9
10
 
10
11
  # Main namespace for PadUtils.
11
12
  #
@@ -0,0 +1,31 @@
1
+ module PadUtils
2
+
3
+ # Converts a Ruby filename to a class name
4
+ #
5
+ # @param file [String] the file path and name. Must be a Ruby (`.rb`) file.
6
+ # @return [String] the class name
7
+ # @example
8
+ # PadUtils.filename_to_class("foo_bar.rb") # => FooBar
9
+ def self.filename_to_classname(file)
10
+ filename = File.basename file
11
+ filename_no_ext = filename.gsub(".rb", "")
12
+ class_name = PadUtils.camel_case(filename_no_ext)
13
+ class_name
14
+ end
15
+
16
+ # Create a class from a filename.
17
+ #
18
+ # **Don't forget to `require_relative` the file**
19
+ #
20
+ # @param file [String] the file path and name. Must be a Ruby (`.rb`) file.
21
+ # @return [Object] the class generated from the file
22
+ # @example
23
+ # require_relative 'somepath/foo_bar'
24
+ # foobar = PadUtils.filename_to_class("somepath/foo_bar.rb")
25
+ # f = foobar.new
26
+ def self.filename_to_class(file)
27
+ class_name = PadUtils.filename_to_classname(file)
28
+ Object.const_get(class_name)
29
+ end
30
+
31
+ end
@@ -90,6 +90,27 @@ module PadUtils
90
90
  PadUtils.log("Error replacing #{old_text} in #{file} with #{new_text}", e)
91
91
  end
92
92
 
93
+ # Replaces multiple strings in a file.
94
+ #
95
+ # Will log errors using {PadUtils.log PadUtils.log}.
96
+ #
97
+ # @param file [String] the file path and name
98
+ # @param values [Array] the array containing hashes of `key`, `value`
99
+ # @return [Void] nothing
100
+ # @example
101
+ # values = [
102
+ # {key: /REPLACE_ME/, value: "REPLACED"},
103
+ # {key: /REPLACE_ALSO/, value: "MODIFIED AS WELL"}
104
+ # ]
105
+ # PadUtils.replace_keys_in_file("example.txt", values)
106
+ def self.replace_keys_in_file(file, values)
107
+ values.each do |value|
108
+ PadUtils.replace_in_file(file, value[:key], value[:value])
109
+ end
110
+ rescue Exception => e
111
+ PadUtils.log("Error replacing multiple keys in #{file}", e)
112
+ end
113
+
93
114
  # Gets a value from a Ruby config file.
94
115
  #
95
116
  # *This method is some kind of UFO but it is heavily used in Padstone.
@@ -50,4 +50,32 @@ module PadUtils
50
50
  PadUtils.log("Error in readable_stamp_to_time", e)
51
51
  end
52
52
 
53
+ # Returns the interval of time between two Time objects.
54
+ #
55
+ # @param start_time [Time] the starting time
56
+ # @param end_time [Time] the ending time
57
+ # @param unit [Symbol] the unit to use from `:seconds`, `:minutes`, `:hours`, `:days`
58
+ # @return [Float] the interval, rounded at 3 digits
59
+ # @example
60
+ # start = Time.now - 30
61
+ # finish = Time.now
62
+ # PadUtils.interval(start, finish, :minutes) # => 0.5
63
+ def self.interval(start_time, end_time, unit = :seconds)
64
+ result = -1
65
+ inter = (end_time - start_time)
66
+
67
+ if unit == :minutes
68
+ result = inter / 60.0
69
+ elsif unit == :hours
70
+ result = (inter / 60) / 60
71
+ elsif unit == :days
72
+ result = ((inter / 60) / 60) / 24
73
+ else
74
+ result = inter
75
+ end
76
+
77
+ result.round(3)
78
+
79
+ end
80
+
53
81
  end
@@ -1,4 +1,4 @@
1
1
  module PadUtils
2
2
  # PadUtils version number
3
- VERSION = "1.5.3"
3
+ VERSION = "1.6.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pad_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nico Schuele
@@ -25,6 +25,7 @@ files:
25
25
  - Rakefile
26
26
  - bin/padutils
27
27
  - lib/pad_utils.rb
28
+ - lib/pad_utils/pad_code.rb
28
29
  - lib/pad_utils/pad_color.rb
29
30
  - lib/pad_utils/pad_files.rb
30
31
  - lib/pad_utils/pad_json.rb