Sha256: 9d1bd9da493f8e3a9109df9685ba49d75e5fa10052f93e80d9daffebd04c7fd4

Contents?: true

Size: 966 Bytes

Versions: 4

Compression:

Stored size: 966 Bytes

Contents

# encoding: utf-8

require 'ostruct'

module Flextures
  # OpenStruct hack in flextures Plug-in
  class OpenStruct < ::OpenStruct
    # Struct Data translate to Hash
    def to_hash
      (self.methods - ::OpenStruct.new.methods)
        .select{ |name| name.to_s.match(/\w+=/) }
        .map{ |name| name.to_s.gsub(/=/,'').to_sym }
        .reduce({}){ |k,h| h[k]=self.send(k); h }
    end
  end

  module Extensions
    module Array
      # use Object#extend
      # @params [Array] keys hash keys
      # @return [Hash] tanslated Hash data
      # example:
      # hash = array.extend(Extensions::Array).to_hash(keys)
      def to_hash( keys )
        values = self
        values = values[0..keys.size-1]               if keys.size < values.size
        values = values+[nil]*(keys.size-values.size) if keys.size > values.size
        [keys,values].transpose.reduce({}){ |h,pair| k,v=pair; h[k]=v; h }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
flextures-3.1.3 lib/flextures/flextures_extension_modules.rb
flextures-3.1.2 lib/flextures/flextures_extension_modules.rb
flextures-3.1.1 lib/flextures/flextures_extension_modules.rb
flextures-3.1.0 lib/flextures/flextures_extension_modules.rb