storable 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  STORABLE, CHANGES
2
2
 
3
+ * TODO: https://github.com/delano/storable/pull/2
3
4
  * TODO: Handle nested hashes and arrays.
4
5
  * TODO: to_xml, see: http://codeforpeople.com/lib/ruby/xx/xx-2.0.0/README
5
6
 
@@ -96,7 +96,8 @@ or via download:
96
96
 
97
97
  == Thanks
98
98
 
99
- * Pierre Riteau (priteau[http://github.com/priteau]) for bug fixes.
99
+ * Pierre Riteau (priteau[https://github.com/priteau]) for bug fixes.
100
+ * notro[https://github.com/priteau] for proc_source improvements.
100
101
 
101
102
 
102
103
  == More Info
data/Rakefile CHANGED
@@ -33,7 +33,9 @@ version = @spec.version
33
33
 
34
34
  # TESTS/SPECS =========================================================
35
35
 
36
-
36
+ task :test do
37
+ sh "try"
38
+ end
37
39
 
38
40
  # INSTALL =============================================================
39
41
 
@@ -41,7 +43,7 @@ Rake::GemPackageTask.new(@spec) do |p|
41
43
  p.need_tar = true if RUBY_PLATFORM !~ /mswin/
42
44
  end
43
45
 
44
- task :build => [ :package ]
46
+ task :build => [ :test, :package ]
45
47
  task :release => [ :rdoc, :package ]
46
48
  task :install => [ :rdoc, :package ] do
47
49
  sh %{sudo gem install pkg/#{name}-#{version}.gem}
@@ -1,6 +1,8 @@
1
1
  #--
2
2
  # Based on:
3
3
  # http://github.com/imedo/background
4
+ # With improvements by:
5
+ # https://github.com/notro/storable
4
6
  #++
5
7
 
6
8
  require 'stringio'
@@ -8,14 +10,31 @@ require 'irb/ruby-lex'
8
10
  #SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
9
11
 
10
12
  class ProcString < String
11
- attr_accessor :file, :lines, :arity, :kind
13
+ # Filename where the proc is defined
14
+ attr_accessor :file
15
+
16
+ # Range of lines where the proc is defined
17
+ # ex. (12..16)
18
+ attr_accessor :lines
19
+
20
+ attr_accessor :arity, :kind # :nodoc: FIXME: Should be removed?
21
+
22
+ # Return a Proc object
23
+ # If #lines and #file is specified, these are tied to the proc.
12
24
  def to_proc(kind="proc")
13
- result = eval("#{kind} #{self}")
25
+ if @file && @lines
26
+ raise "#lines must be a range" unless @lines.kind_of? Range
27
+ result = eval("#{kind} #{self}", binding, @file, @lines.min)
28
+ else
29
+ result = eval("#{kind} #{self}")
30
+ end
14
31
  result.source = self
15
32
  result
16
33
  end
34
+
35
+ # Return a lambda
17
36
  def to_lambda
18
- to_proc "lamda"
37
+ to_proc "lambda"
19
38
  end
20
39
  end
21
40
 
@@ -24,8 +43,12 @@ class RubyToken::Token
24
43
  # These EXPR_BEG tokens don't have associated end tags
25
44
  FAKIES = [RubyToken::TkWHEN, RubyToken::TkELSIF, RubyToken::TkTHEN]
26
45
 
46
+ def name
47
+ @name ||= nil
48
+ end
49
+
27
50
  def open_tag?
28
- return false if @name.nil? || get_props.nil?
51
+ return false if name.nil? || get_props.nil?
29
52
  a = (get_props[1] == RubyToken::EXPR_BEG) &&
30
53
  self.class.to_s !~ /_MOD/ && # ignore onliner if, unless, etc...
31
54
  !FAKIES.member?(self.class)
@@ -33,7 +56,7 @@ class RubyToken::Token
33
56
  end
34
57
 
35
58
  def get_props
36
- RubyToken::TkReading2Token[@name]
59
+ RubyToken::TkReading2Token[name]
37
60
  end
38
61
 
39
62
  end
@@ -43,7 +66,7 @@ end
43
66
  #
44
67
  module ProcSource
45
68
 
46
- def self.find(filename, start_line=0, block_only=true)
69
+ def self.find(filename, start_line=1, block_only=true)
47
70
  lines, lexer = nil, nil
48
71
  retried = 0
49
72
  loop do
@@ -59,7 +82,7 @@ module ProcSource
59
82
  end
60
83
  stoken, etoken, nesting = nil, nil, 0
61
84
  while token = lexer.token
62
- n = token.instance_variable_get(:@name)
85
+ n = token.name
63
86
 
64
87
  if RubyToken::TkIDENTIFIER === token
65
88
  #nothing
@@ -72,6 +95,8 @@ module ProcSource
72
95
  break
73
96
  end
74
97
  nesting -= 1
98
+ elsif RubyToken::TkLBRACE === token
99
+ nesting += 1
75
100
  elsif RubyToken::TkBITOR === token && stoken
76
101
  #nothing
77
102
  elsif RubyToken::TkNL === token && stoken && etoken
@@ -117,12 +142,14 @@ module ProcSource
117
142
  break
118
143
  when RubyToken::TkDO
119
144
  success = true
145
+ when RubyToken::TkfLBRACE
146
+ success = true
120
147
  when RubyToken::TkCONSTANT
121
- if token.instance_variable_get(:@name) == "Proc" &&
148
+ if token.name == "Proc" &&
122
149
  lexer.token.is_a?(RubyToken::TkDOT)
123
150
  method = lexer.token
124
151
  if method.is_a?(RubyToken::TkIDENTIFIER) &&
125
- method.instance_variable_get(:@name) == "new"
152
+ method.name == "new"
126
153
  success = true
127
154
  end
128
155
  end
@@ -132,7 +159,7 @@ module ProcSource
132
159
  end
133
160
 
134
161
 
135
- def self.get_lines(filename, start_line = 0)
162
+ def self.get_lines(filename, start_line = 1)
136
163
  case filename
137
164
  when nil
138
165
  nil
@@ -157,22 +184,68 @@ module ProcSource
157
184
  end
158
185
 
159
186
  class Proc #:nodoc:
160
- attr_reader :file, :line
161
187
  attr_writer :source
162
188
 
163
189
  def source_descriptor
190
+ @file ||= nil
191
+ @line ||= nil
164
192
  unless @file && @line
165
193
  if md = /^#<Proc:0x[0-9A-Fa-f]+@(.+):(\d+)(.+?)?>$/.match(inspect)
166
194
  @file, @line = md.captures
167
195
  end
168
196
  end
169
- [@file, @line.to_i]
197
+ @line = @line.to_i
198
+ [@file, @line]
170
199
  end
171
200
 
172
201
  def source
173
202
  @source ||= ProcSource.find(*self.source_descriptor)
174
203
  end
175
204
 
205
+ def line
206
+ source_descriptor
207
+ @line
208
+ end
209
+
210
+ def file
211
+ source_descriptor
212
+ @file
213
+ end
214
+
215
+ # Dump to Marshal format.
216
+ # p = Proc.new { false }
217
+ # Marshal.dump p
218
+ def _dump(limit)
219
+ raise "can't dump proc, #source is nil" if source.nil?
220
+ str = Marshal.dump(source)
221
+ str
222
+ end
223
+
224
+ # Load from Marshal format.
225
+ # p = Proc.new { false }
226
+ # Marshal.load Marshal.dump p
227
+ def self._load(str)
228
+ @source = Marshal.load(str)
229
+ @source.to_proc
230
+ end
231
+
232
+ # Dump to JSON string
233
+ def to_json(*args)
234
+ raise "can't serialize proc, #source is nil" if source.nil?
235
+ {
236
+ 'json_class' => self.class.name,
237
+ 'data' => [source.to_s, source.file, source.lines.min, source.lines.max]
238
+ }.to_json#(*args)
239
+ end
240
+
241
+ def self.json_create(o)
242
+ s, file, min, max = o['data']
243
+ ps = ProcString.new s
244
+ ps.file = file
245
+ ps.lines = (min..max)
246
+ ps.to_proc
247
+ end
248
+
176
249
  # Create a Proc object from a string of Ruby code.
177
250
  # It's assumed the string contains do; end or { }.
178
251
  #
@@ -35,7 +35,7 @@ class Storable
35
35
  require 'proc_source'
36
36
  require 'storable/orderedhash' if USE_ORDERED_HASH
37
37
  unless defined?(SUPPORTED_FORMATS) # We can assume all are defined
38
- VERSION = "0.8.5"
38
+ VERSION = "0.8.7"
39
39
  NICE_TIME_FORMAT = "%Y-%m-%d@%H:%M:%S".freeze
40
40
  SUPPORTED_FORMATS = [:tsv, :csv, :yaml, :json, :s, :string].freeze
41
41
  end
@@ -200,7 +200,7 @@ class Storable
200
200
  def init *args
201
201
  from_array *args
202
202
  end
203
-
203
+
204
204
  def initialize *args
205
205
  init *args
206
206
  end
@@ -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.8.6"
4
+ s.version = "0.8.7"
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,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 8
9
- - 6
10
- version: 0.8.6
4
+ prerelease:
5
+ version: 0.8.7
11
6
  platform: ruby
12
7
  authors:
13
8
  - Delano Mandelbaum
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-12-31 00:00:00 -05:00
13
+ date: 2011-03-03 00:00:00 -05:00
19
14
  default_executable:
20
15
  dependencies: []
21
16
 
@@ -55,23 +50,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
50
  requirements:
56
51
  - - ">="
57
52
  - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
53
  version: "0"
62
54
  required_rubygems_version: !ruby/object:Gem::Requirement
63
55
  none: false
64
56
  requirements:
65
57
  - - ">="
66
58
  - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 0
70
59
  version: "0"
71
60
  requirements: []
72
61
 
73
62
  rubyforge_project: storable
74
- rubygems_version: 1.3.7
63
+ rubygems_version: 1.5.2
75
64
  signing_key:
76
65
  specification_version: 3
77
66
  summary: "Storable: Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)"