Browse files

working on specs and a rake task generator

  • Loading branch information...
1 parent bfd0bc9 commit 90f6f70399ec1182422da5922a1ff4802d760eec @selman committed Jul 5, 2010
Showing with 187 additions and 63 deletions.
  1. +8 −6 YAMLiner.gemspec
  2. +64 −51 lib/yamliner.rb
  3. +54 −0 lib/yamliner_task.rb
  4. +4 −2 spec/spec_helper.rb
  5. +8 −0 spec/test.rake
  6. +22 −4 spec/yamliner_spec.rb
  7. +27 −0 spec/yamliner_task_spec.rb
View
14 YAMLiner.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{YAMLiner}
- s.version = "0.1.0"
+ s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Selman ULUG"]
- s.date = %q{2010-06-30}
+ s.date = %q{2010-07-05}
s.description = %q{Simple gem that supplies inline YAML CRUD operations that usable by all kind of text files.}
s.email = %q{selman.ulug@gmail.com}
s.extra_rdoc_files = [
@@ -23,10 +23,12 @@ Gem::Specification.new do |s|
"README.rdoc",
"Rakefile",
"VERSION",
- "lib/YAMLiner.rb",
- "spec/YAMLiner_spec.rb",
+ "YAMLiner.gemspec",
+ "lib/yamliner.rb",
+ "lib/yamliner_task.rb",
"spec/spec.opts",
- "spec/spec_helper.rb"
+ "spec/spec_helper.rb",
+ "spec/yamliner_spec.rb"
]
s.homepage = %q{http://github.com/selman/YAMLiner}
s.rdoc_options = ["--charset=UTF-8"]
@@ -35,7 +37,7 @@ Gem::Specification.new do |s|
s.summary = %q{inline YAML CRUD operations}
s.test_files = [
"spec/spec_helper.rb",
- "spec/YAMLiner_spec.rb"
+ "spec/yamliner_spec.rb"
]
if s.respond_to? :specification_version then
View
115 lib/yamliner.rb
@@ -1,87 +1,100 @@
-require 'yaml'
-require 'tempfile'
-require 'fileutils'
-
-#You can get inline YAML only redefining "to_yaml_style"
-class Object
- def to_yaml_style
- :inline
- end
-end
-
=begin
- y = YAMLiner.new(:file => "test.rb", :line => 2, :input => {:name => "selman"})
+ y = YAMLiner.new(:file => "test.rb", :line => 2, :object => {:name => "selman"})
=end
class YAMLiner
+ require 'yaml'
+ require 'tempfile'
+ require 'fileutils'
+
+ attr_accessor :params
- def initialize(options = {})
- @options = {
+ def initialize(params = {})
+ @params = {
:name => 'YAMLiner',
- :file => '',
+ :file => nil,
:line => 1,
- :input => nil,
- :output => nil,
+ :object => nil,
+ :yaml => nil,
:prefix => '#',
:postfix => '',
:backup => true
}
- @options.merge!(options) unless options.empty?
- check_options []
- @match_line = %r/(^#{Regexp.escape(@options[:prefix] + @options[:name])})(.*?)(#{Regexp.escape(@options[:postfix])}$)/
+ @params.merge!(params) unless params.empty?
+ check_params
+ @match_line = %r/(^#{Regexp.escape(@params[:prefix] + @params[:name])})(.*?)(#{Regexp.escape(@params[:postfix])}$)/
+ @file = nil || File.readlines(params[:file])
end
def read
- check_options [:file]
- File.foreach(@options[:file]) do |rline|
+ return false if @file.nil?
+ check_params [:file]
+ @file.each do |rline|
if rline =~ @match_line
- @options[:output] = YAML::load($2)
- @options[:line] = $.
+ @params[:yaml] = $2 + '\n'
+ @params[:object] = YAML::load($2)
+ @params[:line] = $.
break
end
end
- @options[:output]
+ @params[:object]
end
def write!
- check_options [:file, :input]
- yamline = @options[:prefix] + @options[:name] + @options[:input].to_yaml.chop + @options[:postfix] + "\n"
-
- with_temp do |temp|
- File.foreach(@options[:file]) do |wline|
- if @options[:line] == $.
- wline =~ @match_line ? wline = yamline : temp << yamline
- end
- temp << wline
+ check_params [:file, :object]
+ yline = yamline
+ temp = []
+ @file.each do |wline|
+ if @params[:line] == $.
+ wline =~ @match_line ? wline = yline : temp << yline
end
+ temp << wline
end
+ save_file(temp)
end
def delete!
- check_options [:file]
- with_temp do |temp|
- File.foreach(@options[:file]) do |dline|
- temp << dline unless dline =~ @match_line
- end
+ return false if @file.nil?
+ check_params [:file]
+ temp = []
+ @file.each do |dline|
+ temp << dline unless dline =~ @match_line
end
+ save_file(temp)
+ end
+
+ def yamline
+ @params[:prefix] + @params[:name] + @params[:object].to_yaml.chop + @params[:postfix] + "\n"
+ end
+
+ def [](key)
+ @params[key]
+ end
+
+ def []=(key, value)
+ @params[key] = value
end
private
- def with_temp
- temp = Tempfile.new('working')
- yield(temp)
- ensure
- temp.close
- file = @options[:file]
- FileUtils.mv(file, "#{file}.bak") if @options[:backup]
- FileUtils.mv(temp.path, file)
+ def save_file(temp)
+ @params[:backup] ? file = "#{params[:file]}.bak" : file = params[:file]
+ File.open(file, 'w+') { |f| f.puts temp }
+ @file = File.readlines(params[:file])
+ return true
end
- def check_options(options)
- options += [:name, :prefix]
- options.each do |opt|
- raise ArgumentError.new("\":#{opt}\" option must be set") if @options[opt].nil? or @options[opt].empty?
+ def check_params(params = [])
+ params += [:name, :prefix]
+ params.each do |opt|
+ raise ArgumentError.new("\":#{opt}\" option must be set") if @params[opt].nil? or @params[opt].empty?
end
end
end
+
+#You can get inline YAML only redefining "to_yaml_style"
+class Object
+ def to_yaml_style
+ :inline
+ end
+end # Object
View
54 lib/yamliner_task.rb
@@ -0,0 +1,54 @@
+require 'yamliner'
+
+class YAMLiner
+ require 'rake/tasklib'
+
+ # YAMLiner::Config.new do |c|
+ # c.deneme = "selman"
+ # end
+ class Tasks < ::Rake::TaskLib
+ attr_accessor :params
+
+ def initialize
+ @params = {:test => 'deneme'}
+ @params.merge!(params) unless params.empty?
+ @yamliner = YAMLiner.new(@params)
+ yield self if block_given?
+ define
+ end
+
+ private
+
+ def define
+ namespace :ylt do
+
+ desc "Interactive Move"
+ task :move do
+ @yamliner.ylt_move
+ end
+
+ desc "Interactive Copy"
+ task :copy do
+ @yamliner.ylt_copy
+ end
+
+ desc "Interactive File Insert"
+ task :insertfile do
+ @yamliner.ylt_inserfile
+ end
+
+ end
+ end
+
+ end #Config
+
+ def ylt_move
+ end
+
+ def ylt_copy
+ end
+
+ def ylt_inserfile
+ end
+
+end # YAMLiner
View
6 spec/spec_helper.rb
@@ -1,9 +1,11 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'YAMLiner'
+require 'yamliner'
require 'spec'
require 'spec/autorun'
+require 'fileutils'
+require 'rake'
Spec::Runner.configure do |config|
-
+
end
View
8 spec/test.rake
@@ -0,0 +1,8 @@
+begin
+ require File.join(File.dirname(__FILE__), '../lib/yamliner_task')
+ YAMLiner::Tasks.new do |c|
+ c.params[:name] = "selman"
+ end
+rescue LoadError
+ puts "YAMLiner not available. Install it with: gem install YAMLiner"
+end
View
26 spec/yamliner_spec.rb
@@ -3,10 +3,13 @@
describe "Yamliner" do
before(:all) do
- @test_file = Tempfile.new('testing')
- @test_file.puts "Test File"
+ @test_file = File.join(Dir.pwd, 'spec/test.txt')
+ @test_file_bak = File.join(Dir.pwd, 'spec/test.txt.bak')
+ File.open(@test_file, 'w+') do |f|
+ f.puts "Test File"
+ end
@input = {:name => 'selman', :surname => 'ulug'}
- @yamliner = YAMLiner.new(:file => @test_file.path, :input => @input)
+ @y = YAMLiner.new(:file => @test_file, :object => @input)
end
it "should raise ArgumentError without :name or :prefix parameters" do
@@ -15,7 +18,22 @@
lambda { YAMLiner.new }.should_not raise_exception(ArgumentError)
end
+ it 'should write to file and read from file' do
+ @y.write!
+ @y[:file] = @test_file_bak
+ should be_true(@y[:object] == @input)
+ @y[:file] = @test_file
+ end
+
+ it 'should delete from file' do
+ @y.delete!
+ @y[:file] = @test_file_bak
+ should be_true(@y.read == @input)
+ @y[:file] = @test_file
+ end
+
after(:all) do
- @test_file.close
+ FileUtils.rm(@test_file)
+ FileUtils.rm(@test_file_bak)
end
end
View
27 spec/yamliner_task_spec.rb
@@ -0,0 +1,27 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "YamlinerTask" do
+
+ before(:each) do
+ @rake = Rake::Application.new
+ Rake.application = @rake
+ load File.expand_path(File.dirname(__FILE__) + '/test.rake')
+# Rake::Task.define_task(:config)
+ end
+
+ it 'should run rake ylt:move' do
+ @rake["ylt:move"].invoke
+ end
+
+ it 'should run rake ylt:copy' do
+ @rake["ylt:copy"].invoke
+ end
+
+ it 'should run rake ylt:insertfile' do
+ @rake["ylt:insertfile"].invoke
+ end
+
+ after(:each) do
+ Rake.application = nil
+ end
+end

0 comments on commit 90f6f70

Please sign in to comment.