-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add attrjson, paperclip, fix statemachines bug, bump tapioca
- Loading branch information
1 parent
d5ba230
commit 9a0a956
Showing
11 changed files
with
287 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
ruby "3.3.5" | ||
ruby "3.3.6" | ||
|
||
source "https://rubygems.org" | ||
gemspec | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
# frozen_string_literal: true | ||
|
||
module Boba | ||
VERSION = "0.0.8" | ||
VERSION = "0.0.9" | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# frozen_string_literal: true | ||
# typed: true | ||
|
||
return if !defined?(AttrJson::Record) | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
# `Tapioca::Dsl::Compilers::AttrJson` decorates RBI files for classes that use the `AttrJson` gem. | ||
# https://github.com/jrochkind/attr_json | ||
# | ||
# For example, with the following ActiveRecord model: | ||
# ~~~rb | ||
# class Product < ActiveRecord::Base | ||
# include AttrJson::Record | ||
# | ||
# attr_json :price_cents, :integer | ||
# end | ||
# ~~~ | ||
# | ||
# This compiler will generate the following RBI: | ||
# ~~~rbi | ||
# class Product | ||
# include AttrJsonGeneratedMethods | ||
# extend AttrJson::Record::ClassMethods | ||
# | ||
# module AttrJsonGeneratedMethods | ||
# sig { returns(::Integer) } | ||
# def price_cents; end | ||
# | ||
# sig { params(value: Integer).returns(::Integer) } | ||
# def price_cents=(value); end | ||
# end | ||
# end | ||
# ~~~ | ||
class AttrJson < Tapioca::Dsl::Compiler | ||
extend T::Sig | ||
|
||
# Class methods module is already defined in the gem rbi, so just reference it here. | ||
ClassMethodsModuleName = "AttrJson::Record::ClassMethods" | ||
InstanceMethodModuleName = "AttrJsonGeneratedMethods" | ||
ConstantType = type_member {{ fixed: T.any(T.class_of(::AttrJson::Record), T.class_of(::AttrJson::Model)) }} | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
all_classes.select { |constant| constant < ::AttrJson::Record || constant < ::AttrJson::Model } | ||
end | ||
end | ||
|
||
sig { override.void } | ||
def decorate | ||
rbi_class = root.create_path(constant) | ||
instance_module = RBI::Module.new(InstanceMethodModuleName) | ||
|
||
decorate_attributes(instance_module) | ||
|
||
rbi_class << instance_module | ||
rbi_class.create_include(InstanceMethodModuleName) | ||
rbi_class.create_extend(ClassMethodsModuleName) if constant < ::AttrJson::Record | ||
end | ||
|
||
private | ||
|
||
def decorate_attributes(rbi_scope) | ||
T.unsafe(constant).attr_json_registry | ||
.definitions | ||
.sort_by(&:name) # this is annoying, but we need to sort to force consistent ordering or the rbi checks fail | ||
.each do |definition| | ||
_, type, options = definition.original_args | ||
attribute_name = definition.name | ||
type_name = sorbet_type(type, array: !!options[:array], nilable: !!options[:nil]) | ||
|
||
# Model: attr_json(:other_model_id, :string) | ||
# => other_model_id | ||
# => other_model_id= | ||
rbi_scope.create_method(attribute_name, return_type: type_name) | ||
rbi_scope.create_method( | ||
"#{attribute_name}=", | ||
parameters: [create_param("value", type: type_name)], | ||
return_type: type_name, | ||
) | ||
end | ||
end | ||
|
||
def symbol_type(type_name) | ||
return type_name if type_name.is_a?(Symbol) | ||
return type_name.to_sym if type_name.is_a?(String) | ||
|
||
type_name.type | ||
end | ||
|
||
def sorbet_type(type_name, array: false, nilable: false) | ||
sorbet_type = if type_name.respond_to?(:model) | ||
type_name.model | ||
else | ||
case symbol_type(type_name) | ||
when :string, :immutable_string, :text, :uuid, :binary | ||
"String" | ||
when :boolean | ||
"T::Boolean" | ||
when :integer, :big_integer | ||
"Integer" | ||
when :float | ||
"Float" | ||
when :decimal | ||
"BigDecimal" | ||
when :time, :datetime | ||
"Time" | ||
when :date | ||
"Date" | ||
when :money | ||
"Money" | ||
when :json | ||
"T.untyped" | ||
else | ||
"T.untyped" | ||
end | ||
end | ||
|
||
sorbet_type = "::#{sorbet_type}" | ||
sorbet_type = "T::Array[#{sorbet_type}]" if array | ||
sorbet_type = "T.nilable(#{sorbet_type})" if nilable # todo: improve this | ||
|
||
sorbet_type | ||
end | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(Paperclip) | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
# `Tapioca::Dsl::Compilers::Paperclip` decorates RBI files for classes that use the `has_attached_file` method | ||
# provided by the `paperclip` gem. | ||
# https://github.com/thoughtbot/paperclip | ||
# | ||
# For example, with the following ActiveRecord model: | ||
# ~~~rb | ||
# class Product < ActiveRecord::Base | ||
# has_attached_file(:marketing_image) | ||
# end | ||
# ~~~ | ||
# | ||
# This compiler will generate the following RBI: | ||
# ~~~rbi | ||
# class Product | ||
# include PaperclipGeneratedMethods | ||
# | ||
# module PaperclipGeneratedMethods | ||
# sig { returns(::Paperclip::Attachment) } | ||
# def marketing_image; end | ||
# | ||
# sig { params(value: T.untyped).void } | ||
# def marketing_image=(value); end | ||
# end | ||
# end | ||
# ~~~ | ||
class Paperclip < Tapioca::Dsl::Compiler | ||
extend T::Sig | ||
include RBIHelper | ||
|
||
InstanceModuleName = "PaperclipGeneratedMethods" | ||
ConstantType = type_member { { fixed: T.class_of(::Paperclip::Glue) } } | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
all_classes.select { |c| c < ::Paperclip::Glue } | ||
end | ||
end | ||
|
||
sig { override.void } | ||
def decorate | ||
attachments = ::Paperclip::AttachmentRegistry.names_for(constant) | ||
return if attachments.empty? | ||
|
||
root.create_path(constant) do |klass| | ||
instance_module = RBI::Module.new(InstanceModuleName) | ||
|
||
attachments.each do |attachment_name| | ||
# Model: has_attached_file(:marketing_image) | ||
# => marketing_image | ||
# => marketing_image= | ||
instance_module.create_method(attachment_name, return_type: "::Paperclip::Attachment") | ||
instance_module.create_method( | ||
"#{attachment_name}=", | ||
parameters: [create_param("value", type: "T.untyped")], | ||
return_type: nil, | ||
) | ||
end | ||
|
||
klass << instance_module | ||
klass.create_include(InstanceModuleName) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## AttrJson | ||
|
||
`Tapioca::Dsl::Compilers::AttrJson` decorates RBI files for classes that use the `AttrJson` gem. | ||
https://github.com/jrochkind/attr_json | ||
|
||
For example, with the following ActiveRecord model: | ||
~~~rb | ||
class Product < ActiveRecord::Base | ||
include AttrJson::Record | ||
|
||
attr_json :price_cents, :integer | ||
end | ||
~~~ | ||
|
||
This compiler will generate the following RBI: | ||
~~~rbi | ||
class Product | ||
include AttrJsonGeneratedMethods | ||
extend AttrJson::Record::ClassMethods | ||
|
||
module AttrJsonGeneratedMethods | ||
sig { returns(::Integer) } | ||
def price_cents; end | ||
|
||
sig { params(value: Integer).returns(::Integer) } | ||
def price_cents=(value); end | ||
end | ||
end | ||
~~~ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Paperclip | ||
|
||
`Tapioca::Dsl::Compilers::Paperclip` decorates RBI files for classes that use the `has_attached_file` method | ||
provided by the `paperclip` gem. | ||
https://github.com/thoughtbot/paperclip | ||
|
||
For example, with the following ActiveRecord model: | ||
~~~rb | ||
class Product < ActiveRecord::Base | ||
has_attached_file(:marketing_image) | ||
end | ||
~~~ | ||
|
||
This compiler will generate the following RBI: | ||
~~~rbi | ||
class Product | ||
include PaperclipGeneratedMethods | ||
|
||
module PaperclipGeneratedMethods | ||
sig { returns(::Paperclip::Attachment) } | ||
def marketing_image; end | ||
|
||
sig { params(value: T.untyped).void } | ||
def marketing_image=(value); end | ||
end | ||
end | ||
~~~ |
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