sycl 1.3 → 1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/sycl.rb +55 -9
  2. metadata +4 -4
data/lib/sycl.rb CHANGED
@@ -124,11 +124,14 @@ module Sycl
124
124
  # puts a.to_yaml # outputs '[Alpha, Bravo, Charlie, Delta]'
125
125
 
126
126
  class Array < ::Array
127
+
128
+ @@default_sorting = true
129
+
127
130
  def initialize(*args) # :nodoc:
128
131
  @yaml_preprocessor = nil
129
132
  @yaml_postprocessor = nil
130
133
  @yaml_style = nil
131
- @render_sorted = true
134
+ @render_sorted = @@default_sorting
132
135
  super
133
136
  end
134
137
 
@@ -153,6 +156,17 @@ module Sycl
153
156
  retval
154
157
  end
155
158
 
159
+ # Set Default Array Sorting. In some cases we want to instantiate
160
+ # all sycl objects with sorting defaulted to either true or false.
161
+ #
162
+ # Example:
163
+ #
164
+ # Sycl::Array.default_sorting = false
165
+
166
+ def self.default_sorting=(sort)
167
+ @@default_sorting = sort
168
+ end
169
+
156
170
 
157
171
  # Make sure that if we write to this array, we promote any inputs
158
172
  # to their Sycl equivalents. This lets dot notation, styled YAML,
@@ -269,7 +283,6 @@ module Sycl
269
283
  end
270
284
  end
271
285
 
272
-
273
286
  # Do not sort this array when it is rendered as YAML. Usually we want
274
287
  # elements sorted so that diffs are human-readable, however, there are
275
288
  # certain cases where array ordering is significant (for example, a
@@ -279,6 +292,12 @@ module Sycl
279
292
  @render_sorted = false
280
293
  end
281
294
 
295
+ # Sort this array when it is rendered as YAML. Useful when the default_sorting
296
+ # has been set to false and arrays should be sorted.
297
+
298
+ def render_sorted!
299
+ @render_sorted = true
300
+ end
282
301
 
283
302
  # Set a preprocessor hook which runs before each time YAML is
284
303
  # dumped, for example, via to_yaml() or Sycl::dump(). The hook is a
@@ -547,15 +566,30 @@ module Sycl
547
566
 
548
567
  include Comparable
549
568
 
550
- def <=>(another) # :nodoc:
551
- self.to_str <=> another.to_str
552
- end
553
-
554
- def to_str # :nodoc:
555
- self.keys.sort.first
569
+ def <=>(other) # :nodoc:
570
+ self_keys = self.keys.sort
571
+ other_keys = other.respond_to?(:keys) ? other.keys.sort :
572
+ other.respond_to?(:sort) ? other.sort :
573
+ other.respond_to?(:to_s) ? [other.to_s] :
574
+ other ? [other] : []
575
+
576
+ while true
577
+ if self_keys.empty? && other_keys.empty?
578
+ return 0
579
+ elsif self_keys.empty?
580
+ return 1
581
+ elsif other_keys.empty?
582
+ return -1
583
+ else
584
+ self_key = self_keys.shift
585
+ other_key = other_keys.shift
586
+ if self_key != other_key
587
+ return self_key <=> other_key
588
+ end
589
+ end
590
+ end
556
591
  end
557
592
 
558
-
559
593
  # Make this hash, and its children, rendered in inline/flow style.
560
594
  # The default is to render arrays in block (multi-line) style.
561
595
 
@@ -674,3 +708,15 @@ module Sycl
674
708
 
675
709
  end
676
710
  end
711
+
712
+ class String
713
+ alias_method :original_comparator, :<=>
714
+
715
+ def <=>(other)
716
+ if other.is_a?(Sycl::Hash)
717
+ -1 * (other <=> self)
718
+ else
719
+ self.__send__(:original_comparator, other)
720
+ end
721
+ end
722
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sycl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 3
9
- version: "1.3"
8
+ - 4
9
+ version: "1.4"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Ho
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-10-15 00:00:00 +00:00
17
+ date: 2013-07-17 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies: []
20
20