Skip to content

Commit

Permalink
Seperate sorbet sigs from rb files
Browse files Browse the repository at this point in the history
Pull Request Branch: donaldong/seperate_sorbet_sigs_from_rb_files
Pull Request Link: #80
  • Loading branch information
donaldong committed Nov 10, 2020
1 parent 929f91c commit b83fe2d
Show file tree
Hide file tree
Showing 31 changed files with 764 additions and 415 deletions.
13 changes: 2 additions & 11 deletions lib/redcord.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
# frozen_string_literal: true

# typed: strict
# typed: true

require 'sorbet-runtime'

module Redcord
extend T::Sig

@@configuration_blks = T.let(
[],
T::Array[T.proc.params(arg0: T.untyped).void],
)
@@configuration_blks = []

sig {
params(
blk: T.proc.params(arg0: T.untyped).void,
).void
}
def self.configure(&blk)
@@configuration_blks << blk
end

sig { void }
def self._after_initialize!
@@configuration_blks.each do |blk|
blk.call(Redcord::Base)
Expand Down
78 changes: 0 additions & 78 deletions lib/redcord.rbi

This file was deleted.

50 changes: 1 addition & 49 deletions lib/redcord/actions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# typed: strict
# typed: true

require 'sorbet-coerce'

Expand All @@ -13,19 +13,12 @@ class InvalidAction < StandardError; end
end

module Redcord::Actions
extend T::Sig
extend T::Helpers

sig { params(klass: T.class_of(T::Struct)).void }
def self.included(klass)
klass.extend(ClassMethods)
klass.include(InstanceMethods)
end

module ClassMethods
extend T::Sig

