class Fakecrm::TypeExtension

Attributes

resource[RW]

Public Class Methods

new(resource) click to toggle source
# File lib/fakecrm/resource/extensions/type_extension.rb, line 3
def initialize(resource)
  self.resource = resource
end

Public Instance Methods

add!(custom_attributes) click to toggle source
# File lib/fakecrm/resource/extensions/type_extension.rb, line 12
def add!(custom_attributes)
  custom_attributes.each do |custom_attribute|
    ::Fakecrm.logger.debug "adding custom attribute: #{custom_attribute.name} to #{self.resource.name}"
    add_property!(custom_attribute) 
  end

  # write state
  return self.resource.auto_upgrade! unless custom_attributes.empty?
end
add_property!(custom_attribute) click to toggle source
# File lib/fakecrm/resource/extensions/type_extension.rb, line 28
def add_property!(custom_attribute)
  property_name =  "custom_#{custom_attribute.name}".to_sym

  # do not overwrite existing properties!
  if self.resource.properties[property_name]
    ::Fakecrm.logger.debug "adding custom attribute: #{custom_attribute.name} to #{self.resource.name}"
  end

  case custom_attribute.type
  when "string"
    self.resource.send(:property, property_name, ::DataMapper::Property::String, :length => custom_attribute.max_length.to_i)
  when "text"
    self.resource.send(:property, property_name, ::DataMapper::Property::Text)
  when "enum"
    self.resource.send(:property, property_name, ::DataMapper::Property::Enum, :flags => custom_attribute.valid_values)
  when "multienum"
    # FIXME: multienum should validate valid_values
    self.resource.send(:property, property_name, ::DataMapper::Property::CommaSeparatedList)
  end

  # FIXME: validate mandatory fields
end
remove_custom_properties!() click to toggle source
# File lib/fakecrm/resource/extensions/type_extension.rb, line 22
def remove_custom_properties!
  self.resource.properties.each do |property|
    self.resource.properties.delete(property) if property.name =~ /^custom_/
  end
end
replace!(custom_attributes) click to toggle source
# File lib/fakecrm/resource/extensions/type_extension.rb, line 7
def replace!(custom_attributes)
  remove_custom_properties!
  add!(custom_attributes)
end