rubysl-yaml 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/yaml/dbm.rb CHANGED
@@ -1,23 +1,52 @@
1
1
  require 'yaml'
2
2
  require 'dbm'
3
- #
4
- # YAML + DBM = YDBM
5
- # - Same interface as DBM class
6
- #
3
+
7
4
  module YAML
8
5
 
6
+ # YAML + DBM = YDBM
7
+ #
8
+ # YAML::DBM provides the same interface as ::DBM.
9
+ #
10
+ # However, while DBM only allows strings for both keys and values,
11
+ # this library allows one to use most Ruby objects for values
12
+ # by first converting them to YAML. Keys must be strings.
13
+ #
14
+ # Conversion to and from YAML is performed automatically.
15
+ #
16
+ # See the documentation for ::DBM and ::YAML for more information.
9
17
  class DBM < ::DBM
10
18
  VERSION = "0.1"
19
+
20
+ # Return value associated with +key+ from database.
21
+ #
22
+ # Returns +nil+ if there is no such +key+.
11
23
  def []( key )
12
24
  fetch( key )
13
25
  end
26
+
27
+ # :call-seq:
28
+ # []=( key, value )
29
+ #
30
+ # Set +key+ to +value+ in database.
31
+ #
32
+ # +value+ will be converted to YAML before storage.
14
33
  def []=( key, val )
15
34
  store( key, val )
16
35
  end
36
+
37
+ # :call-seq:
38
+ # fetch( key, ifnone = nil )
39
+ # fetch( key, &block )
40
+ #
41
+ # Return value associated with +key+.
42
+ #
43
+ # If there is no value for +key+ and no block is given, returns +ifnone+.
44
+ #
45
+ # Otherwise, calls block passing in the given +key+.
17
46
  def fetch( keystr, ifnone = nil )
18
47
  begin
19
48
  val = super( keystr )
20
- return YAML::load( val ) if String === val
49
+ return YAML.load( val ) if String === val
21
50
  rescue IndexError
22
51
  end
23
52
  if block_given?
@@ -26,58 +55,110 @@ class DBM < ::DBM
26
55
  ifnone
27
56
  end
28
57
  end
58
+
59
+ # Deprecated, used YAML::DBM#key instead.
29
60
  def index( keystr )
30
61
  super( keystr.to_yaml )
31
62
  end
63
+
64
+ # Returns an array containing the values associated with the given keys.
32
65
  def values_at( *keys )
33
66
  keys.collect { |k| fetch( k ) }
34
67
  end
68
+
69
+ # Deletes value from database associated with +key+.
70
+ #
71
+ # Returns value or +nil+.
35
72
  def delete( key )
36
73
  v = super( key )
37
74
  if String === v
38
- v = YAML::load( v )
75
+ v = YAML.load( v )
39
76
  end
40
77
  v
41
78
  end
42
- def delete_if
79
+
80
+ # Calls the given block once for each +key+, +value+ pair in the database.
81
+ # Deletes all entries for which the block returns true.
82
+ #
83
+ # Returns +self+.
84
+ def delete_if # :yields: [key, value]
43
85
  del_keys = keys.dup
44
86
  del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
45
- del_keys.each { |k| delete( k ) }
87
+ del_keys.each { |k| delete( k ) }
46
88
  self
47
89
  end
90
+
91
+ # Converts the contents of the database to an in-memory Hash, then calls
92
+ # Hash#reject with the specified code block, returning a new Hash.
48
93
  def reject
49
94
  hsh = self.to_hash
50
95
  hsh.reject { |k,v| yield k, v }
51
96
  end
52
- def each_pair
97
+
98
+ # Calls the given block once for each +key+, +value+ pair in the database.
99
+ #
100
+ # Returns +self+.
101
+ def each_pair # :yields: [key, value]
53
102
  keys.each { |k| yield k, fetch( k ) }
54
103
  self
55
104
  end
56
- def each_value
57
- super { |v| yield YAML::load( v ) }
105
+
106
+ # Calls the given block for each value in database.
107
+ #
108
+ # Returns +self+.
109
+ def each_value # :yields: value
110
+ super { |v| yield YAML.load( v ) }
58
111
  self
59
112
  end
113
+
114
+ # Returns an array of values from the database.
60
115
  def values
61
- super.collect { |v| YAML::load( v ) }
116
+ super.collect { |v| YAML.load( v ) }
62
117
  end
118
+
119
+ # Returns true if specified value is found in the database.
63
120
  def has_value?( val )
64
121
  each_value { |v| return true if v == val }
65
122
  return false
66
123
  end
