Sha256: 9e84bad5e1aea5bb1bcad40d3f9878d00398b009b7ebf7448d2de5bad5d91712

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module CanBe
  module Processor
    class Instance
      def initialize(model)
        @model = model
        @config = model.class.can_be_config
        @field_name = @config.field_name
      end

      def boolean_eval(t)
        field_value == t
      end

      def update_field(t, save = false)
        if save
          original_details = @model.details
          @model.update_attributes(@field_name => t)
          original_details.destroy unless original_details.class == @model.details.class
        else
          self.field_value = t
        end
      end

      def field_value=(t)
        set_details(t)
        @model.send(:write_attribute, @field_name, t)
      end

      def field_value
        @model.read_attribute(@field_name)
      end

      def set_default_field_value
        self.field_value = @config.default_type if self.field_value.nil?
      end

      def initialize_details
        set_details(field_value.to_sym) if has_details? && !@model.details_id
      end

      private
      def has_details?
        @model.respond_to?(:details) && @model.respond_to?(:details_id) && @model.respond_to?(:details_type)
      end

      def set_details(t)
        return unless has_details?

        classname = @config.details[t.to_sym]

        if classname
          @model.details = classname.constantize.new
        else
          @model.details_id = nil
          @model.details_type = nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
can_be-0.2.0 lib/can_be/processor/instance.rb