Skip to content

Commit

Permalink
Merge pull request #59 from waterlink/upgrade-to-crystal-0.20.x
Browse files Browse the repository at this point in the history
Upgrade to crystal 0.20.x
  • Loading branch information
waterlink committed Jan 1, 2017
2 parents 313eda6 + da31e37 commit dd03c35
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/.deps/
/libs/
/lib/
.crystal/
/shard.lock
/.shards
Expand Down
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ dependencies:
version: ~> 0.2
query:
github: waterlink/query.cr
version: ~> 0.1
version: ~> 0.2
development_dependencies:
mocks:
github: waterlink/mocks.cr
version: ~> 0.9
version: ~> 0.9.3
singleton:
github: waterlink/singleton.cr
version: ~> 0.1
version: ~> 1.0
20 changes: 10 additions & 10 deletions spec/active_record_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module ActiveRecord
def call(params, fields)
return false unless fields.has_key?("number_of_dependents") &&
params.has_key?("1")
actual = fields["number_of_dependents"] as Int
expected = params["1"] as Int
actual = fields["number_of_dependents"].as(Int)
expected = params["1"].as(Int)
actual > expected
end
end
Expand All @@ -17,8 +17,8 @@ module ActiveRecord
def call(params, fields)
return false unless fields.has_key?("number_of_dependents") &&
params.has_key?("1")
actual = fields["number_of_dependents"] as Int
expected = params["1"] as Int
actual = fields["number_of_dependents"].as(Int)
expected = params["1"].as(Int)
actual < expected
end
end
Expand All @@ -29,9 +29,9 @@ module ActiveRecord
def call(params, fields)
return false unless fields.has_key?("number_of_dependents") &&
params.has_key?("1") && params.has_key?("2")
actual = fields["number_of_dependents"] as Int
low = params["1"] as Int
high = params["2"] as Int
actual = fields["number_of_dependents"].as(Int)
low = params["1"].as(Int)
high = params["2"].as(Int)
actual > low && actual < high
end
end
Expand All @@ -40,10 +40,10 @@ module ActiveRecord
def call(params, fields)
return false unless fields.has_key?("number_of_dependents") &&
params.has_key?("1") && params.has_key?("2")
actual = fields["number_of_dependents"] as Int
actual = fields["number_of_dependents"].as(Int)
array = [] of Int32
array << params["1"] as Int32
array << params["2"] as Int32
array << params["1"].as(Int32)
array << params["2"].as(Int32)
array.includes?(actual)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fake_adapter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FakeAdapter < ActiveRecord::Adapter
end

def self.instance
Singleton.instance_of(self)
Singleton::Of(self).instance
end

def self._reset
Expand Down
8 changes: 4 additions & 4 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
ifdef !active_record_adapter
{% if !flag?(:active_record_adapter) %}
require "spec"
require "mocks"
require "../src/active_record"
require "../src/null_adapter"
require "../src/criteria_helper"
require "./fake_adapter"
end
{% end %}

def _specs_reset
ActiveRecord::NullAdapter.reset
FakeAdapter._reset

ifdef !active_record_adapter
{% if !flag?(:active_record_adapter) %}
Mocks.reset
end
{% end %}
end

Spec.before_each do
Expand Down
60 changes: 26 additions & 34 deletions spec/sql/query_generator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ module ActiveRecord

example "or" do
query = (criteria("number") < 29) |
(criteria("other_number") == 2)
.or(criteria("kind") == "none")
(criteria("other_number") == 2)
.or(criteria("kind") == "none")

expected_query = Query.new(
"(number < :1) OR ((other_number = :2) OR (kind = :3))",
Expand All @@ -93,7 +93,7 @@ module ActiveRecord
generate(query).should eq(expected_query)

query = ((criteria("number") < 29) |
(criteria("other_number") == 2))
(criteria("other_number") == 2))
.or(criteria("kind") == "none")