sig { returns(Integer) }
def count
Redcord::Base.trace(
'redcord_actions_class_methods_count',
Expand All @@ -37,7 +30,6 @@ def count
end
end

sig { params(args: T::Hash[Symbol, T.untyped]).returns(T.untyped) }
def create!(args)
Redcord::Base.trace(
'redcord_actions_class_methods_create!',
Expand All @@ -60,7 +52,6 @@ def create!(args)
end
end

sig { params(id: T.untyped).returns(T.untyped) }
def find(id)
Redcord::Base.trace(
'redcord_actions_class_methods_find',
Expand All @@ -76,7 +67,6 @@ def find(id)
end
end

sig { params(args: T::Hash[Symbol, T.untyped]).returns(T.untyped) }
def find_by(args)
Redcord::Base.trace(
'redcord_actions_class_methods_find_by_args',
Expand All @@ -86,12 +76,10 @@ def find_by(args)
end
end

sig { params(args: T::Hash[Symbol, T.untyped]).returns(Redcord::Relation) }
def where(args)
Redcord::Relation.new(T.let(self, T.untyped)).where(args)
end

sig { params(id: T.untyped).returns(T::Boolean) }
def destroy(id)
Redcord::Base.trace(
'redcord_actions_class_methods_destroy',
Expand All @@ -109,32 +97,6 @@ def destroy(id)
end

module InstanceMethods
extend T::Sig
extend T::Helpers

abstract!

sig { abstract.returns(T.nilable(ActiveSupport::TimeWithZone)) }
def created_at; end

sig {
abstract.params(
time: ActiveSupport::TimeWithZone,
).returns(T.nilable(ActiveSupport::TimeWithZone))
}
def created_at=(time); end

sig { abstract.returns(T.nilable(ActiveSupport::TimeWithZone)) }
def updated_at; end

sig {
abstract.params(
time: ActiveSupport::TimeWithZone,
).returns(T.nilable(ActiveSupport::TimeWithZone))
}
def updated_at=(time); end

sig { void }
def save!
Redcord::Base.trace(
'redcord_actions_instance_methods_save!',
Expand Down Expand Up @@ -173,7 +135,6 @@ def save!
end
end

sig { returns(T::Boolean) }
def save
save!

Expand All @@ -183,7 +144,6 @@ def save
false
end

sig { params(args: T::Hash[Symbol, T.untyped]).void }
def update!(args)
Redcord::Base.trace(
'redcord_actions_instance_methods_update!',
Expand Down Expand Up @@ -215,7 +175,6 @@ def update!(args)
end
end

sig { params(args: T::Hash[Symbol, T.untyped]).returns(T::Boolean) }
def update(args)
update!(args)

Expand All @@ -225,7 +184,6 @@ def update(args)
false
end

sig { returns(T::Boolean) }
def destroy
Redcord::Base.trace(
'redcord_actions_instance_methods_destroy',
Expand All @@ -237,30 +195,24 @@ def destroy
end
end

sig { returns(String) }
def instance_key
"#{self.class.model_key}:id:#{T.must(id)}"
end

sig { params(args: T::Hash[Symbol, T.untyped]).void }
def _set_args!(args)
args.each do |key, value|
send(:"#{key}=", value)
end
end

sig { returns(T.nilable(String)) }
def id
instance_variable_get(:@_id)
end

private

sig { params(id: String).returns(String) }
def id=(id)
instance_variable_set(:@_id, id)
end
end

mixes_in_class_methods(ClassMethods)
end
33 changes: 3 additions & 30 deletions lib/redcord/attribute.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# frozen_string_literal: true

# typed: strict
# typed: ignore
module Redcord
class InvalidAttribute < StandardError; end
end

module Redcord::Attribute
extend T::Sig
extend T::Helpers

# We implicitly determine what should be a range index on Redis based on Ruby
# type.
RangeIndexType = T.type_alias {
Expand All @@ -30,8 +27,7 @@ module Redcord::Attribute
Time,
)
}

sig { params(klass: T.class_of(T::Struct)).void }

def self.included(klass)
klass.extend(ClassMethods)
klass.include(InstanceMethods)
Expand All @@ -43,15 +39,6 @@ def self.included(klass)
end

module ClassMethods
extend T::Sig

sig do
params(
name: Symbol,
type: T.untyped, # until smth better is proposed
options: T::Hash[Symbol, T.untyped],
).void
end
def attribute(name, type, options = {})
# TODO: support uniq options
# TODO: validate types
Expand All @@ -60,16 +47,14 @@ def attribute(name, type, options = {})
index_attribute(name, type) if options[:index]
end

sig { params(attr: Symbol, type: T.any(Class, T::Types::Base)).void }
def index_attribute(attr, type)
if should_range_index?(type)
class_variable_get(:@@range_index_attributes) << attr
else
class_variable_get(:@@index_attributes) << attr
end
end

sig { params(index_name: Symbol, attrs: T::Array[Symbol]).void }

def custom_index(index_name, attrs)
attrs.each do |attr|
type = props[attr][:type]
Expand All @@ -87,7 +72,6 @@ def custom_index(index_name, attrs)
class_variable_get(:@@custom_index_attributes)[index_name] = attrs
end

sig { params(duration: T.nilable(ActiveSupport::Duration)).void }
def ttl(duration)
class_variable_set(:@@ttl, duration)
end
Expand Down Expand Up @@ -117,37 +101,31 @@ def shard_by_attribute(attr=nil)
class_variable_set(:@@shard_by_attribute, attr)
end

sig { returns(Integer) }
def _script_arg_ttl
class_variable_get(:@@ttl)&.to_i || -1
end

sig { returns(T::Array[Symbol]) }
def _script_arg_index_attrs
class_variable_get(:@@index_attributes).to_a
end

sig { returns(T::Array[Symbol]) }
def _script_arg_range_index_attrs
class_variable_get(:@@range_index_attributes).to_a
end

sig { returns(T::Hash[Symbol, T::Array]) }
def _script_arg_custom_index_attrs
class_variable_get(:@@custom_index_attributes)
end

private

sig { params(type: T.any(Class, T::Types::Base)).returns(T::Boolean) }
def should_range_index?(type)
# Change Ruby raw type to Sorbet type in order to call subtype_of?
type = T::Types::Simple.new(type) if type.is_a?(Class)

type.subtype_of?(RangeIndexType)
end

sig { params(type: T.any(Class, T::Types::Base)).returns(T::Boolean) }
def can_custom_index?(type)
# Change Ruby raw type to Sorbet type in order to call subtype_of?
type = T::Types::Simple.new(type) if type.is_a?(Class)
Expand All @@ -156,9 +134,6 @@ def can_custom_index?(type)
end

module InstanceMethods
extend T::Sig

sig { returns(T.nilable(String)) }
def hash_tag
attr = self.class.class_variable_get(:@@shard_by_attribute)

Expand All @@ -175,6 +150,4 @@ def hash_tag
"{#{tag || default_tag}}"
end
end

mixes_in_class_methods(ClassMethods)
end
Loading

0 comments on commit b83fe2d

Please sign in to comment.