Permalink
Browse files

started factoring out config

  • Loading branch information...
mkristian committed Dec 8, 2012
1 parent 964a9b2 commit 802edb0cbb76ea6ba5df1d1f04c8e55049e93398
@@ -1,6 +1,6 @@
module Ixtlan
module Babel
- class Config
+ class Context
def initialize( options )
@only = options[ :only ].collect { |o| o.to_s } if options[ :only ]
@@ -0,0 +1,80 @@
+require 'ixtlan/babel/context'
+module Ixtlan
+ module Babel
+ class FilterConfig
+
+ def initialize(context_or_options = nil)
+ use(context_or_options)
+ end
+
+ private
+
+ def context
+ @context ||= {}
+ end
+
+ public
+
+ def add_custom_serializers( map )
+ @map = map
+ end
+
+ def default_context_key( single = :single,
+ collection = :collection )
+ @single, @collection = single, collection
+ end
+
+ def []=( key, options )
+ context[ key.to_sym ] = options if key
+ end
+
+ def []( key )
+ context[ key.to_sym ] if key
+ end
+
+ def use( context_or_options )
+ if context_or_options
+ case context_or_options
+ when Symbol
+ if opts = context[ context_or_options ]
+ @options = opts.dup
+ end
+ when Hash
+ @options = context_or_options
+ end
+ else
+ @options = nil
+ end
+ self
+ end
+
+ def single_options
+ @options || context[default_context_key[0]] || {}
+ end
+
+ def collection_options
+ @options || context[default_context_key[1]] || {}
+ end
+
+ attr_accessor :root
+ def root
+ @root ||= single_options.key?(:root) ? single_options[:root].to_s : nil
+ end
+
+ private
+
+ def options_for( data )
+ @options || (data.is_a?(Array) ? collection_options : single_options)
+ end
+
+ def serialize( data )
+ if @map && ser = @map[ data.class.to_s ]
+ ser.call(data)
+ else
+ data
+ end
+ end
+
+ end
+ end
+end
@@ -1,4 +1,4 @@
-require 'ixtlan/babel/config'
+require 'ixtlan/babel/context'
module Ixtlan
module Babel
class HashFilter
@@ -51,7 +51,7 @@ def use( context_or_options )
def filter( hash = {} )
if hash
filter_data( hash,
- Config.new( options_for( hash ) ) )
+ Context.new( options_for( hash ) ) )
end
end
@@ -91,17 +91,17 @@ def serialize( data )
end
end
- def filter_data( data, config )
+ def filter_data( data, context )
result = {}
data.each do |k,v|
k = k.to_s
case v
when Hash
- result[ k ] = filter_data( v, config[ k ] ) if config.include?( k )
+ result[ k ] = filter_data( v, context[ k ] ) if context.include?( k )
when Array
- result[ k ] = filter_array( v, config[ k ] ) if config.include?( k )
+ result[ k ] = filter_array( v, context[ k ] ) if context.include?( k )
else
- result[ k ] = serialize( v ) if config.allowed?( k )
+ result[ k ] = serialize( v ) if context.allowed?( k )
end
end
result
@@ -6,7 +6,7 @@ def filter( model, &block )
if model
data = block.call( model )
filter_data( model, data,
- Config.new( options_for( data ) ),
+ Context.new( options_for( data ) ),
&block )
end
end
@@ -23,8 +23,8 @@ def filter_array( models, options, &block )
end
end
- def filter_data(model, data, config, &block)
- config.methods.each do |m|
+ def filter_data(model, data, context, &block)
+ context.methods.each do |m|
unless data.include?(m)
data[ m ] = model.send( m.to_sym )
end
@@ -34,11 +34,11 @@ def filter_data(model, data, config, &block)
data.each do |k,v|
k = k.to_s
if v.respond_to? :attributes
- result[ k ] = filter_data( v, block.call(v), config[ k ], &block ) if config.include?( k )
+ result[ k ] = filter_data( v, block.call(v), context[ k ], &block ) if context.include?( k )
elsif v.is_a? Array
- result[ k ] = filter_array( v, config[ k ], &block ) if config.include?( k )
+ result[ k ] = filter_array( v, context[ k ], &block ) if context.include?( k )
else
- result[ k ] = serialize( v ) if config.allowed?( k ) && ! v.respond_to?( :attributes )
+ result[ k ] = serialize( v ) if context.allowed?( k ) && ! v.respond_to?( :attributes )
end
end
result

0 comments on commit 802edb0

Please sign in to comment.