expected_query = Query.new(
Expand All @@ -106,8 +106,8 @@ module ActiveRecord

example "and" do
query = (criteria("number") < 29) &
(criteria("other_number") == 2)
.and(criteria("kind") == "none")
(criteria("other_number") == 2)
.and(criteria("kind") == "none")

expected_query = Query.new(
"(number < :1) AND ((other_number = :2) AND (kind = :3))",
Expand All @@ -119,8 +119,8 @@ module ActiveRecord

example "mixing and, or" do
query = (criteria("number") < 29) &
(criteria("other_number") == 2)
.or(criteria("kind") == "none")
(criteria("other_number") == 2)
.or(criteria("kind") == "none")

expected_query = Query.new(
"(number < :1) AND ((other_number = :2) OR (kind = :3))",
Expand Down Expand Up @@ -160,33 +160,25 @@ module ActiveRecord
end

example "IS expressions" do
[

{criteria("bool").is_true, "(bool) IS TRUE", Query::EMPTY_PARAMS},
{criteria("bool").is_not_true, "(bool) IS NOT TRUE", Query::EMPTY_PARAMS},
{(criteria("bool") < 3).is_true, "(bool < :1) IS TRUE", {"1" => 3}},
{(criteria("bool") < 3).is_not_true, "(bool < :1) IS NOT TRUE", {"1" => 3}},

{criteria("bool").is_false, "(bool) IS FALSE", Query::EMPTY_PARAMS},
{criteria("bool").is_not_false, "(bool) IS NOT FALSE", Query::EMPTY_PARAMS},
{(criteria("bool") < 3).is_false, "(bool < :1) IS FALSE", {"1" => 3}},
{(criteria("bool") < 3).is_not_false, "(bool < :1) IS NOT FALSE", {"1" => 3}},

{criteria("something").is_unknown, "(something) IS UNKNOWN", Query::EMPTY_PARAMS},
{criteria("something").is_not_unknown, "(something) IS NOT UNKNOWN", Query::EMPTY_PARAMS},
{(criteria("something") < 3).is_unknown, "(something < :1) IS UNKNOWN", {"1" => 3}},
{(criteria("something") < 3).is_not_unknown, "(something < :1) IS NOT UNKNOWN", {"1" => 3}},

{criteria("something").is_null, "(something) IS NULL", Query::EMPTY_PARAMS},
{criteria("something").is_not_null, "(something) IS NOT NULL", Query::EMPTY_PARAMS},
{(criteria("something") < 3).is_null, "(something < :1) IS NULL", {"1" => 3}},
{(criteria("something") < 3).is_not_null, "(something < :1) IS NOT NULL", {"1" => 3}},

].each do |example|
query = example[0]
expected_query = Query[example[1], example[2]]
generate(query).should eq(expected_query)
end
generate(criteria("bool").is_true).should eq(Query["(bool) IS TRUE", Query::EMPTY_PARAMS])
generate(criteria("bool").is_not_true).should eq(Query["(bool) IS NOT TRUE", Query::EMPTY_PARAMS])
generate((criteria("bool") < 3).is_true).should eq(Query["(bool < :1) IS TRUE", {"1" => 3}])
generate((criteria("bool") < 3).is_not_true).should eq(Query["(bool < :1) IS NOT TRUE", {"1" => 3}])

generate(criteria("bool").is_false).should eq(Query["(bool) IS FALSE", Query::EMPTY_PARAMS])
generate(criteria("bool").is_not_false).should eq(Query["(bool) IS NOT FALSE", Query::EMPTY_PARAMS])
generate((criteria("bool") < 3).is_false).should eq(Query["(bool < :1) IS FALSE", {"1" => 3}])
generate((criteria("bool") < 3).is_not_false).should eq(Query["(bool < :1) IS NOT FALSE", {"1" => 3}])

generate(criteria("something").is_unknown).should eq(Query["(something) IS UNKNOWN", Query::EMPTY_PARAMS])
generate(criteria("something").is_not_unknown).should eq(Query["(something) IS NOT UNKNOWN", Query::EMPTY_PARAMS])
generate((criteria("something") < 3).is_unknown).should eq(Query["(something < :1) IS UNKNOWN", {"1" => 3}])
generate((criteria("something") < 3).is_not_unknown).should eq(Query["(something < :1) IS NOT UNKNOWN", {"1" => 3}])

generate(criteria("something").is_null).should eq(Query["(something) IS NULL", Query::EMPTY_PARAMS])
generate(criteria("something").is_not_null).should eq(Query["(something) IS NOT NULL", Query::EMPTY_PARAMS])
generate((criteria("something") < 3).is_null).should eq(Query["(something < :1) IS NULL", {"1" => 3}])
generate((criteria("something") < 3).is_not_null).should eq(Query["(something < :1) IS NOT NULL", {"1" => 3}])
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions spec/types_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ require "./spec_helper"

module StuffGenerator
def self.an_int
37 as ActiveRecord::SupportedType
37.as(ActiveRecord::SupportedType)
end

def self.an_int_null
Int::Null.new as ActiveRecord::SupportedType
Int::Null.new.as(ActiveRecord::SupportedType)
end

def self.a_string
"hello world" as ActiveRecord::SupportedType
"hello world".as(ActiveRecord::SupportedType)
end

def self.a_string_null
String::Null.new as ActiveRecord::SupportedType
String::Null.new.as(ActiveRecord::SupportedType)
end
end

macro test_non_null(ty, value, other_value, name="_", check_op="")
macro test_non_null(ty, value, other_value, name = "_", check_op = "")
context "when {{ty}} is not null" do
subject = {{value}} as SupportedType
subject = {{value}}.as SupportedType

it "has correct type" do
typeof(subject).should_not eq {{ty}}
Expand All @@ -42,7 +42,7 @@ context "when {{ty}} is not null" do
end

it "forwards calls to original value" do
{{name.id}} = subject as {{ty}}
{{name.id}} = subject.as({{ty}})
({{check_op.id}}).should eq(true)
end

Expand All @@ -54,9 +54,9 @@ context "when {{ty}} is not null" do
end
end

macro test_null(ty, zero, other_value, name="_", check_op="true")
macro test_null(ty, zero, other_value, name = "_", check_op = "true")
context "when {{ty}} is null" do
subject = {{ty}}::Null.new as SupportedType
subject = {{ty}}::Null.new.as(SupportedType)

it "has correct type" do
typeof(subject).should_not eq {{ty}}
Expand All @@ -79,7 +79,7 @@ context "when {{ty}} is null" do
end

it "forwards calls to zero-like value" do
{{name.id}} = subject as {{ty}}::Null
{{name.id}} = subject.as({{ty}}::Null)
({{check_op.id}}).should eq(true)
end

Expand All @@ -93,11 +93,11 @@ end

macro test_comparable(ty, spec_ty, value, zero)
it "implements correct Comparable" do
subject = {{value}} as SupportedType
null = {{ty}}::Null.new as SupportedType
subject = {{value}}.as(SupportedType)
null = {{ty}}::Null.new.as(SupportedType)

((subject as {{spec_ty}}) <=> {{zero}}).should eq({{value}} <=> {{zero}})
((null as {{ty}}::Null) <=> {{value}}).should eq({{zero}} <=> {{value}})
(subject.as({{spec_ty}}) <=> {{zero}}).should eq({{value}} <=> {{zero}})
(null.as({{ty}}::Null) <=> {{value}}).should eq({{zero}} <=> {{value}})
end
end

Expand Down
14 changes: 7 additions & 7 deletions src/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module ActiveRecord

MACRO_CURRENT = [] of String

DEFAULT_CONNECTION_POOL_CAPACITY = 1
DEFAULT_CONNECTION_POOL_TIMEOUT = 2.0
DEFAULT_CONNECTION_POOL_CAPACITY = 1
DEFAULT_CONNECTION_POOL_TIMEOUT = 2.0

macro null_object(name_and_super, &block)
class {{name_and_super.receiver}} < {{name_and_super.args[0]}}
Expand Down Expand Up @@ -53,12 +53,12 @@ module ActiveRecord
\{% MACRO_FIELDS_{{MACRO_CURRENT.last.id}} << name %}

{{level.id}} def \{{name.id}}=(value : \{{field_declaration.type}})
typed_fields = fields[\{{field_declaration.type.stringify}}] as ::ActiveRecord::Model::Fields::\{{field_declaration.type.id}}
typed_fields = fields[\{{field_declaration.type.stringify}}].as ::ActiveRecord::Model::Fields::\{{field_declaration.type.id}}
typed_fields.fields[\{{field_declaration.var.stringify}}] = value
end

{{level.id}} def \{{name.id}}
typed_fields = fields[\{{field_declaration.type.stringify}}] as ::ActiveRecord::Model::Fields::\{{field_declaration.type.id}}
typed_fields = fields[\{{field_declaration.type.stringify}}].as ::ActiveRecord::Model::Fields::\{{field_declaration.type.id}}
typed_fields.fields.fetch(\{{field_declaration.var.stringify}}, \{{field_declaration.type}}::Null.new)
end

Expand Down Expand Up @@ -145,7 +145,7 @@ module ActiveRecord
null_value
end

def self.build(hash : Hash(K, V))
def self.build(hash : Hash(K, V)) forall K, V
new(hash)
end

Expand All @@ -157,7 +157,7 @@ module ActiveRecord
def self.get(primary_key)
pool.connection do |adapter|
if adapter.get(primary_key)
build(adapter.get(primary_key)) as self
build(adapter.get(primary_key)).as(self)
else
raise RecordNotFoundException.new("Record not found with given id.")
end
Expand Down Expand Up @@ -228,7 +228,7 @@ module ActiveRecord
private def set_field(field, value)
return unless self.class.fields.includes?(field)
fields[self.class.field_types[field]]
.set_field(field, value)
.set_field(field, value)
end

def self.null_value
Expand Down
10 changes: 5 additions & 5 deletions src/null_adapter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ module ActiveRecord
end

def get(primary_key)
return nil if deleted.includes?((primary_key as Int32) - 1)
records[(primary_key as Int32) - 1]
return nil if deleted.includes?((primary_key.as(Int32)) - 1)
records[(primary_key.as(Int32)) - 1]
end

def create(fields)
Expand All @@ -63,7 +63,7 @@ module ActiveRecord
records
end

def where(query : Query::Query)
def where(query : ::Query::Query)
query = self.class.generate_query(query).not_nil!
_where(query.query, query.params)
end
Expand Down Expand Up @@ -104,11 +104,11 @@ module ActiveRecord
end

def update(primary_key, fields)
records[(primary_key as Int32) - 1] = fields.dup
records[(primary_key.as(Int32)) - 1] = fields.dup
end

def delete(primary_key)
deleted << (primary_key as Int32) - 1
deleted << (primary_key.as(Int32)) - 1
end

def _reset
Expand Down
Loading

0 comments on commit dd03c35

Please sign in to comment.