Module: MappableObjectAttributes::MappableData::ClassMethods

Defined in:
lib/mappable_object_attributes/mappable_data.rb

Instance Method Summary collapse

Instance Method Details

#build_from_map(hash_object, mapname = :default) ⇒ Object

ActiveRecord specific


67
68
69
70
71
72
# 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) ⇒ Object


74
75
76
77
78
79
# 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_mapObject


31
32
33
# 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| ... } ⇒ Object

Yields:

  • (data_map)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object


39
40
41
# File 'lib/mappable_object_attributes/mappable_data.rb', line 39

def fetch_map_named(mapname)
  data_maps.fetch(mapname)
end

#init_map_named(mapname) ⇒ Object


43
44
45
# 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) ⇒ Object

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


52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object

pre: Has access to ActiveController


82
83
84
85
86
87
88
89
# 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) ⇒ Object


35
36
37
# File 'lib/mappable_object_attributes/mappable_data.rb', line 35

def mapped_attribute_keys(mapname=:default)
 data_maps.fetch(mapname).map_keys
end