module MappableObjectAttributes::MappableData::ClassMethods

Public Instance Methods

build_from_map(hash_object, mapname=:default) click to toggle source

ActiveRecord specific

# File lib/mappable_object_attributes/mappable_data.rb, line 67
def build_from_map(hash_object, mapname=:default)
  instance = self.new
  instance.assign_attributes_from_hash(hash_object, mapname)

  return instance
end
create_from_map(hash_object, mapname=:default) click to toggle source
# File lib/mappable_object_attributes/mappable_data.rb, line 74
def create_from_map(hash_object, mapname=:default)
  instance = build_from_map(hash_object, mapname)
  instance.save

  return instance
end
default_data_map() click to toggle source
# File lib/mappable_object_attributes/mappable_data.rb, line 31
def default_data_map
  data_maps[:default]
end
define_attributes_map(mapname=:default) { |data_map| ... } click to toggle source
# File lib/mappable_object_attributes/mappable_data.rb, line 16
def define_attributes_map(mapname=:default, &block)
  data_map = self.init_map_named(mapname)  
  # let the model-designer define the mash here
  yield data_map

  # now make accessible if ActiveRecord
  if self.is_activerecord
    data_map.keys.each do |mkey|
      attr_accessible mkey
    end
  end

  return data_map 
end
fetch_map_named(mapname) click to toggle source
# File lib/mappable_object_attributes/mappable_data.rb, line 39
def fetch_map_named(mapname)
  data_maps.fetch(mapname)
end
init_map_named(mapname) click to toggle source
# File lib/mappable_object_attributes/mappable_data.rb, line 43
def init_map_named(mapname)
  data_maps[mapname] ||= DataAttributesMap.new 
end
make_hash_from(hash_object, mapname=:default) click to toggle source

This is called by an importing function, with the expectation that @@data_attributes_map has been defined

Returns a Hashie::Mash that assign_attributes/update_attributes can be called from

# File lib/mappable_object_attributes/mappable_data.rb, line 52
def make_hash_from(hash_object, mapname=:default)
  data_mash = Hashie::Mash.new(hash_object)
  built_mash = Hashie::Mash.new

  # build from the specified data_attributes_map
  specific_data_map = self.fetch_map_named(mapname)
  specific_data_map.each_pair do |key, proc|
    built_mash[key] = proc.call(data_mash)
  end

  return built_mash
end
make_mapped_atts_accessible(data_hsh) click to toggle source

pre: Has access to ActiveController

# File lib/mappable_object_attributes/mappable_data.rb, line 82
      def make_mapped_atts_accessible(data_hsh)
#        params = ActionController::Parameters.new(data_hsh)
#        permitted_params = params.permit!

#        return permitted_params

        data_hsh
      end
mapped_attribute_keys(mapname=:default) click to toggle source
# File lib/mappable_object_attributes/mappable_data.rb, line 35
def mapped_attribute_keys(mapname=:default)
 data_maps.fetch(mapname).map_keys
end