Skip to content

Commit

Permalink
Merge pull request #467 from m11o/feature/add-type-for-ordered-options
Browse files Browse the repository at this point in the history
activesupport: Add types for OrderedOptions and InheritableOptions
  • Loading branch information
pocke authored Jan 25, 2024
2 parents bdabd7f + 8e8346a commit e9d3173
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 104 deletions.
2 changes: 1 addition & 1 deletion gems/actionview/6.0/actionview-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module ActionView

attr_reader lookup_context: untyped

attr_accessor config(@_config): untyped
attr_accessor config(@_config): ActiveSupport::InheritableOptions

attr_accessor assigns(@_assigns): untyped

Expand Down
97 changes: 1 addition & 96 deletions gems/activesupport/6.0/activesupport-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,48 +1846,6 @@ module ActiveSupport
end
end

module ActiveSupport
# Configurable provides a <tt>config</tt> method to store and retrieve
# configuration options as an <tt>OrderedHash</tt>.
module Configurable
extend ActiveSupport::Concern

class Configuration[T, U] < ActiveSupport::InheritableOptions[T, U]
def compile_methods!: () -> untyped

# Compiles reader methods so we don't have to go through method_missing.
def self.compile_methods!: (untyped keys) -> untyped
end

module ClassMethods
def config: () -> untyped

def configure: () { (untyped) -> untyped } -> untyped

private

def config_accessor: (*untyped names, ?instance_accessor: bool instance_accessor, ?instance_writer: bool instance_writer, ?instance_reader: bool instance_reader) { () -> untyped } -> untyped
end

# Reads and writes attributes from a configuration <tt>OrderedHash</tt>.
#
# require 'active_support/configurable'
#
# class User
# include ActiveSupport::Configurable
# end
#
# user = User.new
#
# user.config.allowed_access = true
# user.config.level = 1
#
# user.config.allowed_access # => true
# user.config.level # => 1
def config: () -> untyped
end
end

class Array[unchecked out Elem]
# Returns the tail of the array from +position+.
#
Expand Down Expand Up @@ -7354,7 +7312,7 @@ module ActiveSupport

private

def options: () -> untyped
def options: () -> InheritableOptions

def deserialize: (untyped config) -> untyped
end
Expand Down Expand Up @@ -10540,59 +10498,6 @@ module ActiveSupport
end
end

module ActiveSupport
# Usually key value pairs are handled something like this:
#
# h = {}
# h[:boy] = 'John'
# h[:girl] = 'Mary'
# h[:boy] # => 'John'
# h[:girl] # => 'Mary'
# h[:dog] # => nil
#
# Using +OrderedOptions+, the above code could be reduced to:
#
# h = ActiveSupport::OrderedOptions.new
# h.boy = 'John'
# h.girl = 'Mary'
# h.boy # => 'John'
# h.girl # => 'Mary'
# h.dog # => nil
#
# To raise an exception when the value is blank, append a
# bang to the key name, like:
#
# h.dog! # => raises KeyError: :dog is blank
#
class OrderedOptions[T, U] < Hash[T, U]
alias _get []

def []=: (untyped key, untyped value) -> untyped

def []: (untyped key) -> untyped

def method_missing: (untyped name, *untyped args) -> untyped

def respond_to_missing?: (untyped name, untyped include_private) -> ::TrueClass

def extractable_options?: () -> ::TrueClass
end

# +InheritableOptions+ provides a constructor to build an +OrderedOptions+
# hash inherited from another hash.
#
# Use this if you already have some hash and you want to create a new one based on it.
#
# h = ActiveSupport::InheritableOptions.new({ girl: 'Mary', boy: 'John' })
# h.girl # => 'Mary'
# h.boy # => 'John'
class InheritableOptions[T, U] < OrderedOptions[T, U]
def initialize: (?untyped? parent) -> untyped

def inheritable_copy: () -> untyped
end
end

module ActiveSupport
# +ParameterFilter+ allows you to specify keys for sensitive data from
# hash-like object and replace corresponding value. Filtering only certain
Expand Down
45 changes: 42 additions & 3 deletions gems/activesupport/6.0/activesupport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@ module ActiveSupport
end

module Configurable
extend Concern

class Configuration < InheritableOptions
def compile_methods!: () -> void

# Compiles reader methods so we don't have to go through method_missing.
def self.compile_methods!: (Array[Symbol | String] keys) -> void
end

module ClassMethods
# Manual definition to make block optional
def config_accessor: (*untyped names, ?instance_accessor: bool instance_accessor, ?instance_writer: bool instance_writer, ?instance_reader: bool instance_reader) ?{ () -> untyped } -> untyped
| ...
def config: () -> InheritableOptions

def configure: () { (InheritableOptions config) -> void } -> void

private

# TODO: Fix to use `interned` in names params after releasing ruby/rbs#1499
def config_accessor: (*(String | Symbol) names, ?instance_accessor: bool instance_accessor, ?instance_writer: bool instance_writer, ?instance_reader: bool instance_reader) ?{ () -> untyped } -> void
end

def config: () -> InheritableOptions
end

class Deprecation
Expand Down Expand Up @@ -239,6 +255,29 @@ module ActiveSupport
def tv_usec: () -> Integer
def wednesday?: () -> bool
end

class OrderedOptions < Hash[Symbol, untyped]
alias _get []

# TODO: Fix to use `interned` in key params after releasing ruby/rbs#1499
def []=: ((String | Symbol) key, untyped value) -> untyped

# TODO: Fix to use `interned` in key params after releasing ruby/rbs#1499
def []: ((String | Symbol) key) -> untyped

def method_missing: (Symbol name, *untyped args) -> untyped

def respond_to_missing?: (Symbol name, ?bool include_private) -> true

def extractable_options?: () -> true
end

class InheritableOptions < OrderedOptions
# TODO: Fix to use `interned` in parent params after releasing ruby/rbs#1499
def initialize: (?Hash[(String | Symbol), untyped]? parent) -> void

def inheritable_copy: () -> instance
end
end

class Hash[unchecked out K, unchecked out V]
Expand Down
8 changes: 4 additions & 4 deletions gems/railties/6.0/railties-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ module Rails

attr_accessor ssl_options: untyped

attr_accessor public_file_server: untyped
attr_accessor public_file_server: ActiveSupport::OrderedOptions

attr_accessor session_options: untyped

Expand Down Expand Up @@ -194,7 +194,7 @@ module Rails

attr_accessor require_master_key: untyped

attr_accessor credentials: untyped
attr_accessor credentials: ActiveSupport::OrderedOptions

attr_accessor disable_sandbox: untyped

Expand Down Expand Up @@ -551,9 +551,9 @@ module Rails
#
# +Rails.application.secrets.namespace+ returns +my_app_production+ in the
# production environment.
def secrets: () -> untyped
def secrets: () -> ActiveSupport::OrderedOptions

attr_writer secrets: untyped
attr_writer secrets: ActiveSupport::OrderedOptions

# The secret_key_base is used as the input secret to the application's key generator, which in turn
# is used to create all MessageVerifiers/MessageEncryptors, including the ones that sign and encrypt cookies.
Expand Down

0 comments on commit e9d3173

Please sign in to comment.