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 +4 -4
- data/lib/pad_utils.rb +1 -0
- data/lib/pad_utils/pad_code.rb +31 -0
- data/lib/pad_utils/pad_text.rb +21 -0
- data/lib/pad_utils/pad_time.rb +28 -0
- data/lib/pad_utils/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58ba3d3b2eea40a399913261cc63c6fa6673fd95
|
4
|
+
data.tar.gz: 9d80db089a6ffbb0f1152abda5f30a027da35ec4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4947d63a00f1f86f74a5c6adedc4173d9dfc8d413e8714dd3bcb520077f6363379044156ea5b982db035a18b98f4450e7b92daf6adb64e6b455e25c2b7db41bc
|
7
|
+
data.tar.gz: 185c906198551cbe36130f59d604c76227c6119173c1205c11a24691152015867b4474433b1699507f61ca9e925ab7a03dfa93071245f4ff4a865f9737630a4b
|
data/lib/pad_utils.rb
CHANGED
@@ -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
|
data/lib/pad_utils/pad_text.rb
CHANGED
@@ -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.
|
data/lib/pad_utils/pad_time.rb
CHANGED
@@ -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
|
data/lib/pad_utils/version.rb
CHANGED
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.
|
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
|