Permalink
Browse files

removed deserializer. filter delivers a result class now.

  • Loading branch information...
1 parent cb060d3 commit bfd5fcb8a2bc2dc69ed96cdac88284e53235599b @mkristian committed Jul 8, 2013
View
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
Gem::Specification.new do |s|
s.name = 'ixtlan-babel'
- s.version = '0.3.5'
+ s.version = '0.4.0'
s.summary = 'babel offers a filter for hashes and with that comes json/yaml/xml de/serialization of models which provides a hash representation'
s.description = 'babel offers a filter for hashes and with that comes json/yaml/xml de/serialization of models which provides a hash representationi. possible models are activerecord, activemodel, resources from datamapper, virtus'
@@ -19,10 +19,10 @@ Gem::Specification.new do |s|
s.test_files += Dir['spec/**/*_spec.rb']
- s.add_development_dependency 'rake', '~> 10.0.0'
+ s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'copyright-header', '~> 1.0.7'
- s.add_development_dependency 'minitest', '~> 4.3'
- s.add_development_dependency 'virtus', '~> 0.5.0'
+ s.add_development_dependency 'minitest', '~> 5.0'
+ s.add_development_dependency 'virtus', '~> 0.5'
s.add_development_dependency 'multi_json', '~> 1.6'
s.add_development_dependency 'json', '~> 1.7'
end
@@ -28,7 +28,7 @@ class AbstractFilter
def options
@options || {}
end
-
+
def add_custom_serializers( map )
@map = map
end
@@ -40,7 +40,7 @@ def serialize( data )
data
end
end
-
+
end
end
-end
+end
@@ -1,88 +0,0 @@
-#
-# Copyright (C) 2013 Christian Meier
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy of
-# this software and associated documentation files (the "Software"), to deal in
-# the Software without restriction, including without limitation the rights to
-# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
-# the Software, and to permit persons to whom the Software is furnished to do so,
-# subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in all
-# copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-#
-require 'multi_json'
-module Ixtlan
- module Babel
- class Deserializer
-
- def initialize(model_class)
- @model_class = model_class
- end
-
- private
-
- def self.config
- @config ||= FilterConfig.new
- end
-
- def filter
- @filter ||= HashFilter.new
- end
-
- protected
-
- def self.default_context_key(default)
- config.default_context_key(default)
- end
-
- def self.add_context(key, options = {})
- config[key] = options
- end
-
- public
-
- def use(context_or_options)
- @context_or_options = context_or_options
- self
- end
-
- def from_array_hash( data )
- if filter.options[:root]
- data.collect{ |d| @model_class.new( filter.filter( d[ filter.options[:root] ] ) ) }
- else
- data.collect{ |d| @model_class.new( filter.filter( d ) ) }
- end
- end
- private :from_array_hash
-
- def from_hash(data, options = nil)
- filter.options = options || {}
- filter.options[:root] ||= self.class.config.root
- if data.is_a? Array
- from_array_hash( data )
- else
- data = data[ filter.options[:root] ] if filter.options[:root]
- @model_class.new( filter.filter( data ) )
- end
- end
-
- def from_json(json, options = nil)
- data = MultiJson.load(json)
- from_hash(data, options)
- end
-
- def from_yaml(yaml, options = nil)
- data = YAML.load_stream(StringIO.new(yaml)).documents
- from_hash(data, options)
- end
- end
- end
-end
@@ -4,5 +4,5 @@ class DataMapper::Validations::ValidationErrorsSerializer < Ixtlan::Babel::Seria
def to_hash( o = nil)
@model_or_models.to_hash
end
-
+
end
@@ -32,7 +32,7 @@ class Factory
DATE_TIME_TO_S = Proc.new do |dt|
dt.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % (dt.sec_fraction * NANOSECONDS_IN_DAY ) )[0..6] + dt.strftime('%z')
end
-
+
DEFAULT_MAP = {
'DateTime' => DATE_TIME_TO_S,
'ActiveSupport::TimeWithZone' => TIME_TO_S,
@@ -57,7 +57,8 @@ def add( clazz, &block )
def new_serializer( resource )
if resource.respond_to?(:model)
model = resource.model
- elsif resource.respond_to?( :collect) && !resource.respond_to?( :to_hash)
+ elsif resource.respond_to?( :collect) &&
+ !resource.respond_to?( :to_hash)
if resource.empty?
return EmptyArraySerializer.new
else
@@ -22,7 +22,7 @@
module Ixtlan
module Babel
class FilterConfig
-
+
private
def context
@@ -32,7 +32,7 @@ def context
def context_options( context_or_options )
if context_or_options
case context_or_options
- when Symbol
+ when Symbol
if opts = context[ context_or_options ]
opts.dup
end
@@ -44,7 +44,7 @@ def context_options( context_or_options )
public
- def default_context_key( single = :single,
+ def default_context_key( single = :single,
collection = :collection )
@single, @collection = single, collection
end
@@ -58,15 +58,17 @@ def []( key )
end
def single_options( context_or_options )
- context_options( context_or_options ) || context[ default_context_key[ 0 ] ] || {}
+ context_options( context_or_options ) ||
+ context[ default_context_key[ 0 ] ] || {}
end
def collection_options( context_or_options )
- context_options( context_or_options ) || context[ default_context_key[ 1 ] ] || {}
+ context_options( context_or_options ) ||
+ context[ default_context_key[ 1 ] ] || {}
end
attr_accessor :root
end
end
-end
+end
@@ -25,15 +25,15 @@ class HashFilter < AbstractFilter
def filter( data )
if data
- filter_data( data,
+ filter_data( data,
Context.new( options ) )
end
end
private
def filter_array( array, options )
- array.collect do |item|
+ array.collect do |item|
if item.is_a?( Array ) || item.is_a?( Hash )
filter_data( item, options )
else
@@ -48,9 +48,11 @@ def filter_data( data, context )
k = k.to_s
case v
when Hash
- result[ k ] = filter_data( v, context[ k ] ) if context.include?( k )
+ result[ k ] = filter_data( v,
+ context[ k ] ) if context.include?( k )
when Array
- result[ k ] = filter_array( v, context[ k ] ) if context.include?( k )
+ result[ k ] = filter_array( v,
+ context[ k ] ) if context.include?( k )
else
result[ k ] = serialize( v ) if context.allowed?( k )
end
Oops, something went wrong.

0 comments on commit bfd5fcb

Please sign in to comment.