<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: yaml code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 12 May 2008 07:55:38 GMT</pubDate>
    <description>DZone Snippets: yaml code</description>
    <item>
      <title>Save db data to fixture files</title>
      <link>http://snippets.dzone.com/posts/show/4729</link>
      <description>rake db:fixtures:dump_all &lt;br /&gt;&#1086;&#1085; &#1089;&#1086;&#1093;&#1088;&#1072;&#1085;&#1080;&#1090; &#1074;&#1089;&#1077; &#1076;&#1072;&#1085;&#1085;&#1099;&#1077; &#1080;&#1079; &#1074;&#1072;&#1096;&#1080;&#1093; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094; development &#1073;&#1072;&#1079;&#1099; &#1074; &#1103;&#1084;&#1083;&#1099; &#1076;&#1083;&#1103; &#1090;&#1077;&#1089;&#1090;&#1086;&#1074; rake &lt;br /&gt;&lt;br /&gt;db:fixtures:dump_references &lt;br /&gt;["areas","countries"]&lt;br /&gt;&#1089;&#1086;&#1093;&#1088;&#1072;&#1085;&#1080;&#1090; &#1090;&#1086;&#1083;&#1100;&#1082;&#1086; &#1090;&#1077; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094;&#1099; &#1082;&#1086;&#1090;&#1086;&#1088;&#1099; &#1077;&#1087;&#1088;&#1086;&#1087;&#1080;&#1089;&#1072;&#1085;&#1099; &#1091; &#1085;&#1077;&#1075;&#1086; &#1074; &#1082;&#1086;&#1085;&#1092;&#1080;&#1075;&#1077;, &#1074; &#1076;&#1072;&#1085;&#1085;&#1086;&#1084; &#1089;&#1083;&#1091;&#1095;&#1072;&#1077; &#1101;&#1090;&#1086; &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  namespace :fixtures do&lt;br /&gt;&lt;br /&gt;    desc 'Create YAML test fixtures from data in an existing database.&lt;br /&gt;Defaults to development database. Set RAILS_ENV to override.'&lt;br /&gt;    task :dump_all =&gt; :environment do&lt;br /&gt;      sql = "SELECT * FROM %s"&lt;br /&gt;      skip_tables = ["schema_info"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  namespace :fixtures do&lt;br /&gt;    desc 'Create YAML test fixtures for references. Defaults to development database. &lt;br /&gt;    Set RAILS_ENV to override.'&lt;br /&gt;    task :dump_references =&gt; :environment do&lt;br /&gt;      sql = "SELECT * FROM %s"&lt;br /&gt;      dump_tables = ["areas","countries"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      dump_tables.each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        file_name = "#{RAILS_ROOT}/test/fixtures/#{table_name}.yml"&lt;br /&gt;        p "Fixture save for table #{table_name} to #{file_name}"&lt;br /&gt;        File.open(file_name, 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Nov 2007 09:30:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4729</guid>
      <author>bublik (Voloshin Ruslan)</author>
    </item>
    <item>
      <title>DRYing up YAML fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3114</link>
      <description>// Rails Recipes explained how to DRY up the database configuration code. I applied the same idea to user fixtures, which worked while we used MySQL. Once on Postgres, "defaults" started throwing an error. The easiest solution was to make quentin's values the 'defaults'&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;quentin: &amp;defaults&lt;br /&gt;  id: 1&lt;br /&gt;  login: quentin&lt;br /&gt;  email: quentin@example.com&lt;br /&gt;  site_id: 1&lt;br /&gt;  salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd&lt;br /&gt;  crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test&lt;br /&gt;  created_at: &lt;%= 5.days.ago.to_s :db %&gt;&lt;br /&gt;  activated_at: &lt;%= 5.days.ago.to_s :db %&gt; # only if you're activating new signups&lt;br /&gt;aaron:&lt;br /&gt;  id: 2&lt;br /&gt;  login: aaron&lt;br /&gt;  email: aaron@example.com&lt;br /&gt;  activation_code: aaronscode&lt;br /&gt;  site_id: 1&lt;br /&gt;  &lt;&lt;: *defaults&lt;br /&gt;#etc...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Dec 2006 03:12:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3114</guid>
      <author>chebuctonian (Daniel Haran)</author>
    </item>
    <item>
      <title>Create YAML test fixtures from database in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2525</link>
      <description>As found at &lt;a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake"&gt;http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;&lt;br /&gt;task :extract_fixtures =&gt; :environment do&lt;br /&gt;  sql  = "SELECT * FROM %s"&lt;br /&gt;  skip_tables = ["schema_info"]&lt;br /&gt;  ActiveRecord::Base.establish_connection&lt;br /&gt;  (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;    i = "000"&lt;br /&gt;    File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;      data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;      file.write data.inject({}) { |hash, record|&lt;br /&gt;        hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;        hash&lt;br /&gt;      }.to_yaml&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Sep 2006 01:48:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2525</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Accessing Subversion repository information in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2374</link>
      <description>&lt;code&gt;&lt;br /&gt;require 'yaml'&lt;br /&gt;svn_info = YAML.load(`svn info /my/working/copy`)&lt;br /&gt;puts svn_info['URL']&lt;br /&gt;#=&gt; "svn://some-remote-repository.com/svn"&lt;br /&gt;puts svn_info['Revision']&lt;br /&gt;#=&gt; "133"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Should also work with svk info as well. Credit to htonl in #caboose for the YAML tip.</description>
      <pubDate>Mon, 07 Aug 2006 23:05:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2374</guid>
      <author>lukeredpath (Luke Redpath)</author>
    </item>
    <item>
      <title>FileMetaData: simple generic file_info metadata comment</title>
      <link>http://snippets.dzone.com/posts/show/933</link>
      <description>This is a general-purpose default text snippet that &lt;br /&gt;you can use to add metadata to any file. It is based&lt;br /&gt;on YAML.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;### begin_: file metadata&lt;br /&gt;    ### &lt;region-file_info&gt;&lt;br /&gt;    ### main:&lt;br /&gt;    ###   - name : ___filename___&lt;br /&gt;    ###     desc : |&lt;br /&gt;    ###         ___description___&lt;br /&gt;    ###     date : created="___date___"&lt;br /&gt;    ###     last : lastmod="___last___"&lt;br /&gt;    ###     lang : ___lang___&lt;br /&gt;    ###     tags : ___tags___&lt;br /&gt;    ### &lt;/region-file_info&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Dec 2005 07:27:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/933</guid>
      <author>drefty (drefty)</author>
    </item>
    <item>
      <title>quickly testing yaml syntax</title>
      <link>http://snippets.dzone.com/posts/show/562</link>
      <description>&lt;code&gt;&lt;br /&gt;irb --simple-prompt -r yaml&lt;br /&gt;&gt;&gt; d = YAML.load &lt;&lt;-TEXT&lt;br /&gt;line_with_breaks: &gt;&lt;br /&gt;  My name is Simon &lt;br /&gt;  Things I like:&lt;br /&gt;    * drawings&lt;br /&gt;TEXT&lt;br /&gt;=&gt; {"line_with_breaks"=&gt;"My name is Simon Things I like:\n  * drawings\n"}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;ripped from noradio</description>
      <pubDate>Sun, 07 Aug 2005 04:20:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/562</guid>
      <author>technoweenie (Rick Olson)</author>
    </item>
    <item>
      <title>App level configuration for your Rails apps</title>
      <link>http://snippets.dzone.com/posts/show/551</link>
      <description>Hard coding stuff into your view templates isn't a great idea, and sometimes you want it out of your layouts too. If you have variables your app requires, but which may change between deployments, put them in /config/appconfig.yml in YAML format, then put this at the top of your /config/environment.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;require 'yaml'&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And at the bottom of your /config/environment.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;APP_CONFIG = YAML::load(File.open("#{RAILS_ROOT}/config/appconfig.yml"))&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now your YAML variables are accessible anywhere from your controllers or views like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;APP_CONFIG["variable"]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Easy peasy.</description>
      <pubDate>Fri, 05 Aug 2005 04:14:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/551</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Check that database.yml is parsed properly</title>
      <link>http://snippets.dzone.com/posts/show/477</link>
      <description>You can confirm that your database YAML file is being parsed properly with this one line shell command.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;ruby -ryaml -e "File.open('config/database.yml') { |f| puts YAML.load(f).inspect }"&lt;/code&gt;</description>
      <pubDate>Sat, 16 Jul 2005 08:37:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/477</guid>
      <author>leeo (Lee O'Mara)</author>
    </item>
    <item>
      <title>Loading and parsing YAML files</title>
      <link>http://snippets.dzone.com/posts/show/287</link>
      <description>I use YAML heavily to specify basic configuration options for my applications.  Additionally, I have bash scripts to download and parse currency and weather information, which all gets stored as YAML for ease of use.&lt;br /&gt;&lt;br /&gt;This is how I parse the YAML into my applications:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;require 'yaml'&lt;br /&gt;&lt;br /&gt;class ApplicationController &lt; ActionController::Base&lt;br /&gt;  before_filter :configure_app&lt;br /&gt;&lt;br /&gt;  # Your code here...&lt;br /&gt;&lt;br /&gt;  def configure_app&lt;br /&gt;    @config = YAML::load(File.open("#{RAILS_ROOT}/config/config.yml"))&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 15 May 2005 11:20:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/287</guid>
      <author>darryl_ring (Darryl A. Ring)</author>
    </item>
  </channel>
</rss>
