Permalink
Browse files

have default context name for single and collections. fixed some plac…

…es to treat TrueClass, Fixnum and String right
  • Loading branch information...
1 parent 90c24f8 commit 981410c28ea03f4cc18a8c45a95c6600f3234366 @mkristian committed Nov 26, 2012
Showing with 30 additions and 13 deletions.
  1. +20 −8 lib/ixtlan/babel/hash_filter.rb
  2. +10 −5 lib/ixtlan/babel/serializer.rb
@@ -18,9 +18,8 @@ def add_custom_serializers(map)
@map = map
end
- def default_context_key(default = nil)
- @default = default if default
- @default
+ def default_context_key(single = :single, collection = :collection)
+ @single, @collection = single, collection
end
def []=(key, options)
@@ -56,11 +55,15 @@ def NO_MODEL.[](*args)
end
def filter(hash = {}, model = NO_MODEL, &block)
- filter_data(model, hash, options, &block) if hash
+ filter_data(model, hash, hash.is_a?(Array) ? collection_options : single_options, &block) if hash
end
- def options
- @options || context[default_context_key] || {}
+ def single_options
+ @options || context[default_context_key[0]] || {}
+ end
+
+ def collection_options
+ @options || context[default_context_key[1]] || {}
end
private
@@ -95,13 +98,22 @@ def filter_data(model, data, options = {}, &block)
elsif models_or_model.respond_to?(:collect)
data[m] = models_or_model.collect { |i| block.call(i) }
else
- data[m]= block.call(model.send(m))
+ val = model.send(m)
+ case val
+ when Fixnum
+ data[m] = val
+ when String
+ data[m] = val
+ when TrueClass
+ data[m] = val
+ else
+ data[m]= block.call(val)
+ end
end
end
end
end
methods ||= []
-
result = {}
data.each do |k,v|
case v
@@ -50,8 +50,8 @@ def self.model_name
model.model_name
end
- def self.default_context_key(default)
- filter.default_context_key(default)
+ def self.default_context_key(single = :single, collection = :collection)
+ filter.default_context_key(single, collection)
end
def self.add_context(key, options = {})
@@ -66,12 +66,13 @@ def use(context_or_options)
end
def to_hash(options = nil)
- filter.use(filter.options.dup.merge!(options)) if options
if @model_or_models.respond_to?(:collect) && ! @model_or_models.is_a?(Hash)
+ filter.use(filter.collection_options.dup.merge!(options)) if options
@model_or_models.collect do |m|
filter_model(attr(m), m)
end
else
+ filter.use(filter.single_options.dup.merge!(options)) if options
filter_model(attr(@model_or_models), @model_or_models)
end
end
@@ -81,7 +82,11 @@ def to_json(options = nil)
end
def to_xml(options = {})
- opts = filter.options.dup.merge!(options)
+ if @model_or_models.respond_to?(:collect) && ! @model_or_models.is_a?(Hash)
+ opts = filter.collection_options.dup.merge!(options)
+ else
+ opts = filter.single_options.dup.merge!(options)
+ end
root = opts.delete :root
fitler.use(opts)
result = to_hash
@@ -104,7 +109,7 @@ def attr(model)
private
def filter_model(model, data)
- if root = filter.options[:root]
+ if root = filter.single_options[:root]
{root.to_s => filter.filter(model, data){ |model| attr(model) } }
else
filter.filter(model, data){ |model| attr(model) }

0 comments on commit 981410c

Please sign in to comment.