YAMLiner 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -3
- data/VERSION +1 -1
- data/YAMLiner.gemspec +2 -2
- data/lib/yamliner.rb +24 -16
- data/spec/yamliner_spec.rb +5 -5
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
= What is this?
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
This library extends given object instance with it's own methods and then you
|
4
|
+
can use this instance to CRUD over files as comment lines in format of <b>inline YAML</b>
|
5
|
+
line with your specified options.
|
5
6
|
|
6
7
|
== Installation
|
7
8
|
|
@@ -24,7 +25,7 @@ operation functions #read, #delete!, #write! (creates and updates)
|
|
24
25
|
you can change :name, :prefix, :postfix for every type of text file
|
25
26
|
comment styles like this:
|
26
27
|
|
27
|
-
>> my_hash.
|
28
|
+
>> my_hash.yamline_set(:prefix => '/*', :postfix => '*/', :name => 'my_c_comment')
|
28
29
|
=> {:name=>"my_c_comment", :file=>"", :line=>0, :prefix=>"/*", :postfix=>"*/", :writeto=>""}
|
29
30
|
>> my_hash.yamline
|
30
31
|
=> "/*my_c_comment--- {:name: selman, :surname: ulug}*/\n"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/YAMLiner.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{YAMLiner}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Selman ULUG"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-08-01}
|
13
13
|
s.description = %q{Simple gem that supplies inline YAML CRUD operations that usable by all kind of text files.}
|
14
14
|
s.email = %q{selman.ulug@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/yamliner.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#<b>inline YAML</b>.
|
4
4
|
# a = {:name => 'selman', :surname => 'ulug'}
|
5
5
|
# YAMLiner::line a
|
6
|
-
# a.
|
6
|
+
# a.yamline_set(:name => 'MyLine')
|
7
7
|
require 'yaml'
|
8
8
|
require 'fileutils'
|
9
9
|
|
@@ -24,29 +24,37 @@ module YAMLiner
|
|
24
24
|
:writeto => '' }
|
25
25
|
|
26
26
|
objects.each do |object|
|
27
|
-
if object.
|
27
|
+
if object.respond_to?(:to_yaml)
|
28
28
|
object.extend(YAMLinerActions)
|
29
|
-
object.instance_variable_set(:@
|
29
|
+
object.instance_variable_set(:@yamline_settings, settings)
|
30
30
|
else
|
31
|
-
raise "
|
31
|
+
raise "#{object.class} do not responding to to_yaml method"
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
#CRUD operation functions and some tools
|
37
37
|
module YAMLinerActions
|
38
|
-
attr_reader :
|
38
|
+
attr_reader :yamline_settings
|
39
39
|
|
40
40
|
#You can get inline YAML only redefining this method
|
41
41
|
def to_yaml_style
|
42
42
|
:inline
|
43
43
|
end
|
44
44
|
|
45
|
+
#aliasing to_yaml_properties to remove our instance variable
|
46
|
+
alias to_yaml_properties_orginal to_yaml_properties
|
47
|
+
|
48
|
+
#removing our yamline_settings instance variable
|
49
|
+
def to_yaml_properties
|
50
|
+
to_yaml_properties_orginal - [:@yamline_settings]
|
51
|
+
end
|
52
|
+
|
45
53
|
#Returns generated YAMLiner line
|
46
54
|
# >> a.yamline
|
47
55
|
# => "#YAMLiner--- {:name: selman, :surname: ulug}\n"
|
48
56
|
def yamline
|
49
|
-
@
|
57
|
+
@yamline_settings[:prefix] + @yamline_settings[:name] + to_yaml.chop + @yamline_settings[:postfix] + "\n"
|
50
58
|
end
|
51
59
|
|
52
60
|
#YAMLiner settings default values
|
@@ -56,16 +64,16 @@ module YAMLiner
|
|
56
64
|
# :prefix => '#'
|
57
65
|
# :postfix => ''
|
58
66
|
# :writeto => ''
|
59
|
-
def
|
60
|
-
@
|
67
|
+
def yamline_set(settings = {})
|
68
|
+
@yamline_settings.merge!(settings) unless settings.empty?
|
61
69
|
end
|
62
70
|
|
63
71
|
#Reads your supplied file/files if successfull returns readed object
|
64
|
-
# Dir['**/*.txt'].each {|f| a.
|
72
|
+
# Dir['**/*.txt'].each {|f| a.yamline_set(:file => f); a.yamline_read }
|
65
73
|
def yamline_read(lines = nil, loaded = true)
|
66
74
|
lines = file_lines unless lines
|
67
75
|
return unless lines
|
68
|
-
matcher = %r{(^#{Regexp.escape(@
|
76
|
+
matcher = %r{(^#{Regexp.escape(@yamline_settings[:prefix] + @yamline_settings[:name])})(.*?)(#{Regexp.escape(@yamline_settings[:postfix])}$)}
|
69
77
|
line_l = []
|
70
78
|
line_s = lines.select {|line| line =~ matcher; line_l << YAML::load($2) if $2 }
|
71
79
|
return if line_s.empty?
|
@@ -74,18 +82,18 @@ module YAMLiner
|
|
74
82
|
|
75
83
|
#Writes the generated YAMLiner line to supplied file/files if there
|
76
84
|
#is a same formated line it deletes and write again.
|
77
|
-
# a.
|
85
|
+
# a.yamline_set(:file => 'test.txt')
|
78
86
|
# a.yamline_write!
|
79
87
|
def yamline_write!
|
80
88
|
lines = file_lines || []
|
81
89
|
yamline_delete!(lines) unless lines.empty?
|
82
|
-
lines.insert(
|
90
|
+
lines.insert(@yamline_settings[:line], yamline)
|
83
91
|
save_file(lines)
|
84
92
|
end
|
85
93
|
|
86
94
|
#Finds and deletes formatted line/lines from supplied
|
87
95
|
#file/files. if formetted line is not uniq it deletes all of them.
|
88
|
-
# a.
|
96
|
+
# a.yamline_set(:file => 'test.txt')
|
89
97
|
# a.yamline_delete!
|
90
98
|
def yamline_delete!(lines = nil)
|
91
99
|
lines = file_lines unless lines
|
@@ -98,15 +106,15 @@ module YAMLiner
|
|
98
106
|
private
|
99
107
|
|
100
108
|
def file_lines
|
101
|
-
file = @
|
109
|
+
file = @yamline_settings[:file]
|
102
110
|
return if file.empty?
|
103
111
|
return unless File.exists?(file)
|
104
112
|
File.readlines(file)
|
105
113
|
end
|
106
114
|
|
107
115
|
def save_file(temp)
|
108
|
-
writeto = @
|
109
|
-
writeto = @
|
116
|
+
writeto = @yamline_settings[:writeto]
|
117
|
+
writeto = @yamline_settings[:file] if writeto.empty?
|
110
118
|
File.open(writeto, 'w+') { |file| file.puts temp }
|
111
119
|
true
|
112
120
|
end
|
data/spec/yamliner_spec.rb
CHANGED
@@ -11,21 +11,21 @@ describe "Yamliner" do
|
|
11
11
|
YAMLiner::line @input
|
12
12
|
end
|
13
13
|
|
14
|
-
it "should raise exception other than Array or Hash" do
|
15
|
-
lambda { s=''; YAMLiner::line s}.should raise_exception
|
16
|
-
end
|
14
|
+
# it "should raise exception other than Array or Hash" do
|
15
|
+
# lambda { s=''; YAMLiner::line s}.should raise_exception
|
16
|
+
# end
|
17
17
|
|
18
18
|
it "should return nil when no file specified to read" do
|
19
19
|
@input.yamline_read.should be_nil
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should return nil when specified file is not available to read" do
|
23
|
-
@input.
|
23
|
+
@input.yamline_set(:file => 'not_available.txt')
|
24
24
|
@input.yamline_read.should be_nil
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should read specified file and return nil when no YAMLiner" do
|
28
|
-
@input.
|
28
|
+
@input.yamline_set(:file => @test_file)
|
29
29
|
@input.yamline_read.should be_nil
|
30
30
|
end
|
31
31
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: YAMLiner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Selman ULUG
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-08-01 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|