Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile
require "sqlite3"
SQLite3::Database.new(":memory:") do |db|
rows = db.execute <<-SQL
create table numbers (
name varchar(30),
val int
);
SQL
class Composite
def initialize(obj)
@target = obj
end
def extend(component)
@target = self.class.new(component.new(@target))
end
def method_missing(id, *args, &block)
>> User.ancestors
=> [User(...), ActiveRecord::Base, ActiveRecord::Aggregations, ActiveRecord::Transactions, 
ActiveRecord::Reflection, ActiveRecord::Serialization, ActiveModel::Serializers::Xml, 
ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::AutosaveAssociation, 
ActiveRecord::NestedAttributes, ActiveModel::SecurePassword, ActiveRecord::IdentityMap, 
ActiveRecord::Associations, ActiveRecord::NamedScope, ActiveModel::Validations::Callbacks, 
ActiveRecord::Callbacks, ActiveModel::Observing, ActiveRecord::Timestamp, 
ActiveModel::MassAssignmentSecurity, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey, 
ActiveRecord::AttributeMethods::Read, ActiveRecord::AttributeMethods::Write, 
require "forwardable"
class BigAssObject
def initialize
@sub_object_1 = Something.new
@sub_object_2 = SomethingElse.new
@sub_object_3 = SomethingEEEELSE.new
end
delegate [:a, :b, :c, :d] => :sub_object_1
eval(DATA.read) # :trollface:
## USE VELLUM TO DESCRIBE A DRAWING IN THE ABSTRACT
drawing = Vellum::Drawing.new(300,400)
drawing.layer(:box) do |g|
g.rect(g.top_left, g.width, g.height)
end
ARTICLE_DATA = { 1 => { :status => "published" },
2 => { :status => "draft" } }
module BrokenRecord
class Table
def initialize(record_class)
@record_class = record_class
end
class A
def a
b
end
def b
puts "YAY"
end
end
class A
def initialize
@b = B.new
end
def method_missing(m, *a, &b)
@b.public_send(m, *a, &b) # changing this to use send() fixes the error
end
require "minitest/autorun"
require "delegate"
require "sqlite3"
describe "Mapping fields in results" do
before do
BrokenRecord.database = SQLite3::Database.new(":memory:")
BrokenRecord.database.execute %{
def update(id, params)
# FIXME: This is not secure
sql = params.map { |k,v| "#{k} = #{v.inspect}" }.join(", ")
# BrokenRecord.database is just an instance of Sqlite::Database
BrokenRecord.database.execute %{
update #{@table_name}
set #{sql}
where id = #{id}
}