class Reactor::Plans::CommonObjClass

Constants

ALLOWED_PARAMS

Public Class Methods

new() click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 9
def initialize
  @take_attrs = []
  @drop_attrs = []
  @mandatory_attrs = []
  @mandatory_drop_attrs = []
  @preset_attrs = {}
  @params = {}
  @params_options = {}
end

Public Instance Methods

drop(attr_name, opts={}) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 37
def drop(attr_name, opts={})
  attr_name = attr_name.to_s
  @drop_attrs << attr_name
  @take_attrs.delete(attr_name)
  @mandatory_attrs.delete(attr_name)
  @preset_attrs.delete(attr_name)
end
migrate!() click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 45
def migrate!
  raise "#{self.class.name} did not implement migrate!"
end
preset(attribute, value) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 24
def preset(attribute, value)
  @preset_attrs[attribute] = value
end
set(key, value, options={}) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 19
def set(key, value, options={})
  @params_options[key.to_sym] = options
  @params[key.to_sym] = value
end
take(attr_name, opts={}) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 28
def take(attr_name, opts={})
  attr_name = attr_name.to_s
  @take_attrs << attr_name
  @drop_attrs.delete(attr_name)
  @mandatory_attrs << attr_name if opts[:mandatory] == true
  @mandatory_drop_attrs << attr_name if opts[:mandatory] == false
  @preset_attrs[attr_name] = opts[:preset] if opts.key? :preset
end

Protected Instance Methods

migrate_attrs!(klass) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 60
def migrate_attrs!(klass)
  attrs = (klass.attributes + @take_attrs).uniq - @drop_attrs
  klass.attributes = attrs
  klass.mandatory_attributes = ((klass.mandatory_attributes + @mandatory_attrs).uniq - @drop_attrs - @mandatory_drop_attrs)
end
migrate_params!(klass) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 66
def migrate_params!(klass)
  @params.each{|k,v|klass.set(k,v,@params_options[k])}
  klass.preset_attributes.merge(@preset_attrs).each{|k,v|klass.preset(k,v)}
  klass.save!
end
prepare_attrs!(klass=nil) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 50
def prepare_attrs!(klass=nil)
  @take_attrs.each do |attr|
    error("attribute doesn't exist #{attr}") unless Reactor::Cm::Attribute.exists?(attr)
  end
end
prepare_params!(klass=nil) click to toggle source
# File lib/reactor/plans/common_obj_class.rb, line 56
def prepare_params!(klass=nil)
  @params.keys.each{|k| error("unknown parameter: #{k}") unless ALLOWED_PARAMS.include? k}
end