storable 0.7.2 → 0.7.3

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.
@@ -3,6 +3,14 @@ STORABLE, CHANGES
3
3
  * TODO: Handle nested hashes and arrays.
4
4
  * TODO: to_xml, see: http://codeforpeople.com/lib/ruby/xx/xx-2.0.0/README
5
5
 
6
+ #### 0.7.3 (2010-04-10) #############################
7
+
8
+ * ADDED: Allow anonymous objects via Storable.from_json etc.
9
+ * ADDED: Enabled support for marshalling Proc objects in IRB sessions.
10
+ * ADDED: Storable#call for calling Proc fields via instance_eval
11
+ * ADDED: Support for Range field type (parse from a String when necessary)
12
+
13
+
6
14
  #### 0.7.2 (2010-04-06) #############################
7
15
 
8
16
  * FIXED: Handle empty symbols like in Storable 0.6.4. Fixes "interning empty string" error. [Diego Elio 'Flameeyes' Pettenò]
@@ -26,6 +26,33 @@ Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)
26
26
  puts mac2.position.class # => Fixnum
27
27
 
28
28
 
29
+ == Storing Procs
30
+
31
+ Storable can also marshal Proc objects to and from their actual source code.
32
+
33
+ require 'storable'
34
+
35
+ class Maths < Storable
36
+ field :x => Float
37
+ field :y => Float
38
+ field :calculate => Proc
39
+ end
40
+
41
+ mat1 = Maths.new 2.0, 3.0
42
+ mat1.calculate = Proc.new { @x * @y }
43
+
44
+ mat1.calculate.source # => "{ @x * @y }"
45
+ mat1.call :calculate # => 6.0
46
+
47
+ dump = mat1.to_json
48
+
49
+ mat2 = Maths.from_json dump
50
+ mat2.call :calculate # => 6.0
51
+
52
+
53
+ Anything is possible when you keep your mind open and you use Ruby.
54
+
55
+
29
56
  == Installation
30
57
 
31
58
  Via Rubygems, one of:
@@ -40,7 +67,7 @@ or via download:
40
67
 
41
68
  == Prerequisites
42
69
 
43
- * Ruby 1.8, Ruby 1.9, or JRuby 1.2
70
+ * Ruby 1.8, Ruby 1.9, or JRuby 1.2+
44
71
 
45
72
 
46
73
  == Thanks
@@ -48,6 +48,7 @@ module ProcSource
48
48
  retried = 0
49
49
  loop do
50
50
  lines = get_lines(filename, start_line)
51
+ return nil if lines.nil?
51
52
  #p [start_line, lines[0]]
52
53
  if !line_has_open?(lines.join) && start_line >= 0
53
54
  start_line -= 1 and retried +=1 and redo
@@ -135,15 +136,11 @@ module ProcSource
135
136
  case filename
136
137
  when nil
137
138
  nil
138
- ## NOTE: IRB AND EVAL LINES NOT TESTED
139
- ### special "(irb)" descriptor?
140
- ##when "(irb)"
141
- ## IRB.conf[:MAIN_CONTEXT].io.line(start_line .. -2)
142
- ### special "(eval...)" descriptor?
143
- ##when /^\(eval.+\)$/
144
- ## EVAL_LINES__[filename][start_line .. -2]
145
- # regular file
146
- else
139
+ when "(irb)" # special "(irb)" descriptor?
140
+ IRB.conf[:MAIN_CONTEXT].io.line(start_line .. -2)
141
+ when /^\(eval.+\)$/ # special "(eval...)" descriptor?
142
+ EVAL_LINES__[filename][start_line .. -2]
143
+ else # regular file
147
144
  # Ruby already parsed this file? (see disclaimer above)
148
145
  if defined?(SCRIPT_LINES__) && SCRIPT_LINES__[filename]
149
146
  SCRIPT_LINES__[filename][(start_line - 1) .. -1]
@@ -39,7 +39,7 @@ class Storable
39
39
  require 'proc_source'
40
40
  require 'storable/orderedhash' if USE_ORDERED_HASH
41
41
  unless defined?(SUPPORTED_FORMATS) # We can assume all are defined
42
- VERSION = "0.7.2"
42
+ VERSION = "0.7.3"
43
43
  NICE_TIME_FORMAT = "%Y-%m-%d@%H:%M:%S".freeze
44
44
  SUPPORTED_FORMATS = [:tsv, :csv, :yaml, :json, :s, :string].freeze
45
45
  end
@@ -100,7 +100,9 @@ class Storable
100
100
  next
101
101
  end
102
102
 
103
- define_method(m) do instance_variable_get("@#{m}") end
103
+ define_method(m) do
104
+ instance_variable_get("@#{m}")
105
+ end
104
106
  define_method("#{m}=") do |val|
105
107
  instance_variable_set("@#{m}",val)
106
108
  end
@@ -162,16 +164,28 @@ class Storable
162
164
  format ||= @format
163
165
  Storable.write_file(file_path, dump(format, with_titles))
164
166
  end
165
-
167
+
166
168
  # Create a new instance of the object from a hash.
167
169
  def self.from_hash(from={})
168
170
  return nil if !from || from.empty?
169
- me = self.new
170
- me.from_hash(from)
171
+ if self == Storable
172
+ Storable::Anonymous.new from
173
+ else
174
+ new.from_hash(from)
175
+ end
176
+ end
177
+
178
+ def call(fname)
179
+ unless field_types[fname.to_sym] == Proc &&
180
+ Proc === self.send(fname)
181
+ raise "Field #{fname} is not a Proc"
182
+ end
183
+ self.instance_eval &self.send(fname)
171
184
  end
172
185
 
173
186
  def from_hash(from={})
174
187
  fnames = field_names
188
+ return from if fnames.nil? || fnames.empty?
175
189
  fnames.each_with_index do |fname,index|
176
190
  ftype = field_types[fname]
177
191
  value_orig = from[fname] || from[fname.to_s]
@@ -197,6 +211,23 @@ class Storable
197
211
  value = value_orig.to_i
198
212
  elsif ftype == Symbol
199
213
  value = value_orig.to_s.to_sym
214
+ elsif ftype == Range
215
+ if Range === value_orig
216
+ value = value_orig
217
+ elsif Numeric === value_orig
218
+ value = value_orig..value_orig
219
+ else
220
+ value_orig = value_orig.to_s
221
+ if value_orig.match(/\.\.\./)
222
+ el = value_orig.split('...')
223
+ value = el.first.to_f...el.last.to_f
224
+ elsif value_orig.match(/\.\./)
225
+ el = value_orig.split('..')
226
+ value = el.first.to_f..el.last.to_f
227
+ else
228
+ value = value_orig..value_orig
229
+ end
230
+ end
200
231
  elsif ftype == Proc && String === value_orig
201
232
  value = Proc.from_string value_orig
202
233
  end
@@ -374,6 +405,19 @@ class Storable
374
405
  end
375
406
  File.chmod(0600, path)
376
407
  end
408
+
409
+ class Anonymous
410
+ def initialize from
411
+ @hash = from
412
+ end
413
+ def [](key)
414
+ @hash[meth.to_sym]
415
+ end
416
+ def method_missing(meth,*args)
417
+ @hash[meth.to_sym]
418
+ end
419
+ end
420
+
377
421
  end
378
422
 
379
423
 
@@ -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.7.2"
4
+ s.version = "0.7.3"
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"
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.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delano Mandelbaum
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-04-06 00:00:00 -04:00
12
+ date: 2010-04-11 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15