Skip to content

Commit

Permalink
Fixed bug in from_hash which was incorrectly parsing some data types …
Browse files Browse the repository at this point in the history
…(incl. Integer). Better JSON loading.
  • Loading branch information
delano committed May 7, 2009
1 parent 384ed16 commit 1bfb4bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
@@ -1,6 +1,12 @@
STORABLE, CHANGES


#### 0.5.1 (2009-05-07) #############################

* FIXED: Bug in from_hash which was incorrectly parsing some data types (incl. Integer)
* ADDED: bin/example
* ADDED: OrderedHash for Ruby 1.8.x (still unordered in JRuby. Need to investigate.)

#### 0.5 (2009-05-07) ###############################

* First public release. See commit history for solutious-stella, solutious-rudy,
Expand Down
46 changes: 24 additions & 22 deletions lib/storable.rb
@@ -1,20 +1,23 @@
#--
# TODO: Handle nested hashes and arrays.
# TODO: to_xml, see: http://codeforpeople.com/lib/ruby/xx/xx-2.0.0/README
# TODO: Rename to Stuffany
#++

require 'yaml'
require 'fileutils'

USE_ORDERED_HASH = (RUBY_VERSION =~ /1.9/).nil?
require 'orderedhash' if USE_ORDERED_HASH
require 'json' rescue nil

require 'yaml'
require 'fileutils'

# Storable makes data available in multiple formats and can
# re-create objects from files. Fields are defined using the
# Storable.field method which tells Storable the order and
# name.
class Storable
unless defined?(SUPPORTED_FORMATS) # We can assume all are defined
VERSION = 5
VERSION = 0.5
NICE_TIME_FORMAT = "%Y-%m-%d@%H:%M:%S".freeze
SUPPORTED_FORMATS = [:tsv, :csv, :yaml, :json, :s, :string].freeze
end
Expand Down Expand Up @@ -46,7 +49,7 @@ def postprocess
# The value is not touched when the type is not provided.
def self.field(args={})
# TODO: Examine casting from: http://codeforpeople.com/lib/ruby/fattr/fattr-1.0.3/
args = {args => nil} unless args.is_a? Hash
args = {args => nil} unless args.kind_of?(Hash)

args.each_pair do |m,t|

Expand Down Expand Up @@ -127,14 +130,15 @@ def self.from_hash(from={})

if field_types[index] == Array
((value ||= []) << stored_value).flatten
elsif field_types[index] == Hash
elsif field_types[index].kind_of?(Hash)

value = stored_value
else

# SimpleDB stores attribute shit as lists of values
value = stored_value.first if stored_value.is_a?(Array) && stored_value.size == 1

##value = stored_value.first if stored_value.is_a?(Array) && stored_value.size == 1
value = (stored_value.is_a?(Array) && stored_value.size == 1) ? stored_value.first : stored_value

if field_types[index] == Time
value = Time.parse(value)
elsif field_types[index] == DateTime
Expand All @@ -145,10 +149,11 @@ def self.from_hash(from={})
value = value.to_f
elsif field_types[index] == Integer
value = value.to_i
elsif field_types[index].kind_of?(Storable) && stored_value.is_a?(Hash)
value = field_types[index].from_hash(stored_value)
else
value = (stored_value.is_a?(Array) && stored_value.size == 1) ? stored_value.first : stored_value
elsif field_types[index].kind_of?(Storable) && stored_value.kind_of?(Hash)
# I don't know why this is here so I'm going to raise an exception
# and wait a while for an error in one of my other projects.
#value = field_types[index].from_hash(stored_value)
raise "Delano, delano, delano. Clean up Storable!"
end
end

Expand All @@ -162,20 +167,19 @@ def self.from_hash(from={})
# Return the object data as a hash
# +with_titles+ is ignored.
def to_hash(with_titles=true)
tmp = {}
tmp = USE_ORDERED_HASH ? OrderedHash.new : {}
field_names.each do |fname|
tmp[fname] = self.send(fname)
end
tmp
end

# Create a new instance of the object from YAML.
# +from+ a YAML string split into an array by line.
def self.from_yaml(from=[])
# from is an array of strings
from_str = from.join('')
# +from+ a YAML String or Array (split into by line).
def self.from_yaml(*from)
from_str = [from].flatten.compact.join('')
hash = YAML::load(from_str)
hash = from_hash(hash) if hash.is_a? Hash
hash = from_hash(hash) if hash.kind_of?(Hash)
hash
end
def to_yaml(with_titles=true)
Expand All @@ -185,19 +189,17 @@ def to_yaml(with_titles=true)
# Create a new instance of the object from a JSON string.
# +from+ a JSON string split into an array by line.
def self.from_json(from=[])
require 'json'
# from is an array of strings
from_str = from.join('')
tmp = JSON::load(from_str)
hash_sym = tmp.keys.inject({}) do |hash, key|
hash[key.to_sym] = tmp[key]
hash
end
hash_sym = from_hash(hash_sym) if hash_sym.is_a? Hash
hash_sym = from_hash(hash_sym) if hash_sym.kind_of?(Hash)
hash_sym
end
def to_json(with_titles=true)
require 'json'
to_hash.to_json
end

Expand Down Expand Up @@ -255,7 +257,7 @@ def self.from_delimited(from=[],delim=',')
next unless values[index]
hash[key.to_sym] = values[index]
end
hash = from_hash(hash) if hash.is_a? Hash
hash = from_hash(hash) if hash.kind_of?(Hash)
hash
end

Expand Down

0 comments on commit 1bfb4bb

Please sign in to comment.