pad_utils 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 670f14eb8e988c3a250ef6968c75d43255afe3dc
4
- data.tar.gz: 794a1ff4059c81f06cf53cc4d707c73146493ceb
3
+ metadata.gz: 46369e29476640a59df9f7c0a8ac5864bbc24361
4
+ data.tar.gz: c5a3ac14efe6a8d7700c0afc3e0420ecbe986cc2
5
5
  SHA512:
6
- metadata.gz: e9967be3fb3dda66afa9328fb4702f13512a14f14557a60432a5bcc638410e76cf4f421b54b776ed621c43beca1c2e2c0a502bc766e00105d83aae9e917580a8
7
- data.tar.gz: cc4ec2ca116fc036c0786ef5aaf1159ff715dd59d44656dce9b7ad62842875818ee9313a5435c66206cef7535598a67206b4998130d25d49a876a301748dcf1b
6
+ metadata.gz: 2da2c9f8d35ec72b2246a398f4560e8b94fe010190c6f7a61f3b7b9680a5f76772145e2474e040c4cf8fc407a504ea3aac9a99b6e9e5e3d880fb076e575c880a
7
+ data.tar.gz: 0fa3dd9e8a963b0e8fff13bae15a1816fc6a8a0f9ed1a479784bf40b65573f31cf9fb431764387cc50b65d4afe7026516ca1ec810233f3da3449770d750baaad
@@ -90,8 +90,113 @@ module PadUtils
90
90
  PadUtils.log("Error replacing #{old_text} in #{file} with #{new_text}", e)
91
91
  end
92
92
 
93
- # TODO: A method to get the value of an option. E.g. get :foo from Config.Bar = :foo inside a file.
94
- # TODO: A method to set the value of an option in a file. Change the value if it exists, create it if it doesn't.
93
+ # Gets a value from a Ruby config file.
94
+ #
95
+ # *This method is some kind of UFO but it is heavily used in Padstone.
96
+ # It is made to retrieve the value inside a Rails config file or initializer
97
+ # such as getting the value of `config.eager_load` in `production.rb`.
98
+ # Everything gets returned as a string or as nil if not found.*
99
+ #
100
+ # Will log errors using {PadUtils.log PadUtils.log}.
101
+ #
102
+ # @param key [String] the config key to look for
103
+ # @param file [String] the file path and name containing the key
104
+ # @return [String, nil] the value returned as a string or nil
105
+ # @example
106
+ # PadUtils.get_config_value("config.eager_load", "production.rb") # => 'true'
107
+ def self.get_config_value(key, file)
108
+ content = PadUtils.get_file_content(file)
109
+ content.each_line do |line|
110
+ if line.strip.start_with? key
111
+ return line.gsub(key, "").gsub("=","").strip
112
+ end
113
+ end
114
+ nil
115
+ rescue Exception => e
116
+ PadUtils.log("Error in get_config_value", e)
117
+ end
118
+
119
+ # Sets a value in a Ruby config file.
120
+ #
121
+ # *This is another method typically used by Padstone to write
122
+ # new config values in Rails config files such as `production.rb` or
123
+ # overwrite existing ones.*
124
+ #
125
+ # Will log errors using {PadUtils.log PadUtils.log}.
126
+ #
127
+ # @param key [String] the config key to find (or create)
128
+ # @param value [String] the value to set
129
+ # @param file [String] the file path and name of the file to overwrite
130
+ # @param comment [String] the optional comment to add before the key
131
+ # @return [Void] nothing
132
+ # @example
133
+ # key = "config.assets.digest"
134
+ # value = "false"
135
+ # file = "production.rb"
136
+ # PadUtils.set_config_value(key, value, file, "Overwritten with PadUtils")
137
+ def self.set_config_value(key, value, file, comment = nil)
138
+ # read the config file
139
+ content = PadUtils.get_file_content(file)
140
+
141
+ # set some vars
142
+ found = false
143
+ new_content = ""
144
+
145
+ # for each line in the file, check if one contains the key.
146
+ # If the key is found, get its position so we can indent the
147
+ # config line properly.
148
+ content.each_line do |line|
149
+ position = line.index(key)
150
+ if position != nil
151
+ new_line = ""
152
+ (0..position - 1).each do |p|
153
+ new_line << " "
154
+ end
155
+ if comment != nil
156
+ new_content << "#{new_line}# #{comment}\n"
157
+ end
158
+ new_content << "#{new_line}#{key} = #{value}\n"
159
+ found = true
160
+ else
161
+ new_content << line
162
+ end
163
+ end
164
+
165
+ # If the config key was not found, we'll insert it before the last end,
166
+ # indented with two spaces
167
+ if !found
168
+ PadUtils.insert_before_last(original: file, tag: 'end', text: "\n\n # #{comment == nil ? key : comment}\n #{key} = #{value}\n")
169
+ else
170
+ PadUtils.write_to_file(file, new_content)
171
+ end
172
+ rescue Exception => e
173
+ PadUtils.log("Error in set_config_value", e)
174
+ end
175
+
176
+ # Replaces a line in a file containing a specific value.
177
+ #
178
+ # Will log errors using {PadUtils.log PadUtils.log}.
179
+ #
180
+ # @param value [String] the value to search for in a line
181
+ # @param in_file [String] the file path and name where to search
182
+ # @param new_value [String] the value replacing the line
183
+ # @return [Void] nothing
184
+ # @example
185
+ # PadUtils.replace_line_containing("Config.port", in_file: "config.rb", new_value: "Config.port = 232")
186
+ def self.replace_line_containing(value, in_file: nil, new_value: nil)
187
+ content = PadUtils.get_file_content(in_file)
188
+ new_content = ""
189
+ content.each_line do |line|
190
+ if line.include? value
191
+ new_content << "#{new_value}\n"
192
+ else
193
+ new_content << line
194
+ end
195
+ end
196
+ PadUtils.write_to_file(in_file, new_content)
197
+ rescue Exception => e
198
+ PadUtils.log("Error in replace_line_containing", e)
199
+ end
95
200
 
96
201
  # Inserts text before the first occurence of a string.
97
202
  #
@@ -1,4 +1,4 @@
1
1
  module PadUtils
2
2
  # PadUtils version number
3
- VERSION = "1.3.1"
3
+ VERSION = "1.4.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.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nico Schuele