0

I want to create an alternative for activerecord named scope.

module M
  def self.included(klass)
    klass.extend ClassMethods
    klass.class_variable_set("@@obj_array", [])
  end
  module ClassMethods

    def attr_accessor (*args)
      args.each do |method_name|
        define_method :method_name do
        end
      end
      super
    end

    def where(*attr)
      obj1 = []
      a = attr.keys.map { |i| i.to_s }.join()
      c = class_variable_get("@@obj_array")
      c.each do |obj|
        if obj.instance_variable_get("@#{a}") == attr.values.map { |i| i.to_s }.join().to_i || obj.instance_variable_get("@#{a}") == attr.values.map { |i| i.to_s }.join().to_s
          obj1 << obj
        end
      end
      obj1
    end

    def scope (name, lambda)
      self.class_eval do

        define_singleton_method "#{name}" do
          lambda.call
        end
      end
    end
  end

  def initialize
    self.class.class_variable_get("@@obj_array") << self
  end
end

class User
  include M
  attr_accessor :age, :email, :status
  scope :children, ->{ where(age: 17) }
  scope :active, ->{ where(status: "active") }
  scope :person, ->{ where(email: "foo@abc..com")}

  # scope :some, -> {where(age: 5, email: 'fdsa@fjdsk.com')}

end

a = User.new
b = User.new
c = User.new
a.age = 17
b.age = 12
b.status = "active"
c.email = "foo@abc..com"
# p User.children.all
# p User.active.all
#User.children.active.all

I have written the above code. User.active returns an array of objects with status active. I have done that.

User.active.all should returns an array of objects with status active. Not able to do with all method User.children.active.all should return children with age 17 and status active. I don't know how to go about it.

eleena
  • 1
  • 3
  • 1
    The example is stand-alone (it executes, other than the commented-out lines), and doesn't require Rails or ActiveRecord in order to run. Although the code resembles ActiveRecord, it doesn't require ActiveRecord. Can you please explain your question's relationship to Rails? Is this code to be used within a Rails project where you want to create some objects that work like ActiveRecord objects? – Wayne Conrad Aug 22 '16 at 12:52
  • it is actually an assignment i need to clear in next 30 minutes and its really very urgent. i am just a beginner in ruby i need to make it work somehow without using anything from rails/ I want to get output for User.children.active.all somehow – eleena Aug 22 '16 at 13:05
  • @eleena ActiveRecord accomplishes this by returning proxy objects from its `where` method. These objects behave like arrays but also respond to `where` for additional filtering. That's really the only sensible way of accomplishing this task. – user229044 Aug 22 '16 at 13:08
  • I dont have idea about ActiveRecord. Could you please elaborate how would this be accomplished – eleena Aug 22 '16 at 13:13
  • It is very URGENT. I really need the solution in next few minutes – eleena Aug 22 '16 at 13:35
  • 2
    I hate to be discouraging, but this is unfortunately too complex a task to teach in a few minutes. Even if you knew Ruby well and understood the design pattern that @meagar is suggesting, it would be difficult to write in 30 minutes. I wonder why someone assigned this to you with such a short deadline, and before you knew the language well. – Wayne Conrad Aug 22 '16 at 13:37
  • 2
    @eleena No. Please stop bothering people to do your homework for you. This isn't what Stack Overflow is for. We sympathize, but you're not going to find somebody here to provide you with ready-to-submit code for your assignment. – user229044 Aug 22 '16 at 13:42

0 Answers0