How should a custom time schedule class / object look in Ruby or Rails?

Question

I've refactored this a bit, but I still feel something's missing.

I have 5 classes working together

User, Schedule, WeekBlock, DayBlock, Block

It's setup so that a User creates a schedule for when they are available, which is saved in a JSON string in this format, with multiple weeks, which is saved in the database.

#{ Week:        [{"time,week day": "end time"}]}
{ "2017/01/01": [{ "15:30,0":     "16:30" }, { "14:30,2": "15:30" }] }

When I do this:

user = User.find params[:id]
schedule = Schedule.new(user)

The initialize method will create a WeekBlock for each week, and a DayBlock object for each day which each has an array of each Block for that day.

The parent object is saved as an instance variable in each object, for example:

class Schedule
  def initialize
    # for each week
       WeekBlock.new self, opts
    # end
  end
end
class WeekBlock
  def initialize(parent, opts)
    @parent = parent
    # for each day
       DayBlock.new self, opts
    # end
  end
end
class DayBlock
  def initialize(parent, opts)
    @parent = parent
    # for each day 
      Block.new self, opts
    # end
  end
end
class Block
  def initialize(parent, opts)
    @parent = parent
  end
end

When I create the Schedule object in the console, it's enormous due to all the references to each parent.

Should I only pass the variables that I need from the parent class instead of self? Or should all of this be one class?


Show source
| class   | ruby-on-rails   | ruby   | ruby-on-rails-4   | json   2017-01-07 01:01 0 Answers

Answers ( 0 )

◀ Go back