#1
  1. Perl Hacker turned Farmer
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2003
    Location
    Idaho Boondocks
    Posts
    1,466
    Rep Power
    87

    Help to DRY up code


    I'm working on an application that includes single table inheritance classes.

    Here is the structure that I am working on:

    Classes
    ---------------------
    Asset (parent class)
    Acreage (child class of Asset)
    Crop (child class of Asset)

    Relationships
    ---------------------
    Crop belongs_to Acreage


    An Acreage is a physical piece of property that has a size (acres), gps coordinates, physical address, etc.

    A Crop is something that is planted in an Acreage for a season.

    A Crop also has a size (acres), gps coordintes, physical address, etc and can generally pull that information from Acreage. On occasion there may be more than one crop planted in an Acreage (the Acreage is split up). In this case, each Crop object must override the Acreage values with values of its own.

    In my crop.rb model, I set up the attribute definitions as follows:

    Code:
      def acres
        if self.id.nil?  #does a crop object even exist?
          return  #if not, quit
        elsif super.nil?  #does the crop object have override values defined?
          Acreage.find_by_id(self.acreage_id).acres #if not, get values from acreage
        else
          super #else display override values defined in the crop object
        end
      end
    
      def latitude
        if self.id.nil?
          return
        elsif super.nil?
          Acreage.find_by_id(self.acreage_id).latitude
        else
          super
        end
      end
    
      def longitude
        if self.id.nil?
          return
        elsif super.nil?
          Acreage.find_by_id(self.acreage_id).longitude
        else
          super
        end
      end
    But I can see that I am repeating myself in each method to check for existence of the object and whether the object should get its values from acreage or from itself. Can anyone point me in the right direction for cleaning up the repetition?

    Thanks,

    Raklet
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Central Washington (USA)
    Posts
    518
    Rep Power
    11
    Something like this... i mocked the Acerage class so you might have to make some changes to fit with yours such as changing check_nil_id('@acres') to check_nil_id('acres').

    Code:
    class Acerage
      attr_writer :latitude , :longitude, :acres, :id
      def initialize
        @id = 0
      end
    end
    
    class Crop < Acerage
      def get_acres
        check_nil_id('@acres')
      end
    
      def get_latitude
        check_nil_id('@latitude')
      end
    
      def get_longitude
        check_nil_id('@longitude')
      end
      
      private
      def check_nil_id(prop)
        if self.id.nil?  #does a crop object even exist?
          return  #if not, quit
        else #super.nil?  #does the crop object have override values defined?
          return self.instance_variable_get(prop)  #if not, get values from acreage
        #else
        #  return super #else display override values defined in the crop object
        end
      end
    end

IMN logo majestic logo threadwatch logo seochat tools logo