Skip to content

Commit

Permalink
Merge pull request #502 from Shopify/at-smart-properties-module
Browse files Browse the repository at this point in the history
  • Loading branch information
paracycle authored Sep 16, 2021
2 parents 114ceb5 + 82dc4f9 commit eba4a5c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ GEM
connection_pool (>= 2.2.2)
rack (~> 2.0)
redis (>= 4.2.0)
smart_properties (1.15.0)
smart_properties (1.16.2)
sorbet (0.5.9125)
sorbet-static (= 0.5.9125)
sorbet-runtime (0.5.9125)
Expand Down
8 changes: 4 additions & 4 deletions lib/tapioca/compilers/dsl/smart_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
all_classes.select do |c|
c < ::SmartProperties
end.reject do |c|
name_of(c).nil? || c == ::SmartProperties::Validations::Ancestor
all_modules.select do |c|
name_of(c) &&
c != ::SmartProperties::Validations::Ancestor &&
c < ::SmartProperties && ::SmartProperties::ClassMethods === c
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# DO NOT EDIT MANUALLY
# This is an autogenerated file for types exported from the `smart_properties` gem.
# Please instead update this file by running `tapioca sync`.
# Please instead update this file by running `bin/tapioca gem smart_properties`.

# typed: true

module SmartProperties
mixes_in_class_methods(::SmartProperties::ClassMethods)
mixes_in_class_methods ::SmartProperties::ClassMethods
mixes_in_class_methods ::SmartProperties::ModuleMethods

def initialize(*args, &block); end

def [](name); end
def []=(name, value); end

class << self

private

def included(base); end
Expand All @@ -24,9 +24,9 @@ class SmartProperties::AssignmentError < ::SmartProperties::Error
def initialize(sender, property, message); end

def property; end
def property=(_); end
def property=(_arg0); end
def sender; end
def sender=(_); end
def sender=(_arg0); end
end

module SmartProperties::ClassMethods
Expand All @@ -38,28 +38,25 @@ module SmartProperties::ClassMethods
def property!(name, options = T.unsafe(nil)); end
end

class SmartProperties::ConfigurationError < ::SmartProperties::Error
end
class SmartProperties::ConfigurationError < ::SmartProperties::Error; end

class SmartProperties::ConstructorArgumentForwardingError < ::SmartProperties::Error
def initialize(positional_arguments, keyword_arguments); end


private

def generate_description(argument_type, argument_number); end
end

class SmartProperties::Error < ::ArgumentError
end
class SmartProperties::Error < ::ArgumentError; end

class SmartProperties::InitializationError < ::SmartProperties::Error
def initialize(sender, properties); end

def properties; end
def properties=(_); end
def properties=(_arg0); end
def sender; end
def sender=(_); end
def sender=(_arg0); end
def to_hash; end
end

Expand All @@ -68,7 +65,7 @@ class SmartProperties::InvalidValueError < ::SmartProperties::AssignmentError

def to_hash; end
def value; end
def value=(_); end
def value=(_arg0); end

private

Expand All @@ -81,6 +78,10 @@ class SmartProperties::MissingValueError < ::SmartProperties::AssignmentError
def to_hash; end
end

module SmartProperties::ModuleMethods
def included(target); end
end

class SmartProperties::Property
def initialize(name, attrs = T.unsafe(nil)); end

Expand Down Expand Up @@ -115,11 +116,10 @@ class SmartProperties::Property
end

SmartProperties::Property::ALLOWED_DEFAULT_CLASSES = T.let(T.unsafe(nil), Array)

SmartProperties::Property::MODULE_REFERENCE = T.let(T.unsafe(nil), Symbol)

class SmartProperties::PropertyCollection
include(::Enumerable)
include ::Enumerable

def initialize; end

Expand All @@ -136,11 +136,11 @@ class SmartProperties::PropertyCollection
protected

def children; end
def children=(_); end
def children=(_arg0); end
def collection; end
def collection=(_); end
def collection=(_arg0); end
def collection_with_parent_collection; end
def collection_with_parent_collection=(_); end
def collection_with_parent_collection=(_arg0); end
def notify_children; end
def refresh(parent_collection); end

Expand All @@ -150,19 +150,18 @@ class SmartProperties::PropertyCollection
end

SmartProperties::VERSION = T.let(T.unsafe(nil), String)

module SmartProperties::Validations
end
module SmartProperties::Validations; end

class SmartProperties::Validations::Ancestor
include(::SmartProperties)
extend(::SmartProperties::ClassMethods)
include ::SmartProperties
extend ::SmartProperties::ClassMethods
extend ::SmartProperties::ModuleMethods

def to_proc; end
def to_s; end
def validate(klass); end

class << self
def must_be(*_); end
def must_be(*_arg0); end
end
end
42 changes: 38 additions & 4 deletions spec/tapioca/compilers/dsl/smart_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Tapioca::Compilers::Dsl::SmartPropertiesSpec < DslSpec
assert_empty(gathered_constants)
end

it("gathers only SmartProperty classes") do
it("gathers only SmartProperty classes and modules") do
add_ruby_file("content.rb", <<~RUBY)
class Post
include ::SmartProperties
Expand All @@ -21,16 +21,27 @@ class User
class Comment
end
module Viewable
include ::SmartProperties
end
module Editable
end
RUBY

assert_equal(["Post", "User"], gathered_constants)
assert_equal(["Post", "User", "Viewable"], gathered_constants)
end

it("ignores SmartProperty classes without a name") do
it("ignores SmartProperty classes and modules without a name") do
add_ruby_file("content.rb", <<~RUBY)
post = Class.new do
include ::SmartProperties
end
viewable = Module.new do
include ::SmartProperties
end
RUBY

assert_empty(gathered_constants)
Expand All @@ -52,7 +63,7 @@ class Post
assert_equal(expected, rbi_for(:Post))
end

it("generates RBI file for simple smart property") do
it("generates RBI file for simple smart property class") do
add_ruby_file("post.rb", <<~RUBY)
class Post
include SmartProperties
Expand All @@ -75,6 +86,29 @@ def title=(title); end
assert_equal(expected, rbi_for(:Post))
end

it("generates RBI file for simple smart property module") do
add_ruby_file("viewable.rb", <<~RUBY)
module Viewable
include SmartProperties
property :title, accepts: String
end
RUBY

expected = <<~RBI
# typed: strong
module Viewable
sig { returns(T.nilable(::String)) }
def title; end
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
def title=(title); end
end
RBI

assert_equal(expected, rbi_for(:Viewable))
end

it("generates RBI file for required smart property") do
add_ruby_file("post.rb", <<~RUBY)
class Post
Expand Down

0 comments on commit eba4a5c

Please sign in to comment.