Skip to content

Commit

Permalink
Upgrade to crystal 0.18.4 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
waterlink committed Jun 25, 2016
1 parent 7fc2f14 commit c4c95b5
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 55 deletions.
5 changes: 4 additions & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ dependencies:
pool:
github: ysbaddaden/pool
version: ~> 0.2
singleton:
github: waterlink/singleton.cr
version: ~> 0.1

development_dependencies:
mocks:
github: waterlink/mocks.cr
branch: master
version: ~> 0.9
23 changes: 19 additions & 4 deletions spec/fake_adapter.cr
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
require "singleton"

class FakeAdapter < ActiveRecord::Adapter
getter adapter, table_name, primary_field, fields

def initialize(@table_name, @primary_field, @fields, register = true)
@@table_name : String?
@@primary_field : String?
@@fields : Array(String)?
@@register : Bool?

def initialize(@table_name : String, @primary_field : String, @fields : Array(String), register = true)
@adapter = ActiveRecord::NullAdapter.new(table_name.not_nil!, primary_field, fields, register)
end

def initialize
initialize(@@table_name.not_nil!, @@primary_field.not_nil!, @@fields.not_nil!, @@register.not_nil!)
end

def self.instance
@@instance.not_nil!
Singleton.instance_of(self)
end

def self._reset
@@instance = nil
Singleton.reset
end

def self.build(table_name, primary_field, fields, register = true)
(@@instance ||= new(table_name, primary_field, fields, register)).not_nil!
@@table_name = table_name
@@primary_field = primary_field
@@fields = fields
@@register = register
instance
end

macro delegate(to, method)
Expand Down
12 changes: 6 additions & 6 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ class SameQueryExpectation(T)
def initialize(@expected : T)
end

def match(@actual)
ActiveRecord::QueryObject.same_query?(@expected, @actual)
def match(actual)
ActiveRecord::QueryObject.same_query?(@expected, actual)
end

def failure_message(_ignored = nil)
"expected: #{@expected.inspect}\n got: #{@actual.inspect}"
def failure_message(actual)
"expected: #{@expected.inspect}\n got: #{actual.inspect}"
end

def negative_failure_message(_ignored = nil)
"expected: value != #{@expected.inspect}\n got: #{@actual.inspect}"
def negative_failure_message(actual)
"expected: value != #{@expected.inspect}\n got: #{actual.inspect}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/criteria.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module ActiveRecord
Registry.fetch(name)
end

def initialize(@name)
def initialize(@name : String)
end

def expression
Expand Down
8 changes: 7 additions & 1 deletion src/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ module ActiveRecord
end

macro table_name(value)
@@table_name = {{value.stringify}}
set_table_name({{value.id.stringify}})
end

def self.set_table_name(value)
@@table_name = value
end

macro primary(field_declaration)
Expand Down Expand Up @@ -126,6 +130,8 @@ module ActiveRecord
(@@table_name ||= Support.plural(name)).not_nil!
end

@@table_name : String?

def initialize(hash)
hash.each do |field, value|
set_field(field, value)
Expand Down
26 changes: 17 additions & 9 deletions src/model/fields.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
private macro init_typed_fields
{
{% for group in TYPE_GROUPS %}
{{group.id.stringify}} => {{group.id}}.new,
{% end %}
}
end

private macro alias_typed_fields(name)
alias {{name.id}} = {% for group in TYPE_GROUPS %}
{{group.id}} |
{% end %} {{TYPE_GROUPS[0].id}}
end

module ActiveRecord
# Model::Fields represents collection of typed fields for model instance
class Model::Fields
Expand All @@ -21,7 +35,9 @@ module ActiveRecord
alias {{group.id}} = Generic({{group.id}}Types)
{% end %}

private getter typed_fields
alias_typed_fields(GenericSupportedType)

private getter typed_fields : Hash(::String, GenericSupportedType)

def initialize
@typed_fields = init_typed_fields
Expand All @@ -46,11 +62,3 @@ module ActiveRecord
end
end
end

private macro init_typed_fields
{
{% for group in TYPE_GROUPS %}
{{group.id.stringify}} => {{group.id}}.new,
{% end %}
}
end
4 changes: 3 additions & 1 deletion src/null_adapter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ module ActiveRecord
new(table_name, primary_field, fields, register)
end

def initialize(@table_name, @primary_field, @fields, register = true)
@@query_generators : Array(QueryGenerator)?

def initialize(@table_name : String, @primary_field : String, @fields : Array(String), register = true)
@last_id = 0
@records = [] of Hash(String, ActiveRecord::SupportedType)
@deleted = [] of Int32
Expand Down
46 changes: 23 additions & 23 deletions src/query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ module ActiveRecord
Query.new(klass.new(self))
end

class GenericExpression
def initialize(@receiver, @argument)
class GenericExpression(T, K)
def initialize(@receiver : T, @argument : K)
end

def ==(other : GenericExpression)
Expand All @@ -104,8 +104,8 @@ module ActiveRecord
getter argument
end

class UnaryExpression
def initialize(@receiver)
class UnaryExpression(T)
def initialize(@receiver : T)
end

def ==(other : UnaryExpression)
Expand All @@ -119,61 +119,61 @@ module ActiveRecord
getter receiver
end

class Equal < GenericExpression
class Equal(T, K) < GenericExpression(T, K)
end

class NotEqual < GenericExpression
class NotEqual(T, K) < GenericExpression(T, K)
end

class Greater < GenericExpression
class Greater(T, K) < GenericExpression(T, K)
end

class GreaterEqual < GenericExpression
class GreaterEqual(T, K) < GenericExpression(T, K)
end

class Less < GenericExpression
class Less(T, K) < GenericExpression(T, K)
end

class LessEqual < GenericExpression
class LessEqual(T, K) < GenericExpression(T, K)
end

class Or < GenericExpression
class Or(T, K) < GenericExpression(T, K)
end

class And < GenericExpression
class And(T, K) < GenericExpression(T, K)
end

class Xor < GenericExpression
class Xor(T, K) < GenericExpression(T, K)
end

class In < GenericExpression
class In(T, K) < GenericExpression(T, K)
end

class Not < UnaryExpression
class Not(T) < UnaryExpression(T)
end

class IsTrue < UnaryExpression
class IsTrue(T) < UnaryExpression(T)
end

class IsFalse < UnaryExpression
class IsFalse(T) < UnaryExpression(T)
end

class IsUnknown < UnaryExpression
class IsUnknown(T) < UnaryExpression(T)
end

class IsNull < UnaryExpression
class IsNull(T) < UnaryExpression(T)
end

class IsNotTrue < UnaryExpression
class IsNotTrue(T) < UnaryExpression(T)
end

class IsNotFalse < UnaryExpression
class IsNotFalse(T) < UnaryExpression(T)
end

class IsNotUnknown < UnaryExpression
class IsNotUnknown(T) < UnaryExpression(T)
end

class IsNotNull < UnaryExpression
class IsNotNull(T) < UnaryExpression(T)
end
end
end
20 changes: 19 additions & 1 deletion src/query_generator_response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ require "./adapter"

module ActiveRecord
abstract class QueryGenerator
abstract class QueryProtocol
abstract def query
abstract def params

def ==(other : QueryProtocol)
self.query == other.query &&
self.params == other.params
end

def ==(other)
false
end

def hash
{query, params}.hash
end
end

abstract class Response
def match(klass)
return unless klass == self.class
Expand All @@ -23,7 +41,7 @@ module ActiveRecord
class GeneratedQuery < Response
getter query

def initialize(@query)
def initialize(@query : QueryProtocol)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/sql/query_generator.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module Sql
struct Query
class Query < QueryGenerator::QueryProtocol
class EmptyParams
def self.build
{} of String => ::ActiveRecord::SupportedType
Expand All @@ -18,7 +18,7 @@ module ActiveRecord
new(*args)
end

def initialize(@query, params = nil)
def initialize(@query : String, params = nil)
@params = EmptyParams.build
(params || EMPTY_PARAMS).each do |key, value|
@params[key] = value
Expand Down
2 changes: 1 addition & 1 deletion src/support.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ActiveRecord
def snakecase(name)
first = true

String.build(name.bytesize * 2) do |str|
String.build do |str|
name.each_char do |chr|
if first
first = false
Expand Down
12 changes: 7 additions & 5 deletions src/types.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module ActiveRecord
AS = {} of String => Int32
SPEC_TYPES = [] of Int32
TYPE_GROUPS = [] of Int32

macro alias_types(group, special=false, as=nil)
{% as = "#{group.id}Types".id unless as %}
macro alias_types(group, special=false, _as=nil)
{% AS[""] = _as %}
{% AS[""] = "#{group.id}Types".id unless AS.[""] %}
{% TYPE_GROUPS << group unless special %}

alias {{as.id}} =
alias {{AS[""].id}} =
{% for x in SPEC_TYPES %}
{% if x[0].id == group.id %}
{% SPEC_TYPES << {"*Supported*", x[1].id} unless special %}
Expand Down Expand Up @@ -76,8 +78,8 @@ module ActiveRecord
{{default}} == other
end

macro method_missing(name, args, block)
{{default}}.\{{name.id}}(\{{args.argify}}) \{{block}}
macro method_missing(call)
{{default}}.\{{call.name.id}}(\{{call.args.argify}}) \{{call.block}}
end
{{:end.id}}

Expand Down

0 comments on commit c4c95b5

Please sign in to comment.