Module: RIO::IF::YAML
- Included in:
- Rio
- Defined in:
- lib/rio/if/yaml.rb
Instance Method Summary collapse
-
#document(*args, &block) ⇒ Object
Select a single yaml document.
-
#documents(*selectors, &block) ⇒ Object
Select documents from a YAML file.
-
#dump(obj) ⇒ Object
Alias for IF::YAML#putobj!.
-
#getobj ⇒ Object
Calls YAML.load.
-
#load ⇒ Object
Calls YAML.load.
-
#object(*args, &block) ⇒ Object
Select a single object.
-
#objects(*selectors, &block) ⇒ Object
Select objects from a YAML file.
-
#putobj(obj) ⇒ Object
Calls YAML.dump, leaving the Rio open.
-
#putobj!(obj) ⇒ Object
Dumps an object to a Rio as with IF::YAML#putobj, and closes the Rio.
-
#skipdocuments(*selectors, &block) ⇒ Object
Reject documents from a YAML file.
-
#skipobjects(*selectors, &block) ⇒ Object
Reject objects from a YAML file.
-
#yaml(&block) ⇒ Object
Puts a Rio in YAML mode.
-
#yaml? ⇒ Boolean
Queries if the Rio is in yaml-mode.
Instance Method Details
#document(*args, &block) ⇒ Object
Select a single yaml document. See #documents, IF::GrandeStream#line and #yaml.
161 |
# File 'lib/rio/if/yaml.rb', line 161 def document(*args,&block) target.document(*args,&block); self end |
#documents(*selectors, &block) ⇒ Object
Select documents from a YAML file. See #yaml and RIO::Doc::INTRO
150 |
# File 'lib/rio/if/yaml.rb', line 150 def documents(*selectors,&block) target.documents(*selectors,&block); self end |
#dump(obj) ⇒ Object
Alias for IF::YAML#putobj!
202 |
# File 'lib/rio/if/yaml.rb', line 202 def dump(obj) target.dump(obj); self end |
#getobj ⇒ Object
Calls YAML.load.
Loads a single YAML object from the stream referenced by the Rio
rio('database.yml').yaml.getobj
See #yaml and RIO::Doc::INTRO
172 |
# File 'lib/rio/if/yaml.rb', line 172 def getobj() target.getobj() end |
#load ⇒ Object
Calls YAML.load.
Loads a single YAML object from the stream referenced by the Rio
rio('database.yml').yaml.load
See #yaml and RIO::Doc::INTRO
182 |
# File 'lib/rio/if/yaml.rb', line 182 def load() target.load() end |
#object(*args, &block) ⇒ Object
Select a single object. See #objects, IF::GrandeStream#line and #yaml.
157 |
# File 'lib/rio/if/yaml.rb', line 157 def object(*args,&block) target.object(*args,&block); self end |
#objects(*selectors, &block) ⇒ Object
Select objects from a YAML file. See #yaml and RIO::Doc::INTRO
142 |
# File 'lib/rio/if/yaml.rb', line 142 def objects(*selectors,&block) target.objects(*selectors,&block); self end |
#putobj(obj) ⇒ Object
Calls YAML.dump, leaving the Rio open.
189 |
# File 'lib/rio/if/yaml.rb', line 189 def putobj(obj) target.putobj(obj); self end |
#putobj!(obj) ⇒ Object
Dumps an object to a Rio as with IF::YAML#putobj, and closes the Rio.
rio('afile.yaml').yaml.putobj!(anobject)
is identical to
rio('afile.yaml').yaml.putobj(anobject).close
199 |
# File 'lib/rio/if/yaml.rb', line 199 def putobj!(obj) target.putobj!(obj); self end |
#skipdocuments(*selectors, &block) ⇒ Object
Reject documents from a YAML file. Calls IF::GrandeStream#skiprows. See #yaml and RIO::Doc::INTRO
154 |
# File 'lib/rio/if/yaml.rb', line 154 def skipdocuments(*selectors,&block) target.skipdocuments(*selectors,&block); self end |
#skipobjects(*selectors, &block) ⇒ Object
Reject objects from a YAML file. Calls IF::GrandeStream#skiprecords. See #yaml and RIO::Doc::INTRO
146 |
# File 'lib/rio/if/yaml.rb', line 146 def skipobjects(*selectors,&block) target.skipobjects(*selectors,&block); self end |
#yaml(&block) ⇒ Object
Puts a Rio in YAML mode.
Rio uses the YAML class from the Ruby standard library to provide support for reading and writing YAML files. Normally using (skip)records
is identical to (skip)lines
because while records
only selects and does not specify the record-type, lines
is the default.
The YAML extension distingishes between items selected using IF::GrandeStream#records, IF::GrandeStream#rows and IF::GrandeStream#lines. Rio returns objects loaded via YAML#load when records
or #objects is used; returns the YAML text as a String when rows
or #documents is used; and returns lines as Strings as normal when lines
is used. records
is the default.
To read a single YAML document, Rio provides #getobj and #load. For example, consider the following partial ‘database.yml’ from the rails distribution:
development:
adapter: mysql
database: rails_development
test:
adapter: mysql
database: rails_test
To get the object represented in the yaml file:
rio('database.yml').yaml.load
==>{"development"=>{"adapter"=>"mysql", "database"=>"rails_development"},
"test"=>{"adapter"=>"mysql", "database"=>"rails_test"}}
Or one could read parts of the file like so:
rio('database.yml').yaml.getobj['development']['database']
==>"rails_development"
Single objects can be written using #putobj and #putobj! which is aliased to #dump
anobject = {
'production' => {
'adapter' => 'mysql',
'database' => 'rails_production',
}
}
rio('afile.yaml').yaml.dump(anobject)
Single objects can be written using IF::GrandeStream#putrec (aliased to IF::YAML#putobj and IF::YAML#dump)
rio('afile.yaml').yaml.putobj(anobject)
Single objects can be loaded using IF::GrandeStream#getrec (aliased to IF::YAML#getobj and IF::YAML#load)
anobject = rio('afile.yaml').yaml.getobj
A Rio in yaml-mode is just like any other Rio. And all the things you can do with any Rio come for free. They can be iterated over using IF::Grande#each and read into an array using IF::Grande#[] just like any other Rio. All the selection criteria are identical also.
Get the first three objects into an array:
array_of_objects = rio('afile.yaml').yaml[0..2]
Iterate over only YAML documents that are a kind_of ::Hash:
rio('afile.yaml').yaml(::Hash) {|ahash| ...}
This takes advantage of the fact that the default for matching records is ===
Selecting records using a Proc can be used as normal:
anarray = rio('afile.yaml').yaml(proc{|anobject| ...}).to_a
One could even use the copy operator to convert a CSV file to a YAML representation of the same data:
rio('afile.yaml').yaml < rio('afile.csv').csv
131 132 133 134 |
# File 'lib/rio/if/yaml.rb', line 131 def yaml(&block) target.yaml(&block); self end |
#yaml? ⇒ Boolean
Queries if the Rio is in yaml-mode. See #yaml
138 |
# File 'lib/rio/if/yaml.rb', line 138 def yaml?() target.yaml? end |