lib/radriar/roar/key_translation.rb in radriar-0.1.0.alpha.1 vs lib/radriar/roar/key_translation.rb in radriar-0.1.0.alpha.2

- old
+ new

@@ -2,10 +2,51 @@ module Radriar module Roar module KeyTranslation extend ActiveSupport::Concern + included do + unless ::Representable::Hash.instance_methods.include?(:__radriar_old_to_hash) + ::Representable::Hash.module_eval do + # TODO: Module#prepend giving too much headache + alias_method :__radriar_old_to_hash, :to_hash + alias_method :__radriar_old_from_hash, :from_hash + + define_method(:from_hash) do |data, options={}, binding_builder=::Representable::Hash::PropertyBinding| + if Radriar::Representable.translate_keys? + data = filter_wrap(UnderscoreKeys.new(data), options) + update_properties_from(data, options, binding_builder) + else + __radriar_old_from_hash(data, options, binding_builder) + end + end + + define_method(:to_hash) do |options={}, binding_builder=::Representable::Hash::PropertyBinding| + if Radriar::Representable.translate_keys? + CamelizeKeys.new(__radriar_old_to_hash(options, binding_builder)) + else + __radriar_old_to_hash(options, binding_builder) + end + end + end + end + + unless ::Grape::Endpoint.instance_methods.include?(:__radriar_old_params) + ::Grape::Endpoint.class_eval do + alias_method :__radriar_old_params, :params + + define_method(:params) do + if Radriar::Representable.translate_keys? + @params ||= UnderscoreKeys.new(@request.params) + else + __radriar_old_params + end + end + end + end + end + class KeyTranslatorHash < Hashie::Mash protected def convert_key(key) end @@ -31,27 +72,11 @@ end class CamelizeKeys < KeyTranslatorHash protected def convert_key(key) - key.to_s.camelize(:lower) + key.to_s.start_with?("_") ? key.to_s : key.to_s.camelize(:lower) end - end - - - module Representer - def from_hash(data, options={}, binding_builder=::Representable::Hash::PropertyBinding) - data = filter_wrap(UnderscoreKeys.new(data), options) - update_properties_from(data, options, binding_builder) - end - - def to_hash(options={}, binding_builder=::Representable::Hash::PropertyBinding) - CamelizeKeys.new(super) - end - end - - included do - ::Representable::Hash.prepend(Representer) end end end end