Last active
September 14, 2021 13:54
-
-
Save DmitryBash/883219f1eedf4987fbb5a5403c57e08b to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query(params) | |
# this magic method prepares sql and gets correct data from DB | |
# NO CHANGES HERE | |
puts "Params = #{params}" | |
[ | |
{name: "John", surname: "Doe", age: 33}, | |
{name: "John", surname: "Doe", age: 34}, | |
{name: "John", surname: "Doe", age: 35} | |
] | |
end | |
class ActiveRecord | |
class << self | |
def criteria | |
@criteria ||= {} | |
end | |
def where(args) | |
criteria.merge!(args) | |
self | |
end | |
def order(order) | |
criteria[:order] = order | |
self | |
end | |
def map(&block) | |
query(criteria).map(&block) | |
end | |
end | |
end | |
class User < ActiveRecord | |
end | |
ages = User.where(name: "John").where(surname: "Doe").order("age").map do |u| | |
u[:age] | |
end | |
puts "Ages = #{ages}" # [33, 34, 35] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment