YAMLiner 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Selman ULUG
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = What is this?
2
+
3
+ With this library you gives a Hash or an Array instance and it extends this with it's own methods
4
+ then you can CRUD on this instance <b>inline YAML</b> line with your specified options.
5
+
6
+ == Installation
7
+
8
+ (sudo) gem install YAMLiner
9
+ then in your source code;
10
+ require 'yamliner'
11
+
12
+ = Usage Samples
13
+
14
+ >> my_hash = {:name => 'selman', :surname => 'ulug'}
15
+ => {:name=>"selman", :surname=>"ulug"}
16
+ >> YAMLiner::line my_hash
17
+ => [{:name=>"selman", :surname=>"ulug"}]
18
+ >> my_hash.yamline
19
+ => "#YAMLiner--- {:name: selman, :surname: ulug}\n"
20
+
21
+ It supplies
22
+ CRUD[http://en.wikipedia.org/wiki/Create,_read,_update_and_delete]
23
+ operation functions #read, #delete!, #write! (creates and updates)
24
+ you can change :name, :prefix, :postfix for every type of text file
25
+ comment styles like this:
26
+
27
+ >> my_hash.yamline_settings(:prefix => '/*', :postfix => '*/', :name => 'my_c_comment')
28
+ => {:name=>"my_c_comment", :file=>"", :line=>0, :prefix=>"/*", :postfix=>"*/", :writeto=>""}
29
+ >> my_hash.yamline
30
+ => "/*my_c_comment--- {:name: selman, :surname: ulug}*/\n"
31
+
32
+ == Note on Patches/Pull Requests
33
+
34
+ * Fork the project.
35
+ * Make your feature addition or bug fix.
36
+ * Add tests for it. This is important so I don't break it in a
37
+ future version unintentionally.
38
+ * Commit, do not mess with rakefile, version, or history.
39
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
40
+ * Send me a pull request. Bonus points for topic branches.
41
+
42
+ == Copyright
43
+
44
+ Copyright (c) 2010 Selman ULUG. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "YAMLiner"
8
+ gem.summary = "inline YAML CRUD operations"
9
+ gem.description = "Simple gem that supplies inline YAML CRUD operations that usable by all kind of text files."
10
+ gem.email = "selman.ulug@gmail.com"
11
+ gem.homepage = "http://github.com/selman/YAMLiner"
12
+ gem.authors = ["Selman ULUG"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ begin
36
+ require 'reek/rake/task'
37
+ Reek::Rake::Task.new do |t|
38
+ t.fail_on_error = false
39
+ t.verbose = true
40
+ end
41
+ rescue LoadError
42
+ task :reek do
43
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
44
+ end
45
+ end
46
+
47
+ begin
48
+ require 'roodi'
49
+ require 'roodi_task'
50
+ RoodiTask.new do |t|
51
+ t.verbose = true
52
+ t.patterns = %w(lib/**/*.rb spec/**/*.rb)
53
+ end
54
+ rescue LoadError
55
+ task :roodi do
56
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
57
+ end
58
+ end
59
+
60
+ task :default => :spec
61
+
62
+ require 'rake/rdoctask'
63
+ Rake::RDocTask.new do |rdoc|
64
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
65
+
66
+ rdoc.rdoc_dir = 'rdoc'
67
+ rdoc.title = "YAMLiner #{version}"
68
+ rdoc.rdoc_files.include('README*')
69
+ rdoc.rdoc_files.include('lib/**/*.rb')
70
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
data/YAMLiner.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{YAMLiner}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Selman ULUG"]
12
+ s.date = %q{2010-07-17}
13
+ s.description = %q{Simple gem that supplies inline YAML CRUD operations that usable by all kind of text files.}
14
+ s.email = %q{selman.ulug@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "YAMLiner.gemspec",
27
+ "lib/yamliner.rb",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb",
30
+ "spec/yamliner_spec.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/selman/YAMLiner}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{inline YAML CRUD operations}
37
+ s.test_files = [
38
+ "spec/spec_helper.rb",
39
+ "spec/yamliner_spec.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ end
55
+
data/lib/yamliner.rb ADDED
@@ -0,0 +1,116 @@
1
+ #= YAMLiner
2
+ #Simple library for CRUD operations on formatted comment lines with
3
+ #<b>inline YAML</b>.
4
+ # a = {:name => 'selman', :surname => 'ulug'}
5
+ # YAMLiner::line a
6
+ # a.yamline_settings(:name => 'MyLine')
7
+ require 'yaml'
8
+ require 'fileutils'
9
+
10
+ #YAMLiner module definition
11
+ module YAMLiner
12
+ extend self
13
+
14
+ #extends given Array or Hash instance with YAMLinerActions
15
+ # a = {:name => 'selman', :surname => 'ulug'}
16
+ # YAMLiner::line a
17
+ def line(*objects)
18
+ settings =
19
+ {:name => 'YAMLiner',
20
+ :file => '',
21
+ :line => 0,
22
+ :prefix => '#',
23
+ :postfix => '',
24
+ :writeto => '' }
25
+
26
+ objects.each do |object|
27
+ if object.kind_of?(Array) or object.kind_of?(Hash)
28
+ object.extend(YAMLinerActions)
29
+ object.instance_variable_set(:@settings, settings)
30
+ else
31
+ raise "only Hash or Array classes supported: #{object.class} not supported"
32
+ end
33
+ end
34
+ end
35
+
36
+ #CRUD operation functions and some tools
37
+ module YAMLinerActions
38
+ attr_reader :settings
39
+
40
+ #You can get inline YAML only redefining this method
41
+ def to_yaml_style
42
+ :inline
43
+ end
44
+
45
+ #Returns generated YAMLiner line
46
+ # >> a.yamline
47
+ # => "#YAMLiner--- {:name: selman, :surname: ulug}\n"
48
+ def yamline
49
+ @settings[:prefix] + @settings[:name] + self.to_yaml.chop + @settings[:postfix] + "\n"
50
+ end
51
+
52
+ #YAMLiner settings default values
53
+ # :name => 'YAMLiner'
54
+ # :files => []
55
+ # :line => 0
56
+ # :prefix => '#'
57
+ # :postfix => ''
58
+ # :writeto => ''
59
+ def yamline_settings(sets = {})
60
+ @settings.merge!(sets) unless sets.empty?
61
+ end
62
+
63
+ #Reads your supplied file/files if successfull returns readed object
64
+ # Dir['**/*.txt'].each {|f| a.yamline_settins(:file => f); a.yamline_read }
65
+ def yamline_read(lines = nil, loaded = true)
66
+ lines = file_lines unless lines
67
+ return unless lines
68
+ matcher = %r{(^#{Regexp.escape(@settings[:prefix] + @settings[:name])})(.*?)(#{Regexp.escape(@settings[:postfix])}$)}
69
+ line_l = []
70
+ line_s = lines.select {|line| line =~ matcher; line_l << YAML::load($2) if $2 }
71
+ return if line_s.empty?
72
+ loaded ? line_l : line_s
73
+ end
74
+
75
+ #Writes the generated YAMLiner line to supplied file/files if there
76
+ #is a same formated line it deletes and write again.
77
+ # a.yamline_settings(:file => 'test.txt')
78
+ # a.yamline_write!
79
+ def yamline_write!
80
+ lines = file_lines || []
81
+ yamline_delete!(lines) unless lines.empty?
82
+ lines.insert(settings[:line], yamline)
83
+ save_file(lines)
84
+ end
85
+
86
+ #Finds and deletes formatted line/lines from supplied
87
+ #file/files. if formetted line is not uniq it deletes all of them.
88
+ # a.yamline_settings(:file => 'test.txt')
89
+ # a.yamline_delete!
90
+ def yamline_delete!(lines = nil)
91
+ lines = file_lines unless lines
92
+ return unless lines
93
+ lines_readed = yamline_read(lines, false)
94
+ lines_readed.each {|lr| lines.delete(lr) } if lines_readed
95
+ save_file(lines)
96
+ end
97
+
98
+ private
99
+
100
+ def file_lines
101
+ file = @settings[:file]
102
+ return if file.empty?
103
+ return unless File.exists?(file)
104
+ File.readlines(file)
105
+ end
106
+
107
+ def save_file(temp)
108
+ writeto = @settings[:writeto]
109
+ writeto = @settings[:file] if writeto.empty?
110
+ File.open(writeto, 'w+') { |file| file.puts temp }
111
+ true
112
+ end
113
+
114
+ end # YAMLinerActions
115
+
116
+ end # YAMLiner
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color -fn
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'yamliner'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Yamliner" do
4
+
5
+ before(:all) do
6
+ @test_file = File.join(Dir.pwd, 'spec/test.txt')
7
+ File.open(@test_file, 'w+') do |f|
8
+ f.puts "Test File"
9
+ end
10
+ @input = {:name => 'selman', :surname => 'ulug'}
11
+ YAMLiner::line @input
12
+ end
13
+
14
+ it "should raise exception other than Array or Hash" do
15
+ lambda { s=''; YAMLiner::line s}.should raise_exception
16
+ end
17
+
18
+ it "should return nil when no file specified to read" do
19
+ @input.yamline_read.should be_nil
20
+ end
21
+
22
+ it "should return nil when specified file is not available to read" do
23
+ @input.yamline_settings(:file => 'not_available.txt')
24
+ @input.yamline_read.should be_nil
25
+ end
26
+
27
+ it "should read specified file and return nil when no YAMLiner" do
28
+ @input.yamline_settings(:file => @test_file)
29
+ @input.yamline_read.should be_nil
30
+ end
31
+
32
+ it 'should write to file' do
33
+ @input.yamline_write!.should_not be_nil
34
+ end
35
+
36
+ it "should read file and two objects must be equal" do
37
+ @input.yamline_read.should_not be_nil
38
+ read_lines = @input.yamline_read
39
+ read_lines.each do |rl|
40
+ rl.size.should eql(@input.size)
41
+ @input.each do |k, v|
42
+ rl.has_key?(k).should be_true
43
+ rl.has_value?(v).should be_true
44
+ end
45
+ end
46
+ end
47
+
48
+ it 'should delete from file' do
49
+ @input.yamline_delete!.should_not be_nil
50
+ @input.yamline_read.should be_nil
51
+ end
52
+
53
+ after(:all) do
54
+ FileUtils.rm(@test_file)
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: YAMLiner
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Selman ULUG
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-17 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Simple gem that supplies inline YAML CRUD operations that usable by all kind of text files.
38
+ email: selman.ulug@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - YAMLiner.gemspec
54
+ - lib/yamliner.rb
55
+ - spec/spec.opts
56
+ - spec/spec_helper.rb
57
+ - spec/yamliner_spec.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/selman/YAMLiner
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: inline YAML CRUD operations
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/yamliner_spec.rb