Is there a way to prevent Guard to run all the watchers that match a file structure but only the first one?
I basically need to instantiate a different object if the file copied in my root folder has a specific structure or not. For instance, if the file name matches \d{2}-\d{6}_\d{5}_\d+_\d+.csv I need to instances object A while for all the other .cvs files object B.
As first attempt I was trying to use negative lookbehind but due to lookbehind limitations it looks like I cannot do that.
So, now I'm trying to force Guard to execute only the first watcher that matches.
My Guardfile looks like
guard :my_csv_files do
watch(%r{^\d{2}-\d{6}_\d{5}_\d+_\d+.csv$})
end
guard :others_csv_files do
watch(%r{^.+.csv$})
end
Thanks
This has been available for a while, but was never documented. (Until today).
There's a :first_match option, which does exactly what you want:
https://github.com/guard/guard/wiki/Guardfile-DSL---Configuring-Guard#guard
Related
To debug / reporting purpose, before a refactor, we need to print the current file name in a rule refactor method.
Here we do a LOT of highly specific filtering, so I need to dump filenames where filter passes BEFORE doing any changes, so rector reporting will not output nothing.
Can a rector rule access current parsed file name?
Found !
in the rule we can use
$this->file->getFilePath()
Also, in the refactor method we can also see line of current node
$node->getLine()
I found an unexpected issue while attempting to load a block within a magento module. The block name was *Mycompany_CustomerModule_Block_CustomerModuleDashboardDataBlock* (yes, the name is very long, but I added the module name all blocks related to the module to avoid confusing a dashboard.html with the one used by Magento Core).
The issue is that, if I try to load such block, with the following command:
$this->getLayout()->createBlock('customermodule/customermoduledashboarddatablock')
Magento raises the exception "Invalid block type". I couldn't figure out what was wrong, since I copied the whole file from a block that works perfectly, and then I tried renaming the block to something shorter, such as simply DashboardDataBlock. With the shorter name, block is loaded correctly.
My question is, therefore, are there any limitations in the length of Classes' names? I can always shorten the class name, but I'd like to know if there are limits so that I can avoid having similar issues in the future. Thanks.
The problem is, regardless of file system case-sensitivity, that the last part of Magento class name must not be camel cased.
MyNamespace_MyModule_MyClassName NO
MyNamespace_MyModule_myclassname NO
MyNamespace_MyModule_myclassName NO
MyNamespace_MyModule_Myclassname YES
MyNamespace_MyModule_My_Class_Name YES
You will notice this pattern in core modules, too. Class prefixes of namespace and module are read from the configuration files and can contain arbitrary upper case letters. But the actual class names are derived from the class alias in a way that does not allow upper case letters (have a look at the code in Mage_Core_Model_Config::getGroupedClassname() if you want to know, why. Hint: it uses uc_words)
There could be a couple of things at play here. If you are on a case-sensitive filesystem, then the issue is that you need to match camelCasing between your filename and class ID (customermoduledashboarddatablock in your case), with the note that the first letter of any folder or file which is resoled by the autoloader will need to be uppercase regardless of class prefix or class ID. Additionally, note that PHP does not care about casing of class names and method calls - only the autoloader cares, and only on case-sensitive filesystems.
The other possible issue could be total filename length in a Windows environment.
I have a simple FileCreator Ruby class that has 1 method create which creates a blank txt file on my desktop. Using RSpec, how would I test this create method to make sure
that the file was created, without having to create the file? Would I use RSpec::Mocks? Can someone please point me in the right directory? Thanks!
After calling file_creator.create(100) you could search the folder for all File*.txt files and make sure the count matches. (Make sure to have your spec remove the test files after completion).
Dir.glob(File.join(File.expand_path("~/Desktop"), "File*.txt")).length.should == 100
Using Mocks: You could do something like this to verify that the File.open method is actually being called (to test that the files actually get created, though, you may want to consider actually creating the files like the first half of my answer).
File.should_receive(:open).exactly(100).times
You could also try using something like FakeFS which mocks the actual file system.
The simplest way to do it is as below:
FileCreator.count.should eq 100
I'm new to Ruby
MakModule.rb
module Display
class Multiply
def Multiply.mul(first, second)
return first * second
end
end
end
MakRequire1.rb
require "Display"
puts Multiply.mul 5,6
MakRequire2.rb
require "MakModule.rb"
puts Multiply.mul 5,6
both file give me the error below
ruby: No such file or directory -- makRequire (LoadError)
How should I correct my code?
It is simply impossible that the code you posted here generates that error message. The error message says that you tried to require a file named makRequire, but that filename doesn't appear anywhere in the code you posted.
Without the actual code that is generating the actual error, it is impossible to answer your question. However, here are a few general tips:
Whenever the computer tells you that it cannot find something, in 99% of the cases, the problem is that the thing the computer tells it couldn't find isn't actually there.
So, in this case, the computer tells you that it cannot find a file named makRequire.rb, and the most likely explanation for that is that makRequire.rb doesn't actually exist. So, the first thing you need to check is: does makRequire.rb (note the capitalization and the file extension) actually exist? Because if it doesn't exist, then the reason why the computer cannot find it, should be rather obvious.
In 99% of the rest of the cases, the problem is that the thing the computer is looking for does exist, but the computer is looking in the wrong place. So, after you have verified that makRequire.rb actually does exist, you need to make sure that the directory the file is in, is in Ruby's $LOAD_PATH, and if it isn't, you need to add that directory to Ruby's $LOAD_PATH. Alternatively, if you want to require the file relative to the path of the file that is doing the requiring, you need to use require_relative instead of require.
The third thing to check for, is whether the user who own the ruby process has sufficient privileges to access the file makRequire.rb, the directory it is in and all of its parent directories.
Try this,
require File.join(File.dirname(__FILE__),'MarkModule')
Try require './MakModule', because the . is the current directory.
require 'MakModule'
You can require a file that is in the same directory. To use a module you would typically include the module inside a class definition. So you would never require Display, you would require the file that contains Display (without the .rb extension, usually).
I'm writing some code that at run time may create or delete directories within the project path. I haven't really used ruby for file processing so i'm really uneasy about having code that, with a few mistypes weeks down the line, could result in wiping other directories outside of my project path.
Is there anyway to make it impossible for the program to delete files outside of its own path regardless of whats typed in destructive calls?
Pathname is a wrapper class for almost any file operations.
require "pathname"
path= Pathname.new("/home/johannes")
path.directory? # => true
path.children # => [#<Pathname:.bash_history>, #<Pathname:Documents>, #<Pathname:Desktop>]
path.children.each do |p|
p.delete if p.file?
end
Pathname#children does not contain . or .. so you don't accidently walk up the tree instead of down. If you still don't trust in the code, you can even check if on path is contained in another
Pathname.new("test") <=> Pathname.new("test/123") # => -1
You might want to create a wrapper method around your favourite delete method (or, perhaps, around whole class, because not only deleting files is potentially destructive file operation), which would expand all the submitted paths and check whether they begin with your "sandbox" path). You can also try to redefine delete method, if you are willing to cripple it through whole application.
And maybe the cleanest solution of them all would be to create a new user on your system and run your program as him.
On a POSIX system, you can use Dir.chroot to change the root that your application sees. Then ALL actions, not just delete ones, will be limited to the project directory. This does mean that external commands will be unavailable unless you make them part of your project directory as well.
This is the standard 'sandboxing' method used in Unix based systems. It can be difficult to setup (eliminating all external dependancies is sometimes hard), but affords significant protection when configured properly.
You could generate an Array of filenames in your project directory using
my_files = Dir["/bla/bla/your/directory/**/*"]
and then simply check if the filename passed to your "delete" function exist in your my_files array.
I'm sure there is a more elegant solution, but this could work ^_^
You could use File.expand_path and File.dirname on the input, and check that against __FILE__. So something like this might work:
File.delete(path) if File.dirname(File.expand_path(path)).include? File.dirname(File.expand_path(__FILE__))
I've got automated tests that routinely create and wipe out directories. I've taken two approaches:
Use /tmp as much as possible. The 'tmpdir' standard library module will create temporary directories which will be destroyed when your program exits. Or,
When the code creates a directory that it will later be deleting, it drops a marker file into the directory. When it comes time to delete the directory, if the marker file is not found, the code refuses to delete the directory. A marker file might be called ".ok_to_delete", for example.