webdesserts / lazy_files Goto Github PK
View Code? Open in Web Editor NEWA Library to make File-Handling in Ruby simple(r).
License: MIT License
A Library to make File-Handling in Ruby simple(r).
License: MIT License
As found out while pursuing this question, Dir
s are not IO
s, but just streams. Also open
and close
and many of the other basic methods you would use on an IOstream are more so attributes of Streams than they are specific to IOs. Thats why I think using #stream
rather than #io
would be the correct use of terms.
file = Lazy.file('hello_world', 'w')
file.stream #=> <File:hello_world>
In the original File
class, if you wanted to create a file, you would have to File.open( 'file.txt' ) {}
. This is the same command you would use is you wanted to open an existing file as well. I feel that this can be confusing and doesn't allow for you to try to store a file reference and check for existence at the same time. I think something like the following would serve developers better:
Lazy.file( 'README.md' ) #=> nil
Lazy.mkfile( 'README.md' ) #=> <LazyFile:README.md>
Lazy.file( 'README.md' ) #=> <LazyFile:README.md>
I think ::mkfile
makes sense considering there's a ::mkdir
equivalent. I believe there's still a place for a touch
method but creating files was not its main purpose, updating timestamps was.
The ::mkfile
method should also allow you to pass a block and write in it, like so:
Lazy.mkfile( 'README' ) do |f|
f.puts 'TODO: write a description of your app'
end
Returns an array of everything in the current directory and returns either a LazyFile
or LazyDir
for each item. Should not return default directories like ..
or .
.
Lazy.ls() #=> [<LazyDir:lib>, <LazyDir:spec>, <LazyFile:README.md>]
Lazy.ls[0] #=> <LazyDir:src>
Lazy.ls.each do |item|
puts item.path
end
Lazy.ls( ftype: 'dir' ) #=> [<LazyDir:lib>, <LazyDir:spec>]
Lazy.ls( ext: 'md' ) #=> [<LazyFile:README.md>]
Basic functionality should probably be finished by v0.1.0
while filtering should probably be implemented around v0.3.0
There is currently a lot of logic that is duplicated between File
and Dir
. I think a Lazy::Path
or Lazy::Stream
should be attempted. This should also help reduce duplication in specs as well.
#ext
should basically be your File.extname
equivilant
Lazy.file( 'index.html' ).ext() #=> 'html'
#ext?
should return a boolean based on whether the #ext
matches the provided extension or not.
Lazy.file( 'index.html' ).ext?( 'html' ) #=> false
not sure if Lazy.file( 'README.md' ).ext?( 'markdown' )
should return true or not right now. Maybe this is a good spot for a `file.fformat?('markdown') instead, but that probably will be hard to maintain.
Lazy.dir( './' ) #=> <LazyDir:/>
Lazy.dir( '~/code' ) #=> <LazyDir:code>
So apparently file access modes aren't as simple as "read" and "write" and the current setup doesn't support stuff like 'a+' well nor allow users to specify these modes easily. Fix that.
self explanatory
#basically just like Dir.pwd but returns a LazyDir
Lazy.pwd() #=> <LazyDir:code>
This method should allow you to pass a block that will be recursively executed inside all directories nested under the working directory.
Lazy.recursively do |wd|
wd #would be the LazyDir of your working directory
end
should be able to set a limit on the maximum depth of nested directories the method reaches into
# this would execute the script in the wd as well as once in all directories
#1 level down.
Lazy.recursively( limit: 1 ) { |wd| ... }
There should be a copy of this method on the LazyDir
class
Lazy.dir('/').recursively() { |wd| ... }
There should also be a recursive
option for many of the libraries methods.
Lazy.ls( recursive: true ) #=> [ ... ]
Lazy.count( recursive: true ) #=> 5786
::count
should return the number of items in the working directory. It should basically be the equivalent of Lazy.ls.length
but should skip the process of creating objects for each entry.
Lazy.count() #=> 7
it should allow filtering options.
Lazy.count( ext: 'html' ) #=> 3
Lazy.count( ftype: 'dir' ) #=> 2
There should also be a LazyDir#count
method that should work the same, just it should return a count for that directory rather than the working directory.
Lazy.dir( '/' ).count #=> a really large number
Try to use more subject
and let
calls to prep most of the logic. Factories will probably also help. See if you can use lambdas or helper methods as well.
Now that I'm starting to migrate the repository onto Github, I need the README to actually reflect the current state of the program. Also, now that major progress is being made, the README should be updated with every change to the API. This is a library that I want other developers to use. The documentation needs to be top priority.
Just like Dir.chdir
Lazy.cd( '/' ) #=> <LazyDir:/>
Lazy.cd( '/' ) do |dir|
dir.path #=> '/'
end
Pull from ideas mentioned in #12 and create append methods for LazyFiles.
Since I looked into File IOs I now have a much better understanding of how they run and why they work the way they do. It looks like Dirs are similar but I need to make sure I know what I'm getting into. Research!
::dir
and ::file
should try to accept as many object types as possible.
Lazy.dir(Lazy.wd) #=> <LazyDir:code>
Lazy.dir(nil) #=> nil
Currently to_s returns the dir's name...
f = Lazy.dir( '/home/michael/code' )
f.to_s #=> <LazyDir:code>
The question is should it return the full path instead?
f.to_s #=> <LazyDir:/home/michael/code>
One of the common reasons you create a file is to tack on a few characters. It seems kinda over kill to pop open a block for this so I suggest that a puts
and print
method is added to the LazyFile class.
f = Lazy.file( 'readme.md' )
f.print 'hello'
f.print ' world'
f.read #=> "hello world"
f.puts 'goodbye world'
f.read #=> "hello worldgoodbye world/n"
Look into adding factories to the specs and see if it will clean them up a bit.
Look into adding Vim style pointer movement methods as mentioned in #12. See if its viable or even beneficial.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.