Created
August 17, 2017 20:07
-
-
Save nikivazou/48d84b78e65357d98f042240cf227572 to your computer and use it in GitHub Desktop.
This file contains 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
require 'rdl' | |
require 'types/core' | |
extend RDL::Annotate | |
require 'rubygems' | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
:adapter => "mysql2", | |
:host => "localhost", | |
:database => "students", | |
:password => "pass", | |
:ignored_columns => "true" | |
) | |
ActiveRecord::Migration::drop_table :friends | |
unless ActiveRecord::Base.connection.table_exists?(:friends) | |
ActiveRecord::Base.connection.create_table :friends do |t| | |
# :id is created automatically | |
t.string :name | |
t.string :city | |
end | |
end | |
class Friends < ActiveRecord::Base | |
validates :name, presence: true | |
validates :city, presence: true | |
after_initialize :init | |
def init | |
self.name ||= "?" #will set the default value only if it's nil | |
self.city ||= "?" #let's you set a default association | |
end | |
end | |
class Test | |
extend RDL::Annotate | |
# type 'ActiveRecord::Persistence::ClassMethods', :create, '(**%any) -> self' | |
# type 'Friends', :create, '(name: String, **%any) -> self' | |
# type '() -> Integer' #, typecheck: :now | |
def fcreate() | |
# this ignores the type of Friends.create and uses the ActiveRecord.create type | |
f0 = Friends.create(:name => 'You') | |
# Friends.new.create uses the Friends.create type | |
# but crashes at runtime | |
# f1 = Friends.new.create(:name => 'You') | |
# f2 = Friends.new.create(:name => 'Me') | |
# puts %{ FO = #{f0} with name = #{f0.name}} | |
# puts %{ F1 = ${f1} with name = ${f1.name}} | |
5 | |
end | |
Test.new.fcreate() | |
puts "Done Done Done\n\n\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment