storable 0.5.1 → 0.5.2

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.
data/CHANGES.txt CHANGED
@@ -1,6 +1,12 @@
1
1
  STORABLE, CHANGES
2
2
 
3
3
 
4
+ #### 0.5.2 (2009-05-12) #############################
5
+
6
+ * CHANGE: Put OrderedHash into Storable namespace and imported merge fix from Caesars
7
+ * FIXED: Circular dependency with Sysinfo
8
+
9
+
4
10
  #### 0.5.1 (2009-05-07) #############################
5
11
 
6
12
  * FIXED: Bug in from_hash which was incorrectly parsing some data types (incl. Integer)
data/README.rdoc CHANGED
@@ -46,6 +46,7 @@ or via download:
46
46
  == Credits
47
47
 
48
48
  * Delano Mandelbaum (delano@solutious.com)
49
+ * OrderedHash implementation by Jan Molic
49
50
 
50
51
  == License
51
52
 
data/lib/storable.rb CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
 
7
7
  USE_ORDERED_HASH = (RUBY_VERSION =~ /1.9/).nil?
8
- require 'orderedhash' if USE_ORDERED_HASH
8
+
9
9
  require 'json' rescue nil
10
10
 
11
11
  require 'yaml'
@@ -16,6 +16,7 @@ require 'fileutils'
16
16
  # Storable.field method which tells Storable the order and
17
17
  # name.
18
18
  class Storable
19
+ require 'storable/orderedhash' if USE_ORDERED_HASH
19
20
  unless defined?(SUPPORTED_FORMATS) # We can assume all are defined
20
21
  VERSION = 0.5
21
22
  NICE_TIME_FORMAT = "%Y-%m-%d@%H:%M:%S".freeze
@@ -167,7 +168,7 @@ class Storable
167
168
  # Return the object data as a hash
168
169
  # +with_titles+ is ignored.
169
170
  def to_hash(with_titles=true)
170
- tmp = USE_ORDERED_HASH ? OrderedHash.new : {}
171
+ tmp = USE_ORDERED_HASH ? Storable::OrderedHash.new : {}
171
172
  field_names.each do |fname|
172
173
  tmp[fname] = self.send(fname)
173
174
  end
@@ -187,10 +188,9 @@ class Storable
187
188
  end
188
189
 
189
190
  # Create a new instance of the object from a JSON string.
190
- # +from+ a JSON string split into an array by line.
191
- def self.from_json(from=[])
192
- # from is an array of strings
193
- from_str = from.join('')
191
+ # +from+ a YAML String or Array (split into by line).
192
+ def self.from_json(*from)
193
+ from_str = [from].flatten.compact.join('')
194
194
  tmp = JSON::load(from_str)
195
195
  hash_sym = tmp.keys.inject({}) do |hash, key|
196
196
  hash[key.to_sym] = tmp[key]
@@ -0,0 +1,199 @@
1
+ # AUTHOR
2
+ # jan molic /mig/at/1984/dot/cz/
3
+ #
4
+ # DESCRIPTION
5
+ # Hash with preserved order and some array-like extensions
6
+ # Public domain.
7
+ #
8
+ # THANKS
9
+ # Andrew Johnson for his suggestions and fixes of Hash[],
10
+ # merge, to_a, inspect and shift
11
+ class Storable::OrderedHash < ::Hash
12
+ attr_accessor :order
13
+
14
+ class << self
15
+ def [] *args
16
+ hsh = Storable::OrderedHash.new
17
+ if Hash === args[0]
18
+ hsh.replace args[0]
19
+ elsif (args.size % 2) != 0
20
+ raise ArgumentError, "odd number of elements for Hash"
21
+ else
22
+ 0.step(args.size - 1, 2) do |a|
23
+ b = a + 1
24
+ hsh[args[a]] = args[b]
25
+ end
26
+ end
27
+ hsh
28
+ end
29
+ end
30
+ def initialize(*a, &b)
31
+ super
32
+ @order = []
33
+ end
34
+ def store_only a,b
35
+ store a,b
36
+ end
37
+ alias orig_store store
38
+ def store a,b
39
+ @order.push a unless has_key? a
40
+ super a,b
41
+ end
42
+ alias []= store
43
+ def == hsh2
44
+ return false if @order != hsh2.order
45
+ super hsh2
46
+ end
47
+ def clear
48
+ @order = []
49
+ super
50
+ end
51
+ def delete key
52
+ @order.delete key
53
+ super
54
+ end
55
+ def each_key
56
+ @order.each { |k| yield k }
57
+ self
58
+ end
59
+ def each_value
60
+ @order.each { |k| yield self[k] }
61
+ self
62
+ end
63
+ def each
64
+ @order.each { |k| yield k,self[k] }
65
+ self
66
+ end
67
+ alias each_pair each
68
+ def delete_if
69
+ @order.clone.each { |k|
70
+ delete k if yield(k)
71
+ }
72
+ self
73
+ end
74
+ def values
75
+ ary = []
76
+ @order.each { |k| ary.push self[k] }
77
+ ary
78
+ end
79
+ def keys
80
+ @order
81
+ end
82
+ def first
83
+ {@order.first => self[@order.first]}
84
+ end
85
+ def last
86
+ {@order.last => self[@order.last]}
87
+ end
88
+ def invert
89
+ hsh2 = Hash.new
90
+ @order.each { |k| hsh2[self[k]] = k }
91
+ hsh2
92
+ end
93
+ def reject &block
94
+ self.dup.delete_if &block
95
+ end
96
+ def reject! &block
97
+ hsh2 = reject &block
98
+ self == hsh2 ? nil : hsh2
99
+ end
100
+ def replace hsh2
101
+ @order = hsh2.keys
102
+ super hsh2
103
+ end
104
+ def shift
105
+ key = @order.first
106
+ key ? [key,delete(key)] : super
107
+ end
108
+ def unshift k,v
109
+ unless self.include? k
110
+ @order.unshift k
111
+ orig_store(k,v)
112
+ true
113
+ else
114
+ false
115
+ end
116
+ end
117
+ def push k,v
118
+ unless self.include? k
119
+ @order.push k
120
+ orig_store(k,v)
121
+ true
122
+ else
123
+ false
124
+ end
125
+ end
126
+ def pop
127
+ key = @order.last
128
+ key ? [key,delete(key)] : nil
129
+ end
130
+ def to_a
131
+ ary = []
132
+ each { |k,v| ary << [k,v] }
133
+ ary
134
+ end
135
+ def to_s
136
+ self.to_a.to_s
137
+ end
138
+ def inspect
139
+ ary = []
140
+ each {|k,v| ary << k.inspect + "=>" + v.inspect}
141
+ '{' + ary.join(", ") + '}'
142
+ end
143
+ def update hsh2
144
+ hsh2.each { |k,v| self[k] = v }
145
+ self
146
+ end
147
+ alias :merge! update
148
+ def merge hsh2
149
+ ##self.dup update(hsh2) ## 2009-05-12 -- delano
150
+ update hsh2 ## dup doesn't take an argument
151
+ ## and there's no need for it here
152
+ end
153
+ def select
154
+ ary = []
155
+ each { |k,v| ary << [k,v] if yield k,v }
156
+ ary
157
+ end
158
+ def class
159
+ Hash
160
+ end
161
+ def __class__
162
+ Storable::OrderedHash
163
+ end
164
+
165
+ attr_accessor "to_yaml_style"
166
+ def yaml_inline= bool
167
+ if respond_to?("to_yaml_style")
168
+ self.to_yaml_style = :inline
169
+ else
170
+ unless defined? @__yaml_inline_meth
171
+ @__yaml_inline_meth =
172
+ lambda {|opts|
173
+ YAML::quick_emit(object_id, opts) {|emitter|
174
+ emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
175
+ }
176
+ }
177
+ class << self
178
+ def to_yaml opts = {}
179
+ begin
180
+ @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
181
+ rescue
182
+ @to_yaml_style = :inline
183
+ super
184
+ end
185
+ end
186
+ end
187
+ end
188
+ end
189
+ @__yaml_inline = bool
190
+ end
191
+ def yaml_inline!() self.yaml_inline = true end
192
+
193
+ def each_with_index
194
+ @order.each_with_index { |k, index| yield k, self[k], index }
195
+ self
196
+ end
197
+ end # class Storable::OrderedHash
198
+
199
+
data/storable.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  @spec = Gem::Specification.new do |s|
2
2
  s.name = "storable"
3
3
  s.rubyforge_project = "storable"
4
- s.version = "0.5.1"
4
+ s.version = "0.5.2"
5
5
  s.summary = "Storable: Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)"
6
6
  s.description = s.summary
7
7
  s.author = "Delano Mandelbaum"
@@ -16,8 +16,7 @@
16
16
 
17
17
  # = DEPENDENCIES =
18
18
  # Add all gem dependencies
19
- s.add_dependency 'sysinfo', '>= 0.5.0'
20
-
19
+
21
20
  # = MANIFEST =
22
21
  # The complete list of files to be included in the release. When GitHub packages your gem,
23
22
  # it doesn't allow you to run any command that accesses the filesystem. You will get an
@@ -30,6 +29,7 @@
30
29
  README.rdoc
31
30
  Rakefile
32
31
  lib/storable.rb
32
+ lib/storable/orderedhash.rb
33
33
  storable.gemspec
34
34
  )
35
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delano Mandelbaum
@@ -9,19 +9,9 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-07 00:00:00 -04:00
12
+ date: 2009-05-12 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: sysinfo
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 0.5.0
24
- version:
25
15
  - !ruby/object:Gem::Dependency
26
16
  name: RedCloth
27
17
  type: :runtime
@@ -47,6 +37,7 @@ files:
47
37
  - README.rdoc
48
38
  - Rakefile
49
39
  - lib/storable.rb
40
+ - lib/storable/orderedhash.rb
50
41
  - storable.gemspec
51
42
  has_rdoc: true
52
43
  homepage: http://solutious.com/