Ruby:来自YAML的列名和值对的哈希数组
我有这个YAML条目Ruby:来自YAML的列名和值对的哈希数组,ruby,arrays,yaml,Ruby,Arrays,Yaml,我有这个YAML条目 table: - name: table_a select: - id: source.id - code: source.code - name: source.name - is_active: TRUE 我需要将其读入一个散列数组,然后像这样构造一个SELECT SQL语句 SELECT source.id as id, source.code as code, source.name as name, TRUE as is_a
table:
- name: table_a
select:
- id: source.id
- code: source.code
- name: source.name
- is_active: TRUE
我需要将其读入一个散列数组,然后像这样构造一个SELECT SQL语句
SELECT
source.id as id,
source.code as code,
source.name as name,
TRUE as is_active
FROM table_a
我正在努力动态访问散列、键、值对
我使用了这个代码
yml = YAML.load_file("file.yml")
yml.each_pair { |key, value| puts "#{key} = #{value}"}
但是当我使用
yml['select']时,每对
我都会得到“未定义的方法'each'u pair'”错误 基于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()"
asdf['table']
=> {"name"=>"table_a"}
asdf['select']['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'][0]['name']
=> "table_a"
asdf['select'][4]['created_at']
=> "Time.now()"
asdf['table']
=> {"name"=>"table_a"}
asdf['select']['created_at']
=> "Time.now()"
YAML不会将字符串“Time.now()”
转换为RubyTime.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
此外,我强烈建议您使用ORM,比如or,而不是编写自己的SQL。我喜欢Sequel,但无论是哪种情况,优点都是您不必编写查询,ORM可以。您可以告诉它要访问哪些数据库和表,然后它会找出模式和关系。如果您使用的是Rails,ActiveRecord是一个很好的ORM,它与Rails相结合。它可以单独使用,但如果您不使用Rails,我建议您先使用另外两个。您特别需要解决什么问题?你有样本代码吗?到目前为止,您尝试了什么?我没有看到试图读取YAML数据的代码。什么是“散列数组”?我编辑了我的问题以包含示例代码。我是Ruby新手,所以如果我不了解基本知识,请容忍我。你控制YAML吗?它的设计很差,正在创建一个散列元素数组,使您很难访问单个
select
条目。是的,我创建了YAML。每个YAML文件代表我数据库中的一个表,用于创建动态表加载脚本。我不能使用select['created_at'],因为散列的“key”具有动态值,在运行时会发生变化。如果您的key在运行时发生变化,那么您将不得不编写一些非常动态的Ruby。使用数据库时,键是字段名,永远不会更改,与键关联的值是字段内容。如果您的键发生了变化,您的语句听起来确实像是对代码如何与数据库通信和/或某些编写糟糕的代码的混淆。