124
+
125
+ # Returns a Hash (not a DBM database) created by using each value in the
126
+ # database as a key, with the corresponding key as its value.
127
+ #
128
+ # Note that all values in the hash will be Strings, but the keys will be
129
+ # actual objects.
67
130
  def invert
68
131
  h = {}
69
132
  keys.each { |k| h[ self.fetch( k ) ] = k }
70
133
  h
71
134
  end
135
+
136
+ # Replaces the contents of the database with the contents of the specified
137
+ # object. Takes any object which implements the each_pair method, including
138
+ # Hash and DBM objects.
72
139
  def replace( hsh )
73
140
  clear
74
141
  update( hsh )
75
142
  end
143
+
144
+ # Removes a [key, value] pair from the database, and returns it.
145
+ # If the database is empty, returns +nil+.
146
+ #
147
+ # The order in which values are removed/returned is not guaranteed.
76
148
  def shift
77
149
  a = super
78
- a[1] = YAML::load( a[1] ) if a
150
+ a[1] = YAML.load( a[1] ) if a
79
151
  a
80
152
  end
153
+
154
+ # :call-seq:
155
+ # select( &block )
156
+ # select( *keys )
157
+ #
158
+ # If a block is provided, returns a new array containing [key, value] pairs
159
+ # for which the block returns true.
160
+ #
161
+ # Otherwise, same as #values_at
81
162
  def select( *keys )
82
163
  if block_given?
83
164
  self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
@@ -85,26 +166,48 @@ class DBM < ::DBM
85
166
  values_at( *keys )
86
167
  end
87
168
  end
169
+
170
+ # :call-seq:
171
+ # store( key, value )
172
+ #
173
+ #Stores +value+ in database with +key+ as the index. +value+ is converted
174
+ #to YAML before being stored.
175
+ #
176
+ #Returns +value+
88
177
  def store( key, val )
89
178
  super( key, val.to_yaml )
90
179
  val
91
180
  end
181
+
182
+ # Updates the database with multiple values from the specified object.
183
+ # Takes any object which implements the each_pair method, including
184
+ # Hash and DBM objects.
185
+ #
186
+ # Returns +self+.
92
187
  def update( hsh )
93
188
  hsh.keys.each do |k|
94
189
  self.store( k, hsh.fetch( k ) )
95
190
  end
96
191
  self
97
192
  end
193
+
194
+ # Converts the contents of the database to an array of [key, value] arrays,
195
+ # and returns it.
98
196
  def to_a
99
197
  a = []
100
198
  keys.each { |k| a.push [ k, self.fetch( k ) ] }
101
199
  a
102
200
  end
201
+
202
+
203
+ # Converts the contents of the database to an in-memory Hash object, and
204
+ # returns it.
103
205
  def to_hash
104
206
  h = {}
105
207
  keys.each { |k| h[ k ] = self.fetch( k ) }
106
208
  h
107
209
  end
210
+
108
211
  alias :each :each_pair
109
212
  end
110
213
 
data/lib/yaml/store.rb CHANGED
@@ -4,9 +4,50 @@
4
4
  require 'yaml'
5
5
  require 'pstore'
6
6
 
7
+ # YAML::Store provides the same functionality as PStore, except it uses YAML
8
+ # to dump objects instead of Marshal.
9
+ #
10
+ # == Example
11
+ #
12
+ # require 'yaml/store'
13
+ #
14
+ # Person = Struct.new :first_name, :last_name
15
+ #
16
+ # people = [Person.new("Bob", "Smith"), Person.new("Mary", "Johnson")]
17
+ #
18
+ # store = YAML::Store.new "test.store"
19
+ #
20
+ # store.transaction do
21
+ # store["people"] = people
22
+ # store["greeting"] = { "hello" => "world" }
23
+ # end
24
+ #
25
+ # After running the above code, the contents of "test.store" will be:
26
+ #
27
+ # ---
28
+ # people:
29
+ # - !ruby/struct:Person
30
+ # first_name: Bob
31
+ # last_name: Smith
32
+ # - !ruby/struct:Person
33
+ # first_name: Mary
34
+ # last_name: Johnson
35
+ # greeting:
36
+ # hello: world
37
+
7
38
  class YAML::Store < PStore
39
+
40
+ # :call-seq:
41
+ # initialize( file_name, yaml_opts = {} )
42
+ #
43
+ # Creates a new YAML::Store object, which will store data in +file_name+.
44
+ # If the file does not already exist, it will be created.
45
+ #
46
+ #
47
+ # Options passed in through +yaml_opts+ will be used when converting the
48
+ # store to YAML via Hash#to_yaml().
8
49
  def initialize( *o )
9
- @opt = YAML::DEFAULTS.dup
50
+ @opt = {}
10
51
  if String === o.first
