Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2011, 03:22 PM
raklet's Avatar
raklet raklet is offline
Perl Hacker turned Farmer
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Aug 2003
Location: Idaho Boondocks
Posts: 1,466 raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level)raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level)raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level)raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level)raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level)raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level)raklet User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 15 h 17 m 59 sec
Reputation Power: 85
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

Reply With Quote
  #2  
Old November 17th, 2011, 02:14 PM
twostepted's Avatar
twostepted twostepted is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: Central Washington (USA)
Posts: 518 twostepted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 30 m 12 sec
Reputation Power: 10
Send a message via Yahoo to twostepted
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Help to DRY up code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap