基於YAML的,這裏是如何解析它:
asdf = YAML.load('table:
- name: table_a
select:
- id: source.id
- code: source.code
- name: source.name
- is_active: TRUE
- created_at: Time.now()
- updated_at: Time.now()')
=> {"table"=>[{"name"=>"table_a"}],
"select"=>
[{"id"=>"source.id"},
{"code"=>"source.code"},
{"name"=>"source.name"},
{"is_active"=>true},
{"created_at"=>"Time.now()"},
{"updated_at"=>"Time.now()"}]}
你YAML也沒有創造一個容易解析的數據結構,它是讓你赴湯蹈火訪問元素:
asdf['table'][0]['name']
=> "table_a"
asdf['select'][4]['created_at']
=> "Time.now()"
相反,它應該是這樣的:
table:
name: table_a
select:
id: source.id
code: source.code
name: source.name
is_active: TRUE
created_at: Time.now()
updated_at: Time.now()
其中,解析後,將創建的哈希哈希看起來像:
{
"table"=>{
"name" => "table_a"
},
"select" =>
{
"id" => "source.id",
"code" => "source.code",
"name" => "source.name",
"is_active" => true,
"created_at" => "Time.now()",
"updated_at" => "Time.now()"
}
}
這可以讓你輕鬆,也很直觀,訪問內容:
asdf['table']
=> {"name"=>"table_a"}
asdf['select']['created_at']
=> "Time.now()"
YAML不會把字符串"Time.now()"
成一個Ruby Time.now
方法調用,所以編碼字符串進入YAML數據將無濟於事。
相反,解析之後,使用方法:
time_now = Time.now
select = asdf['select']
select['created_at'] = time_now
select['updated_at'] = time_now
您可以通過-操作前傳入YAML,然後解析它更新您的"Time.now()"
字符串:
yaml_string = '
table:
name: table_a
select:
id: source.id
code: source.code
name: source.name
is_active: TRUE
created_at: Time.now()
updated_at: Time.now()
'
yaml_string.gsub!('Time.now()', Time.now.to_s)
導致:
table:
name: table_a
select:
id: source.id
code: source.code
name: source.name
is_active: TRUE
created_at: 2012-12-28 10:09:21 -0700
updated_at: 2012-12-28 10:09:21 -0700
解析它現在返回:
=> {"table"=>{"name"=>"table_a"},
"select"=>
{"id"=>"source.id",
"code"=>"source.code",
"name"=>"source.name",
"is_active"=>true,
"created_at"=>2012-12-28 10:09:21 -0700,
"updated_at"=>2012-12-28 10:09:21 -0700}}
而且YAML可以做到與時間價值的東西,因爲它認識它:
[14] pry(main)> asdf['select']['created_at']
=> 2012-12-28 10:09:21 -0700
[15] pry(main)> asdf['select']['created_at'].class
=> Time
此外,而不是寫自己的SQL,我強烈建議使用ORM,像Sequel或DataMapper。我贊同Sequel,但無論如何,優勢在於您不必編寫查詢,ORM就是這樣做的。你告訴它要訪問哪些數據庫和表,並找出模式和關係。如果你使用Rails,ActiveRecord是一個很好的ORM,它與Rails結合在一起。它可以單獨使用,但如果您不使用Rails,我會先推薦其他兩個。
你究竟在爲什麼而掙扎?你有樣品代碼嗎?你試過什麼了? –
我看不到任何試圖讀取YAML數據的代碼。而且,什麼是「HASH數組」? –
我編輯我的問題,包括示例代碼。我是Ruby新手,如果我不瞭解基礎知識,請耐心等待。 – mevdiven