Sha256: bc47ecdefeaf4b552ad164f93a875a39184fd78189d175668054f8a63451913c
Contents?: true
Size: 1.65 KB
Versions: 1
Compression:
Stored size: 1.65 KB
Contents
module Traitorous module Converter # MethodKeyedUniformedHash is meant to take an array of a simple data # structures and convert each into a uniform class, and then will call a # key_method on that class and use it as the key in the returned hash. # # Exported data will be converted into an array calling do_export on each # element class MethodKeyedUniformHash attr_accessor :key_method, :uniform_class # @param key_method [Symbol] the method to call on the uniform_class instance # to generate the key in the returned hash # @param uniform_class [Class, #new] the class to instantiate with each # element of the do_import array def initialize(key_method, uniform_class) @key_method = key_method @uniform_class = uniform_class end # The import instantiates each element of the array as an instance of # the uniform_class, the key is determined by calling key_method on the # instance and then they are joined to the result hash as a key, # instance pair. # @param arr_data [Array] the array of data to instantiate # @return [Hash] hash containing key, instance pairs def do_import(arr_data) out = {} arr_data.each do |elem_data| obj = uniform_class.new(elem_data) out[obj.send(key_method)] = obj end out end # @param hsh_data [Hash<obj,#export>] keys are ignored # @return Array each element of the values of hsh_data has #export called # on it. def do_export(hsh_data) hsh_data.values.map{|instance| instance.export } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
traitorous-0.2.0 | lib/traitorous/converter/method_keyed_uniform_hash.rb |