Skip to content

Commit

Permalink
update to_yaml to work without Syck, which is not default in >1.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
powerpak committed Jul 21, 2014
1 parent 8cbb2a1 commit 5943a6e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/ucsc_stitch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,12 @@ def chr_sort(chrs)
chr = chr.sub(/^chr/, '')
if is_roman
chr.roman_to_i rescue chr[0] + 1e7
elsif chr =~ /(\d+(\.\d+)?)$/
elsif chr =~ /(\d+(\.\d+)?)/
$1.to_f
else
# all we got is some opaque letter like chrX
# get its charcode and add it to a large number to put it at the end
chr[0] + 1e7
chr[0].ord + 1e7
end
end
end
Expand Down
33 changes: 19 additions & 14 deletions lib/ucsc_stitch/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'uri'

# prefer the syck YAML engine, although it is not available on rubies >= 2.0
begin YAML::ENGINE.yamler = 'syck'; rescue ArgumentError; end

class Numeric
# Turns a number of seconds into a human-readable duration
def duration
Expand Down Expand Up @@ -73,20 +76,22 @@ def to_yaml_style; self.size < 6 && self.values.map{|x| x.size || 0 }.max < 30 ?
# Replacing the to_yaml function so it'll serialize hashes sorted (by their keys)
# with any keys found in @yaml_key_order pinned to the top and sorted by their order in that array.
# Original function is in /usr/lib/ruby/1.8/yaml/rubytypes.rb
def to_yaml( opts = {} )
@yaml_key_order ||= []
YAML::quick_emit( object_id, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
sorted = sort do |a,b|
a_index = @yaml_key_order.index a[0]
b_index = @yaml_key_order.index b[0]
if a_index && b_index then a_index<=>b_index
elsif a_index && !b_index then -1
elsif b_index && !a_index then 1
else a[0]<=>b[0] ; end
end
sorted.each do |k, v|
map.add( k, v )
if YAML::ENGINE.yamler =='syck' then
def to_yaml( opts = {} )
@yaml_key_order ||= []
YAML::quick_emit( object_id, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
sorted = sort do |a,b|
a_index = @yaml_key_order.index a[0]
b_index = @yaml_key_order.index b[0]
if a_index && b_index then a_index<=>b_index
elsif a_index && !b_index then -1
elsif b_index && !a_index then 1
else a[0]<=>b[0] ; end
end
sorted.each do |k, v|
map.add( k, v )
end
end
end
end
Expand Down

0 comments on commit 5943a6e

Please sign in to comment.