11
52
  super(o.shift)
12
53
  end
@@ -15,12 +56,14 @@ class YAML::Store < PStore
15
56
  end
16
57
  end
17
58
 
59
+ # :stopdoc:
60
+
18
61
  def dump(table)
19
62
  @table.to_yaml(@opt)
20
63
  end
21
64
 
22
65
  def load(content)
23
- table = YAML::load(content)
66
+ table = YAML.load(content)
24
67
  if table == false
25
68
  {}
26
69
  else
data/lib/yaml/syck.rb CHANGED
@@ -1,44 +1,53 @@
1
+ # $Id$
1
2
  #
2
- # YAML::Syck module
3
- # .. glues syck and yaml.rb together ..
3
+ # = yaml/syck.rb:
4
4
  #
5
- require 'syck/syck'
6
- require 'yaml/basenode'
7
-
8
- module YAML
9
- module Syck
10
5
 
11
- #
12
- # Mixin BaseNode functionality
13
- #
14
- class Node
15
- include YAML::BaseNode
16
- end
6
+ require 'stringio'
7
+ require 'syck/ext/syck'
8
+ require 'syck/error'
9
+ require 'syck/syck'
10
+ require 'syck/tag'
11
+ require 'syck/stream'
12
+ require 'syck/constants'
13
+ require 'syck/rubytypes'
14
+ require 'syck/types'
17
15
 
18
- #--
19
- # For Rubinius, replaces the rb_iterate call to syck_set_ivars.
20
- #++
21
- def self.set_ivars(hsh, obj)
22
- hsh.each do |key, value|
23
- obj.instance_variable_set :"@#{key}", value
24
- end
25
- end
16
+ module Syck
17
+ #--
18
+ # For Rubinius, replaces the rb_iterate call to syck_set_ivars.
19
+ #++
20
+ def self.set_ivars(hsh, obj)
21
+ hsh.each do |key, value|
22
+ obj.instance_variable_set :"@#{key}", value
23
+ end
24
+ end
26
25
 
27
- #--
28
- # For Rubinius, replaces the rb_iterate call to syck_merge_i.
29
- #++
30
- def self.merge_i(ary, hsh)
31
- ary.each do |entry|
32
- begin
33
- entry = Rubinius::Type.coerce_to entry, Hash, :to_hash
34
- hsh.update entry
35
- rescue
36
- # ignore coercion errors
37
- end
38
- end
26
+ #--
27
+ # For Rubinius, replaces the rb_iterate call to syck_merge_i.
28
+ #++
29
+ def self.merge_i(ary, hsh)
30
+ ary.each do |entry|
31
+ begin
32
+ entry = Rubinius::Type.coerce_to entry, Hash, :to_hash
33
+ hsh.update entry
34
+ rescue
35
+ # ignore coercion errors
36
+ end
37
+ end
39
38
 
40
- nil
41
- end
39
+ nil
40
+ end
42
41
 
42
+ #--
43
+ # For Rubinius, replaces rb_syck_mktime.
44
+ #++
45
+ def self.mktime(str)
46
+ require "date"
47
+ begin
48
+ DateTime.parse(str).to_time
49
+ rescue ArgumentError
50
+ # nothing
43
51
  end
52
+ end
44
53
  end
data/rubysl-yaml.gemspec CHANGED
@@ -13,14 +13,12 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.files = `git ls-files`.split($/)
15
15
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
- spec.extensions = ["ext/rubysl/syck/extconf.rb"]
17
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
17
  spec.require_paths = ["lib"]
19
18
 
20
- spec.required_ruby_version = "~> 1.8.7"
19
+ spec.required_ruby_version = "~> 2.0"
21
20
 
22
21
  spec.add_development_dependency "bundler", "~> 1.3"
23
22
  spec.add_development_dependency "rake", "~> 10.0"
24
23
  spec.add_development_dependency "mspec", "~> 1.5"
25
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
26
24
  end
metadata CHANGED
@@ -1,121 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-16 00:00:00.000000000 Z
11
+ date: 2013-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
- version_requirements: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.3'
20
15
  requirement: !ruby/object:Gem::Requirement
21
16
  requirements:
22
- - - "~>"
17
+ - - ~>
23
18
  - !ruby/object:Gem::Version
24
19
  version: '1.3'
25
- prerelease: false
26
20
  type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: rake
21
+ prerelease: false
29
22
  version_requirements: !ruby/object:Gem::Requirement
30
23
  requirements:
31
- - - "~>"
24
+ - - ~>
32
25
  - !ruby/object:Gem::Version
33
- version: '10.0'
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
34
29
  requirement: !ruby/object:Gem::Requirement
35
30
  requirements:
