yaml 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/macos.yml +22 -0
- data/.github/workflows/ubuntu-rvm.yml +33 -0
- data/.github/workflows/ubuntu.yml +22 -0
- data/.gitignore +9 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +74 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/yaml.rb +61 -0
- data/lib/yaml/dbm.rb +280 -0
- data/lib/yaml/store.rb +86 -0
- data/yaml.gemspec +23 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df874fef52f9b2ebe10c40fc7a87ec471026ac331a14fc3b99eec4ddd3025f4b
|
4
|
+
data.tar.gz: 7266726cf3f3416c38bfe411f1b583b7a6ce6fd154b5419b2c52b70f27685e8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e116d2e0cf55316961b52f7e7387935dde6d5c21ce0deadf77ba4458d242aeee79c8cbe7d1f9a3d8ad8fddcebfbf4db1a1d6576c3dfee1611a00a7f9df9a690
|
7
|
+
data.tar.gz: c045a78de79a9386aec3775417a9f1236ba555daf6e1885996659f5ecf92c0302245be7e210306fe4977680056ff4f13db506244de89d7baba4f45aa9c1901d0
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: macos
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: macos-latest
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ '2.6.x', '2.5.x', '2.4.x' ]
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@master
|
13
|
+
- name: Set up Ruby
|
14
|
+
uses: actions/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: ${{ matrix.ruby }}
|
17
|
+
- name: Set up Bundler
|
18
|
+
run: gem install bundler --no-document
|
19
|
+
- name: Install dependencies
|
20
|
+
run: bundle install
|
21
|
+
- name: Run test
|
22
|
+
run: rake
|
@@ -0,0 +1,33 @@
|
|
1
|
+
name: ubuntu-rvm
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ 'ruby-head' ]
|
11
|
+
fail-fast: false
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@master
|
14
|
+
- name: Set up RVM
|
15
|
+
run: |
|
16
|
+
curl -sSL https://get.rvm.io | bash
|
17
|
+
- name: Set up Ruby
|
18
|
+
run: |
|
19
|
+
source $HOME/.rvm/scripts/rvm
|
20
|
+
rvm install ${{ matrix.ruby }} --binary
|
21
|
+
rvm --default use ${{ matrix.ruby }}
|
22
|
+
- name: Set up Bundler
|
23
|
+
run: |
|
24
|
+
source $HOME/.rvm/scripts/rvm
|
25
|
+
gem install bundler --no-document
|
26
|
+
- name: Install dependencies
|
27
|
+
run: |
|
28
|
+
source $HOME/.rvm/scripts/rvm
|
29
|
+
bundle install
|
30
|
+
- name: Run test
|
31
|
+
run: |
|
32
|
+
source $HOME/.rvm/scripts/rvm
|
33
|
+
rake
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: ubuntu
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ '2.6.x', '2.5.x', '2.4.x' ]
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@master
|
13
|
+
- name: Set up Ruby
|
14
|
+
uses: actions/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: ${{ matrix.ruby }}
|
17
|
+
- name: Set up Bundler
|
18
|
+
run: gem install bundler --no-document
|
19
|
+
- name: Install dependencies
|
20
|
+
run: bundle install
|
21
|
+
- name: Run test
|
22
|
+
run: rake
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# YAML
|
2
|
+
|
3
|
+
This module provides a Ruby interface for data serialization in YAML format.
|
4
|
+
|
5
|
+
The YAML module is an alias of Psych, the YAML engine for Ruby.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'yaml'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install yaml
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Working with YAML can be very simple, for example:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'yaml'
|
29
|
+
|
30
|
+
# Parse a YAML string
|
31
|
+
YAML.load("--- foo") #=> "foo"
|
32
|
+
|
33
|
+
# Emit some YAML
|
34
|
+
YAML.dump("foo") # => "--- foo\n...\n"
|
35
|
+
{ :a => 'b'}.to_yaml # => "---\n:a: b\n"
|
36
|
+
```
|
37
|
+
|
38
|
+
As the implementation is provided by the Psych library, detailed documentation
|
39
|
+
can be found in that library's docs (also part of standard library).
|
40
|
+
|
41
|
+
## Security
|
42
|
+
|
43
|
+
Do not use YAML to load untrusted data. Doing so is unsafe and could allow
|
44
|
+
malicious input to execute arbitrary code inside your application.
|
45
|
+
|
46
|
+
## History
|
47
|
+
|
48
|
+
Syck was the original for YAML implementation in Ruby's standard library
|
49
|
+
developed by why the lucky stiff.
|
50
|
+
|
51
|
+
You can still use Syck, if you prefer, for parsing and emitting YAML, but you
|
52
|
+
must install the 'syck' gem now in order to use it.
|
53
|
+
|
54
|
+
In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was
|
55
|
+
completely removed with the release of Ruby 2.0.0.
|
56
|
+
|
57
|
+
## More info
|
58
|
+
|
59
|
+
For more advanced details on the implementation see Psych, and also check out
|
60
|
+
http://yaml.org for spec details and other helpful information.
|
61
|
+
|
62
|
+
Psych is maintained by Aaron Patterson on github: https://github.com/tenderlove/psych
|
63
|
+
|
64
|
+
Syck can also be found on github: https://github.com/tenderlove/syck
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
69
|
+
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/yaml.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "yaml"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/yaml.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'psych'
|
5
|
+
rescue LoadError
|
6
|
+
warn "It seems your ruby installation is missing psych (for YAML output).\n" \
|
7
|
+
"To eliminate this warning, please install libyaml and reinstall your ruby.\n",
|
8
|
+
uplevel: 1
|
9
|
+
raise
|
10
|
+
end
|
11
|
+
|
12
|
+
YAML = Psych # :nodoc:
|
13
|
+
|
14
|
+
# YAML Ain't Markup Language
|
15
|
+
#
|
16
|
+
# This module provides a Ruby interface for data serialization in YAML format.
|
17
|
+
#
|
18
|
+
# The YAML module is an alias of Psych, the YAML engine for Ruby.
|
19
|
+
#
|
20
|
+
# == Usage
|
21
|
+
#
|
22
|
+
# Working with YAML can be very simple, for example:
|
23
|
+
#
|
24
|
+
# require 'yaml'
|
25
|
+
# # Parse a YAML string
|
26
|
+
# YAML.load("--- foo") #=> "foo"
|
27
|
+
#
|
28
|
+
# # Emit some YAML
|
29
|
+
# YAML.dump("foo") # => "--- foo\n...\n"
|
30
|
+
# { :a => 'b'}.to_yaml # => "---\n:a: b\n"
|
31
|
+
#
|
32
|
+
# As the implementation is provided by the Psych library, detailed documentation
|
33
|
+
# can be found in that library's docs (also part of standard library).
|
34
|
+
#
|
35
|
+
# == Security
|
36
|
+
#
|
37
|
+
# Do not use YAML to load untrusted data. Doing so is unsafe and could allow
|
38
|
+
# malicious input to execute arbitrary code inside your application. Please see
|
39
|
+
# doc/security.rdoc for more information.
|
40
|
+
#
|
41
|
+
# == History
|
42
|
+
#
|
43
|
+
# Syck was the original for YAML implementation in Ruby's standard library
|
44
|
+
# developed by why the lucky stiff.
|
45
|
+
#
|
46
|
+
# You can still use Syck, if you prefer, for parsing and emitting YAML, but you
|
47
|
+
# must install the 'syck' gem now in order to use it.
|
48
|
+
#
|
49
|
+
# In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was
|
50
|
+
# completely removed with the release of Ruby 2.0.0.
|
51
|
+
#
|
52
|
+
# == More info
|
53
|
+
#
|
54
|
+
# For more advanced details on the implementation see Psych, and also check out
|
55
|
+
# http://yaml.org for spec details and other helpful information.
|
56
|
+
#
|
57
|
+
# Psych is maintained by Aaron Patterson on github: https://github.com/ruby/psych
|
58
|
+
#
|
59
|
+
# Syck can also be found on github: https://github.com/ruby/syck
|
60
|
+
module YAML
|
61
|
+
end
|
data/lib/yaml/dbm.rb
ADDED
@@ -0,0 +1,280 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'yaml'
|
3
|
+
require 'dbm'
|
4
|
+
|
5
|
+
module YAML
|
6
|
+
|
7
|
+
# YAML + DBM = YDBM
|
8
|
+
#
|
9
|
+
# YAML::DBM provides the same interface as ::DBM.
|
10
|
+
#
|
11
|
+
# However, while DBM only allows strings for both keys and values,
|
12
|
+
# this library allows one to use most Ruby objects for values
|
13
|
+
# by first converting them to YAML. Keys must be strings.
|
14
|
+
#
|
15
|
+
# Conversion to and from YAML is performed automatically.
|
16
|
+
#
|
17
|
+
# See the documentation for ::DBM and ::YAML for more information.
|
18
|
+
class DBM < ::DBM
|
19
|
+
VERSION = "0.1" # :nodoc:
|
20
|
+
|
21
|
+
# :call-seq:
|
22
|
+
# ydbm[key] -> value
|
23
|
+
#
|
24
|
+
# Return value associated with +key+ from database.
|
25
|
+
#
|
26
|
+
# Returns +nil+ if there is no such +key+.
|
27
|
+
#
|
28
|
+
# See #fetch for more information.
|
29
|
+
def []( key )
|
30
|
+
fetch( key )
|
31
|
+
end
|
32
|
+
|
33
|
+
# :call-seq:
|
34
|
+
# ydbm[key] = value
|
35
|
+
#
|
36
|
+
# Set +key+ to +value+ in database.
|
37
|
+
#
|
38
|
+
# +value+ will be converted to YAML before storage.
|
39
|
+
#
|
40
|
+
# See #store for more information.
|
41
|
+
def []=( key, val )
|
42
|
+
store( key, val )
|
43
|
+
end
|
44
|
+
|
45
|
+
# :call-seq:
|
46
|
+
# ydbm.fetch( key, ifnone = nil )
|
47
|
+
# ydbm.fetch( key ) { |key| ... }
|
48
|
+
#
|
49
|
+
# Return value associated with +key+.
|
50
|
+
#
|
51
|
+
# If there is no value for +key+ and no block is given, returns +ifnone+.
|
52
|
+
#
|
53
|
+
# Otherwise, calls block passing in the given +key+.
|
54
|
+
#
|
55
|
+
# See ::DBM#fetch for more information.
|
56
|
+
def fetch( keystr, ifnone = nil )
|
57
|
+
begin
|
58
|
+
val = super( keystr )
|
59
|
+
return YAML.load( val ) if String === val
|
60
|
+
rescue IndexError
|
61
|
+
end
|
62
|
+
if block_given?
|
63
|
+
yield keystr
|
64
|
+
else
|
65
|
+
ifnone
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Deprecated, used YAML::DBM#key instead.
|
70
|
+
# ----
|
71
|
+
# Note:
|
72
|
+
# YAML::DBM#index makes warning from internal of ::DBM#index.
|
73
|
+
# It says 'DBM#index is deprecated; use DBM#key', but DBM#key
|
74
|
+
# behaves not same as DBM#index.
|
75
|
+
#
|
76
|
+
def index( keystr )
|
77
|
+
super( keystr.to_yaml )
|
78
|
+
end
|
79
|
+
|
80
|
+
# :call-seq:
|
81
|
+
# ydbm.key(value) -> string
|
82
|
+
#
|
83
|
+
# Returns the key for the specified value.
|
84
|
+
def key( keystr )
|
85
|
+
invert[keystr]
|
86
|
+
end
|
87
|
+
|
88
|
+
# :call-seq:
|
89
|
+
# ydbm.values_at(*keys)
|
90
|
+
#
|
91
|
+
# Returns an array containing the values associated with the given keys.
|
92
|
+
def values_at( *keys )
|
93
|
+
keys.collect { |k| fetch( k ) }
|
94
|
+
end
|
95
|
+
|
96
|
+
# :call-seq:
|
97
|
+
# ydbm.delete(key)
|
98
|
+
#
|
99
|
+
# Deletes value from database associated with +key+.
|
100
|
+
#
|
101
|
+
# Returns value or +nil+.
|
102
|
+
def delete( key )
|
103
|
+
v = super( key )
|
104
|
+
if String === v
|
105
|
+
v = YAML.load( v )
|
106
|
+
end
|
107
|
+
v
|
108
|
+
end
|
109
|
+
|
110
|
+
# :call-seq:
|
111
|
+
# ydbm.delete_if { |key, value| ... }
|
112
|
+
#
|
113
|
+
# Calls the given block once for each +key+, +value+ pair in the database.
|
114
|
+
# Deletes all entries for which the block returns true.
|
115
|
+
#
|
116
|
+
# Returns +self+.
|
117
|
+
def delete_if # :yields: [key, value]
|
118
|
+
del_keys = keys.dup
|
119
|
+
del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
|
120
|
+
del_keys.each { |k| delete( k ) }
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
# :call-seq:
|
125
|
+
# ydbm.reject { |key, value| ... }
|
126
|
+
#
|
127
|
+
# Converts the contents of the database to an in-memory Hash, then calls
|
128
|
+
# Hash#reject with the specified code block, returning a new Hash.
|
129
|
+
def reject
|
130
|
+
hsh = self.to_hash
|
131
|
+
hsh.reject { |k,v| yield k, v }
|
132
|
+
end
|
133
|
+
|
134
|
+
# :call-seq:
|
135
|
+
# ydbm.each_pair { |key, value| ... }
|
136
|
+
#
|
137
|
+
# Calls the given block once for each +key+, +value+ pair in the database.
|
138
|
+
#
|
139
|
+
# Returns +self+.
|
140
|
+
def each_pair # :yields: [key, value]
|
141
|
+
keys.each { |k| yield k, fetch( k ) }
|
142
|
+
self
|
143
|
+
end
|
144
|
+
|
145
|
+
# :call-seq:
|
146
|
+
# ydbm.each_value { |value| ... }
|
147
|
+
#
|
148
|
+
# Calls the given block for each value in database.
|
149
|
+
#
|
150
|
+
# Returns +self+.
|
151
|
+
def each_value # :yields: value
|
152
|
+
super { |v| yield YAML.load( v ) }
|
153
|
+
self
|
154
|
+
end
|
155
|
+
|
156
|
+
# :call-seq:
|
157
|
+
# ydbm.values
|
158
|
+
#
|
159
|
+
# Returns an array of values from the database.
|
160
|
+
def values
|
161
|
+
super.collect { |v| YAML.load( v ) }
|
162
|
+
end
|
163
|
+
|
164
|
+
# :call-seq:
|
165
|
+
# ydbm.has_value?(value)
|
166
|
+
#
|
167
|
+
# Returns true if specified +value+ is found in the database.
|
168
|
+
def has_value?( val )
|
169
|
+
each_value { |v| return true if v == val }
|
170
|
+
return false
|
171
|
+
end
|
172
|
+
|
173
|
+
# :call-seq:
|
174
|
+
# ydbm.invert -> hash
|
175
|
+
#
|
176
|
+
# Returns a Hash (not a DBM database) created by using each value in the
|
177
|
+
# database as a key, with the corresponding key as its value.
|
178
|
+
#
|
179
|
+
# Note that all values in the hash will be Strings, but the keys will be
|
180
|
+
# actual objects.
|
181
|
+
def invert
|
182
|
+
h = {}
|
183
|
+
keys.each { |k| h[ self.fetch( k ) ] = k }
|
184
|
+
h
|
185
|
+
end
|
186
|
+
|
187
|
+
# :call-seq:
|
188
|
+
# ydbm.replace(hash) -> ydbm
|
189
|
+
#
|
190
|
+
# Replaces the contents of the database with the contents of the specified
|
191
|
+
# object. Takes any object which implements the each_pair method, including
|
192
|
+
# Hash and DBM objects.
|
193
|
+
def replace( hsh )
|
194
|
+
clear
|
195
|
+
update( hsh )
|
196
|
+
end
|
197
|
+
|
198
|
+
# :call-seq:
|
199
|
+
# ydbm.shift -> [key, value]
|
200
|
+
#
|
201
|
+
# Removes a [key, value] pair from the database, and returns it.
|
202
|
+
# If the database is empty, returns +nil+.
|
203
|
+
#
|
204
|
+
# The order in which values are removed/returned is not guaranteed.
|
205
|
+
def shift
|
206
|
+
a = super
|
207
|
+
a[1] = YAML.load( a[1] ) if a
|
208
|
+
a
|
209
|
+
end
|
210
|
+
|
211
|
+
# :call-seq:
|
212
|
+
# ydbm.select { |key, value| ... }
|
213
|
+
# ydbm.select(*keys)
|
214
|
+
#
|
215
|
+
# If a block is provided, returns a new array containing [key, value] pairs
|
216
|
+
# for which the block returns true.
|
217
|
+
#
|
218
|
+
# Otherwise, same as #values_at
|
219
|
+
def select( *keys )
|
220
|
+
if block_given?
|
221
|
+
self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
|
222
|
+
else
|
223
|
+
values_at( *keys )
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
# :call-seq:
|
228
|
+
# ydbm.store(key, value) -> value
|
229
|
+
#
|
230
|
+
# Stores +value+ in database with +key+ as the index. +value+ is converted
|
231
|
+
# to YAML before being stored.
|
232
|
+
#
|
233
|
+
# Returns +value+
|
234
|
+
def store( key, val )
|
235
|
+
super( key, val.to_yaml )
|
236
|
+
val
|
237
|
+
end
|
238
|
+
|
239
|
+
# :call-seq:
|
240
|
+
# ydbm.update(hash) -> ydbm
|
241
|
+
#
|
242
|
+
# Updates the database with multiple values from the specified object.
|
243
|
+
# Takes any object which implements the each_pair method, including
|
244
|
+
# Hash and DBM objects.
|
245
|
+
#
|
246
|
+
# Returns +self+.
|
247
|
+
def update( hsh )
|
248
|
+
hsh.each_pair do |k,v|
|
249
|
+
self.store( k, v )
|
250
|
+
end
|
251
|
+
self
|
252
|
+
end
|
253
|
+
|
254
|
+
# :call-seq:
|
255
|
+
# ydbm.to_a -> array
|
256
|
+
#
|
257
|
+
# Converts the contents of the database to an array of [key, value] arrays,
|
258
|
+
# and returns it.
|
259
|
+
def to_a
|
260
|
+
a = []
|
261
|
+
keys.each { |k| a.push [ k, self.fetch( k ) ] }
|
262
|
+
a
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
# :call-seq:
|
267
|
+
# ydbm.to_hash -> hash
|
268
|
+
#
|
269
|
+
# Converts the contents of the database to an in-memory Hash object, and
|
270
|
+
# returns it.
|
271
|
+
def to_hash
|
272
|
+
h = {}
|
273
|
+
keys.each { |k| h[ k ] = self.fetch( k ) }
|
274
|
+
h
|
275
|
+
end
|
276
|
+
|
277
|
+
alias :each :each_pair
|
278
|
+
end
|
279
|
+
|
280
|
+
end
|
data/lib/yaml/store.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
#
|
3
|
+
# YAML::Store
|
4
|
+
#
|
5
|
+
require 'yaml'
|
6
|
+
require 'pstore'
|
7
|
+
|
8
|
+
# YAML::Store provides the same functionality as PStore, except it uses YAML
|
9
|
+
# to dump objects instead of Marshal.
|
10
|
+
#
|
11
|
+
# == Example
|
12
|
+
#
|
13
|
+
# require 'yaml/store'
|
14
|
+
#
|
15
|
+
# Person = Struct.new :first_name, :last_name
|
16
|
+
#
|
17
|
+
# people = [Person.new("Bob", "Smith"), Person.new("Mary", "Johnson")]
|
18
|
+
#
|
19
|
+
# store = YAML::Store.new "test.store"
|
20
|
+
#
|
21
|
+
# store.transaction do
|
22
|
+
# store["people"] = people
|
23
|
+
# store["greeting"] = { "hello" => "world" }
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# After running the above code, the contents of "test.store" will be:
|
27
|
+
#
|
28
|
+
# ---
|
29
|
+
# people:
|
30
|
+
# - !ruby/struct:Person
|
31
|
+
# first_name: Bob
|
32
|
+
# last_name: Smith
|
33
|
+
# - !ruby/struct:Person
|
34
|
+
# first_name: Mary
|
35
|
+
# last_name: Johnson
|
36
|
+
# greeting:
|
37
|
+
# hello: world
|
38
|
+
|
39
|
+
class YAML::Store < PStore
|
40
|
+
|
41
|
+
# :call-seq:
|
42
|
+
# initialize( file_name, yaml_opts = {} )
|
43
|
+
# initialize( file_name, thread_safe = false, yaml_opts = {} )
|
44
|
+
#
|
45
|
+
# Creates a new YAML::Store object, which will store data in +file_name+.
|
46
|
+
# If the file does not already exist, it will be created.
|
47
|
+
#
|
48
|
+
# YAML::Store objects are always reentrant. But if _thread_safe_ is set to true,
|
49
|
+
# then it will become thread-safe at the cost of a minor performance hit.
|
50
|
+
#
|
51
|
+
# Options passed in through +yaml_opts+ will be used when converting the
|
52
|
+
# store to YAML via Hash#to_yaml().
|
53
|
+
def initialize( *o )
|
54
|
+
@opt = {}
|
55
|
+
if o.last.is_a? Hash
|
56
|
+
@opt.update(o.pop)
|
57
|
+
end
|
58
|
+
super(*o)
|
59
|
+
end
|
60
|
+
|
61
|
+
# :stopdoc:
|
62
|
+
|
63
|
+
def dump(table)
|
64
|
+
table.to_yaml(@opt)
|
65
|
+
end
|
66
|
+
|
67
|
+
def load(content)
|
68
|
+
table = YAML.load(content)
|
69
|
+
if table == false
|
70
|
+
{}
|
71
|
+
else
|
72
|
+
table
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def marshal_dump_supports_canonical_option?
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
def empty_marshal_data
|
81
|
+
{}.to_yaml(@opt)
|
82
|
+
end
|
83
|
+
def empty_marshal_checksum
|
84
|
+
CHECKSUM_ALGO.digest(empty_marshal_data)
|
85
|
+
end
|
86
|
+
end
|
data/yaml.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "yaml"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Aaron Patterson", "SHIBATA Hiroshi"]
|
5
|
+
spec.email = ["aaron@tenderlovemaking.com", "hsbt@ruby-lang.org"]
|
6
|
+
|
7
|
+
spec.summary = "YAML Ain't Markup Language"
|
8
|
+
spec.description = spec.summary
|
9
|
+
spec.homepage = "https://github.com/ruby/yaml"
|
10
|
+
spec.license = "BSD-2-Clause"
|
11
|
+
|
12
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
13
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Patterson
|
8
|
+
- SHIBATA Hiroshi
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-04-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: YAML Ain't Markup Language
|
15
|
+
email:
|
16
|
+
- aaron@tenderlovemaking.com
|
17
|
+
- hsbt@ruby-lang.org
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".github/workflows/macos.yml"
|
23
|
+
- ".github/workflows/ubuntu-rvm.yml"
|
24
|
+
- ".github/workflows/ubuntu.yml"
|
25
|
+
- ".gitignore"
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- lib/yaml.rb
|
33
|
+
- lib/yaml/dbm.rb
|
34
|
+
- lib/yaml/store.rb
|
35
|
+
- yaml.gemspec
|
36
|
+
homepage: https://github.com/ruby/yaml
|
37
|
+
licenses:
|
38
|
+
- BSD-2-Clause
|
39
|
+
metadata:
|
40
|
+
homepage_uri: https://github.com/ruby/yaml
|
41
|
+
source_code_uri: https://github.com/ruby/yaml
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.2.0.pre1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: YAML Ain't Markup Language
|
61
|
+
test_files: []
|