class Tapioca::Compilers::Dsl::ActiveResource

`Tapioca::Compilers::Dsl::ActiveResource` decorates RBI files for subclasses of [`ActiveResource::Base`](github.com/rails/activeresource) which declare `schema` fields.

For example, with the following `ActiveResource::Base` subclass:

~~~rb class Post < ActiveResource::Base

schema do
  integer 'id', 'month', 'year'
end

end ~~~

this generator will produce the RBI file `post.rbi` with the following content:

~~~rbi # post.rbi # typed: true class Post

sig { returns(Integer) }
def id; end

sig { params(id: Integer).returns(Integer) }
def id=(id); end

sig { returns(T::Boolean) }
def id?; end

sig { returns(Integer) }
def month; end

sig { params(month: Integer).returns(Integer) }
def month=(month); end

sig { returns(T::Boolean) }
def month?; end

sig { returns(Integer) }
def year; end

sig { params(year: Integer).returns(Integer) }
def year=(year); end

sig { returns(T::Boolean) }
def year?; end

end ~~~

Constants

TYPES

Public Instance Methods

decorate(root, constant) click to toggle source
# File lib/tapioca/compilers/dsl/active_resource.rb, line 65
def decorate(root, constant)
  return if constant.schema.blank?

  root.create_path(constant) do |resource|
    constant.schema.each do |attribute, type|
      create_schema_methods(resource, attribute, type)
    end
  end
end
gather_constants() click to toggle source
# File lib/tapioca/compilers/dsl/active_resource.rb, line 76
def gather_constants
  descendants_of(::ActiveResource::Base)
end

Private Instance Methods

create_schema_methods(klass, attribute, type) click to toggle source
# File lib/tapioca/compilers/dsl/active_resource.rb, line 101
def create_schema_methods(klass, attribute, type)
  return_type = type_for(type.to_sym)

  klass.create_method(attribute, return_type: return_type)
  klass.create_method("#{attribute}?", return_type: "T::Boolean")
  klass.create_method("#{attribute}=", parameters: [
    create_param("value", type: return_type),
  ], return_type: return_type)
end
type_for(attr_type) click to toggle source
# File lib/tapioca/compilers/dsl/active_resource.rb, line 96
def type_for(attr_type)
  TYPES.fetch(attr_type, "T.untyped")
end