36
- - - "~>"
31
+ - - ~>
37
32
  - !ruby/object:Gem::Version
38
33
  version: '10.0'
39
- prerelease: false
40
34
  type: :development
41
- - !ruby/object:Gem::Dependency
42
- name: mspec
35
+ prerelease: false
43
36
  version_requirements: !ruby/object:Gem::Requirement
44
37
  requirements:
45
- - - "~>"
38
+ - - ~>
46
39
  - !ruby/object:Gem::Version
47
- version: '1.5'
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
44
  requirements:
50
- - - "~>"
45
+ - - ~>
51
46
  - !ruby/object:Gem::Version
52
47
  version: '1.5'
53
- prerelease: false
54
48
  type: :development
55
- - !ruby/object:Gem::Dependency
56
- name: rubysl-prettyprint
49
+ prerelease: false
57
50
  version_requirements: !ruby/object:Gem::Requirement
58
51
  requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '1.0'
62
- requirement: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - "~>"
52
+ - - ~>
65
53
  - !ruby/object:Gem::Version
66
- version: '1.0'
67
- prerelease: false
68
- type: :development
54
+ version: '1.5'
69
55
  description: Ruby standard library YAML.
70
56
  email:
71
57
  - brixen@gmail.com
72
58
  executables: []
73
- extensions:
74
- - ext/rubysl/syck/extconf.rb
59
+ extensions: []
75
60
  extra_rdoc_files: []
76
61
  files:
77
- - ".gitignore"
78
- - ".travis.yml"
62
+ - .gitignore
63
+ - .travis.yml
79
64
  - Gemfile
80
65
  - LICENSE
81
66
  - README.md
82
67
  - Rakefile
83
- - ext/rubysl/syck/bytecode.c
84
- - ext/rubysl/syck/emitter.c
85
- - ext/rubysl/syck/extconf.rb
86
- - ext/rubysl/syck/gram.c
87
- - ext/rubysl/syck/gram.h
88
- - ext/rubysl/syck/handler.c
89
- - ext/rubysl/syck/implicit.c
90
- - ext/rubysl/syck/node.c
91
- - ext/rubysl/syck/rubyext.c
92
- - ext/rubysl/syck/st.c
93
- - ext/rubysl/syck/st.h
94
- - ext/rubysl/syck/syck.c
95
- - ext/rubysl/syck/syck.h
96
- - ext/rubysl/syck/token.c
97
- - ext/rubysl/syck/yaml2byte.c
98
- - ext/rubysl/syck/yamlbyte.h
99
68
  - lib/rubysl/yaml.rb
100
69
  - lib/rubysl/yaml/version.rb
101
70
  - lib/rubysl/yaml/yaml.rb
102
71
  - lib/yaml.rb
103
- - lib/yaml/baseemitter.rb
104
- - lib/yaml/basenode.rb
105
- - lib/yaml/constants.rb
106
72
  - lib/yaml/dbm.rb
107
- - lib/yaml/encoding.rb
108
- - lib/yaml/error.rb
109
- - lib/yaml/loader.rb
110
- - lib/yaml/rubytypes.rb
111
73
  - lib/yaml/store.rb
112
- - lib/yaml/stream.rb
113
- - lib/yaml/stringio.rb
114
74
  - lib/yaml/syck.rb
115
- - lib/yaml/tag.rb
116
- - lib/yaml/types.rb
117
- - lib/yaml/yamlnode.rb
118
- - lib/yaml/ypath.rb
119
75
  - rubysl-yaml.gemspec
120
76
  - spec/add_builtin_type_spec.rb
121
77
  - spec/add_domain_type_spec.rb
@@ -160,17 +116,17 @@ require_paths:
160
116
  - lib
161
117
  required_ruby_version: !ruby/object:Gem::Requirement
162
118
  requirements:
163
- - - "~>"
119
+ - - ~>
164
120
  - !ruby/object:Gem::Version
165
- version: 1.8.7
121
+ version: '2.0'
166
122
  required_rubygems_version: !ruby/object:Gem::Requirement
167
123
  requirements:
168
- - - ">="
124
+ - - '>='
169
125
  - !ruby/object:Gem::Version
170
126
  version: '0'
171
127
  requirements: []
172
128
  rubyforge_project:
173
- rubygems_version: 2.2.2
129
+ rubygems_version: 2.0.7
174
130
  signing_key:
175
131
  specification_version: 4
176
132
  summary: Ruby standard library YAML.
@@ -208,4 +164,3 @@ test_files:
208
164
  - spec/to_yaml_spec.rb
209
165
  - spec/transfer_spec.rb
210
166
  - spec/try_implicit_spec.rb
211
- has_rdoc: