Skip to content

Commit

Permalink
simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
selman committed Jul 14, 2010
1 parent 8bdee6b commit 144091b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 100 deletions.
178 changes: 81 additions & 97 deletions lib/yamliner.rb
@@ -1,115 +1,99 @@
#Defines YAMLiner class
#require 'yaml'
#require 'fileutils'
require 'yaml'
require 'fileutils'

#Defines YAMLiner Module
module YAMLiner
def self.line(*objects)
objects.each do |object|

#YAMLiner class definition
class << object
require 'yaml'
require 'fileutils'
extend self

attr_accessor_with_default_setter :params do
{ :name => 'YAMLiner',
:file => '',
:line => 0,
:prefix => '#',
:postfix => '',
:backup => false }
end
def line(*objects)
params =
{:name => 'YAMLiner',
:file => '',
:line => 0,
:prefix => '#',
:postfix => '',
:writeto => '' }

attr_accessor_with_default_setter :file do
[]
end
objects.each do |object|
if object.respond_to?(:to_yaml)
object.extend(YAMLinerActions)
object.instance_variable_set(:@lines, [])
object.instance_variable_set(:@params, params)
else
raise "#{object.class} not responds \"to_yaml\" method"
end
end
end

def match_line
%r/(^#{Regexp.escape(@params[:prefix] + @params[:name])})(.*?)(#{Regexp.escape(@params[:postfix])}$)/
end
#CRUD operations functions and some tools
module YAMLinerActions
attr_accessor :params

#Reads your supplied file/files if successfull returns readed object
# Dir['**/*.txt].each {|f| y.read }
def yamline_read
return unless read_file?
@file.each do |rline|
if rline =~ match_line
return YAML::load($2)
end
end
nil
end
#You can get inline YAML only redefining this method
def to_yaml_style
:inline
end

#Writes the generated YAMLiner line to supplied file/files if there
#is a same formated line it writes over it
def yamline_write!
read_file?
# return if @file.size < params[:line]
temp = []
@file.each do |wline|
if @params[:line] == @file.index(wline)
wline =~ match_line ? wline = yamline : temp << yamline
end
temp << wline
end
save_file(temp)
end
def match_line
%r/(^#{Regexp.escape(@params[:prefix] + @params[:name])})(.*?)(#{Regexp.escape(@params[:postfix])}$)/
end

#Finds and deletes formatted line from supplied file/files
def yamline_delete!
return unless read_file?
temp = []
@file.each do |dline|
temp << dline unless dline =~ match_line
end
save_file(temp)
#Reads your supplied file/files if successfull returns readed object
# Dir['**/*.txt].each {|f| y.read }
def yamline_read
return unless read_file?
@lines.each do |rline|
if rline =~ match_line
return YAML::load($2)
end
end
nil
end

#Returns generated YAMLiner line
def yamline
#You can get inline YAML only redefining *to_yaml_style* method
instance_eval { def to_yaml_style; :inline; end }
@params[:prefix] + @params[:name] + self.to_yaml.chop + @params[:postfix] + "\n"
#Writes the generated YAMLiner line to supplied file/files if there
#is a same formated line it writes over it
def yamline_write!
read_file?
# return if @lines.size < params[:line]
temp = []
@lines.each do |wline|
if @params[:line] == @lines.index(wline)
wline =~ match_line ? wline = yamline : temp << yamline
end
temp << wline
end
save_file(temp)
end

private

def read_file?
return if @params[:file].empty?
return unless File.exists?(@params[:file])
@file = File.readlines(@params[:file])
end
#Finds and deletes formatted line from supplied file/files
def yamline_delete!
return unless read_file?
temp = []
@lines.each do |dline|
temp << dline unless dline =~ match_line
end
save_file(temp)
end

def save_file(temp)
@params[:backup] ? file = "#{@params[:file]}.bak" : file = @params[:file]
File.open(file, 'w+') { |f| f.puts temp }
read_file?
end
#Returns generated YAMLiner line
def yamline
@params[:prefix] + @params[:name] + self.to_yaml.chop + @params[:postfix] + "\n"
end

end # YAMLiner
private

def read_file?
return if @params[:file].empty?
return unless File.exists?(@params[:file])
@lines = File.readlines(@params[:file])
end
end
end

class Module
def attr_accessor_with_default_setter( *syms, &block )
raise 'Default value in block required' unless block
syms.each do | sym |
module_eval do
attr_writer( sym )
define_method( sym ) do | |
class << self; self; end.class_eval do
attr_reader( sym )
end
if instance_variables.include? "@#{sym}"
instance_variable_get( "@#{sym}" )
else
instance_variable_set( "@#{sym}", block.call )
end
end
end
def save_file(temp)
@params[:writeto].empty? ? tofile = @params[:file] : tofile = @params[:writeto]
File.open(tofile, 'w+') { |file| file.puts temp }
read_file?
end
nil
end
end

end # YAMLinerActions

end # YAMLiner
6 changes: 3 additions & 3 deletions spec/yamliner_spec.rb
Expand Up @@ -17,9 +17,9 @@
# lambda { YAMLiner.new }.should_not raise_exception(ArgumentError)
# end

# it "should raise Argument error when no file specified to read" do
# @input.yamline_read.should raise_exception
# end
it "should return nil when no file specified to read" do
@input.yamline_read.should be_false
end

it "should return false when specified file is not available to read" do
@input.params[:file] = 'not_available.txt'
Expand Down

0 comments on commit 144091b

Please sign in to comment.