ruby on rails - How to specify markers in YAML file -


i have yaml file going parsed 2 different machines, want specify sort of markers in file indicate machine has right read specific block. example want block 1 parsed machine 1 , block 2 parsed machine2:

 # block 1 - machine 1  -   :id: 1234   :worker: foo1   :opts:     :ftpaccount: user1     :limit: 10  # block 2 - machine 2  -   :id: 5678   :worker: foo2   :opts:     :ftpaccount: user2     :limit: 10 

how can achieve this? how implement similar this? thanks.

treat blocks hash entries key being hostname:

require 'yaml'  yaml = <<eot host1:   # block 1 - machine 1   -     :id: 1234     :worker: foo1     :opts:       :ftpaccount: user1       :limit: 10  host2:   # block 2 - machine 2   -     :id: 5678     :worker: foo2     :opts:       :ftpaccount: user2       :limit: 10  eot  config = yaml.load(yaml) # => {"host1"=> #      [{:id=>1234, #        :worker=>"foo1", #        :opts=>{:ftpaccount=>"user1", :limit=>10}}], #     "host2"=> #      [{:id=>5678, #        :worker=>"foo2", #        :opts=>{:ftpaccount=>"user2", :limit=>10}}]} 

at point can grab chunk need:

config['host1'] # => [{:id=>1234, :worker=>"foo1", :opts=>{:ftpaccount=>"user1", :limit=>10}}]  config['host2'] # => [{:id=>5678, :worker=>"foo2", :opts=>{:ftpaccount=>"user2", :limit=>10}}] 

you don't have hard-code hostname; can ask machine name is:

`hostname`.chomp # => "myhost" 

actually, i'd change yaml little, it's hash of hashes. is, yaml returns hash of arrays of hashes, which, because of array, makes more awkward use:

host1:   # block 1 - machine 1   :id: 1234   :worker: foo1   :opts:     :ftpaccount: user1     :limit: 10  host2:   # block 2 - machine 2   :id: 5678   :worker: foo2   :opts:     :ftpaccount: user2     :limit: 10 

results in:

config = yaml.load(yaml) # => {"host1"=> #      {:id=>1234, :worker=>"foo1", :opts=>{:ftpaccount=>"user1", :limit=>10}}, #     "host2"=> #      {:id=>5678, :worker=>"foo2", :opts=>{:ftpaccount=>"user2", :limit=>10}}}  config['host1'] # => {:id=>1234, :worker=>"foo1", :opts=>{:ftpaccount=>"user1", :limit=>10}}  config['host2'] # => {:id=>5678, :worker=>"foo2", :opts=>{:ftpaccount=>"user2", :limit=>10}} 

finally, if yaml file complex, or long, or has repeated sections, consider writing code emits file you. ruby makes easy generate yaml in smart way automatically uses aliases. instance:

require 'yaml'  some_common_data = {   'shared_db_dsn' => 'mysql://user:password@host/db' }  host1 = 'foo.com' host1_data = {   host1 => {     'id' => 1234,     'worker' => 'foo1',     'opts' => {       'ftpaccount' => 'user1',       'limit' => 10     },     'dsn' => some_common_data   } }  host2 = 'bar.com' host2_data = {   host2 => {     'id' => 5678,     'worker' => 'foo2',     'opts' => {       'ftpaccount' => 'user2',       'limit' => 10     },     'dsn' => some_common_data   } }  data = {   host1 => host1_data,   host2 => host2_data, }  puts data.to_yaml # >> --- # >> foo.com: # >>   foo.com: # >>     id: 1234 # >>     worker: foo1 # >>     opts: # >>       ftpaccount: user1 # >>       limit: 10 # >>     dsn: &1 # >>       shared_db_dsn: mysql://user:password@host/db # >> bar.com: # >>   bar.com: # >>     id: 5678 # >>     worker: foo2 # >>     opts: # >>       ftpaccount: user2 # >>       limit: 10 # >>     dsn: *1 

notice how yaml converted "dsn" alias , referenced in second host's definition using anchor. can add serious space savings, depending on how define variables , build data structure. see "aliases , anchors" more information.

also, i'd highly recommend avoiding use of symbols hash keys. doing yaml can loaded other languages, not ruby. @ point, yaml becomes more useful when building big systems.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -