Sha256: 374a581ed569b01eb5eb258631e29511a56d7c479cd1107f7ab94637521b0353

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'representable'
module Radriar
  module Roar
    module KeyTranslation
      extend ActiveSupport::Concern

      class KeyTranslatorHash < Hashie::Mash
        protected
        def convert_key(key)
        end

        def convert_value(val, duping=false) #:nodoc
          obj = super
          obj = self.class.new(obj) if Hashie::Mash === obj
          obj if Hashie::Mash === obj
          obj
        end

        def initialize_reader(key)
          ck = to_underscore(key)
          custom_writer(ck, self.class.new) unless key?(ck)
          custom_reader(ck.camelize)
        end
      end

      class UnderscoreKeys < KeyTranslatorHash
        protected
        def convert_key(key)
          key.to_s.underscore
        end
      end

      class CamelizeKeys < KeyTranslatorHash
        protected
        def convert_key(key)
          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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radriar-0.1.0.alpha.1 lib/radriar/roar/key_translation.rb