Skip to content

Commit

Permalink
Use concise syntax for methods where it makes sense
Browse files Browse the repository at this point in the history
I'm not a fan, it's just the way you're getting used to it :)
  • Loading branch information
flash-gordon committed Jan 6, 2025
1 parent 8d04fa2 commit 65e9ae0
Show file tree
Hide file tree
Showing 33 changed files with 145 additions and 381 deletions.
12 changes: 3 additions & 9 deletions lib/dry/types/any.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ module Types
#
# @api public
class AnyClass < Nominal
def self.name
"Any"
end
def self.name = "Any"

# @api private
def initialize(**options)
Expand All @@ -21,9 +19,7 @@ def initialize(**options)
# @return [String]
#
# @api public
def name
"Any"
end
def name = "Any"

# @param [Hash] new_options
#
Expand All @@ -37,9 +33,7 @@ def with(**new_options)
# @return [Array]
#
# @api public
def to_ast(meta: true)
[:any, meta ? self.meta : EMPTY_HASH]
end
def to_ast(meta: true) = [:any, meta ? self.meta : EMPTY_HASH]
end

Any = AnyClass.new
Expand Down
12 changes: 3 additions & 9 deletions lib/dry/types/array/constructor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@ class Array < Nominal
# @api private
class Constructor < ::Dry::Types::Constructor
# @api private
def constructor_type
::Dry::Types::Array::Constructor
end
def constructor_type = ::Dry::Types::Array::Constructor

# @return [Lax]
#
# @api public
def lax
Lax.new(type.lax.constructor(fn, meta: meta))
end
def lax = Lax.new(type.lax.constructor(fn, meta: meta))

# @see Dry::Types::Array#of
#
# @api public
def of(member)
type.of(member).constructor(fn, meta: meta)
end
def of(member) = type.of(member).constructor(fn, meta: meta)
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/dry/types/array/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def to_ast(meta: true)
end

# @api private
def constructor_type
::Dry::Types::Array::Constructor
end
def constructor_type = ::Dry::Types::Array::Constructor
end
end
end
Expand Down
30 changes: 7 additions & 23 deletions lib/dry/types/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ module Types
# Common API for building types and composition
#
# @api public
# rubocop:disable Metrics/ModuleLength
module Builder
include ::Dry::Core::Constants

# @return [Class]
#
# @api private
def constrained_type
Constrained
end
def constrained_type = Constrained

# @return [Class]
#
# @api private
def constructor_type
Constructor
end
def constructor_type = Constructor

# Compose two types into a Sum type
#
Expand All @@ -30,9 +25,7 @@ def constructor_type
# @return [Sum, Sum::Constrained]
#
# @api private
def |(other)
compose(other, Sum)
end
def |(other) = compose(other, Sum)

# Compose two types into an Intersection type
#
Expand All @@ -41,9 +34,7 @@ def |(other)
# @return [Intersection, Intersection::Constrained]
#
# @api private
def &(other)
compose(other, Intersection)
end
def &(other) = compose(other, Intersection)

# Compose two types into an Implication type
#
Expand All @@ -52,18 +43,14 @@ def &(other)
# @return [Implication, Implication::Constrained]
#
# @api private
def >(other)
compose(other, Implication)
end
def >(other) = compose(other, Implication)

# Turn a type into an optional type
#
# @return [Sum]
#
# @api public
def optional
Types["nil"] | self
end
def optional = Types["nil"] | self

# Turn a type into a constrained type
#
Expand Down Expand Up @@ -137,9 +124,7 @@ def enum(*values)
# @return [Lax]
#
# @api public
def lax
Lax.new(self)
end
def lax = Lax.new(self)

# Define a constructor for the type
#
Expand Down Expand Up @@ -216,6 +201,5 @@ def compose(other, composition_class)
klass.new(self, other)
end
end
# rubocop:enable Metrics/ModuleLength
end
end
20 changes: 5 additions & 15 deletions lib/dry/types/builder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ def included(base)
# @param [Dry::Types::Type] type
#
# @return [Dry::Types::Array]
def Array(type)
Strict(::Array).of(type)
end
def Array(type) = Strict(::Array).of(type)

# Build a hash schema
#
# @param [Hash{Symbol => Dry::Types::Type}] type_map
#
# @return [Dry::Types::Array]
def Hash(type_map)
Strict(::Hash).schema(type_map)
end
def Hash(type_map) = Strict(::Hash).schema(type_map)

# Build a type which values are instances of a given class
# Values are checked using `is_a?` call
Expand All @@ -47,9 +43,7 @@ def Hash(type_map)
# @param [Class,Module] klass Class or module
#
# @return [Dry::Types::Type]
def Instance(klass)
Nominal(klass).constrained(type: klass)
end
def Instance(klass) = Nominal(klass).constrained(type: klass)
alias_method :Strict, :Instance

# Build a type with a single value
Expand All @@ -58,19 +52,15 @@ def Instance(klass)
# @param [Object] value
#
# @return [Dry::Types::Type]
def Value(value)
Nominal(value.class).constrained(eql: value)
end
def Value(value) = Nominal(value.class).constrained(eql: value)

# Build a type with a single value
# The equality check done with `equal?`
#
# @param [Object] object
#
# @return [Dry::Types::Type]
def Constant(object)
Nominal(object.class).constrained(is: object)
end
def Constant(object) = Nominal(object.class).constrained(is: object)

# Build a constructor type
# If no constructor block given it uses .new method
Expand Down
4 changes: 1 addition & 3 deletions lib/dry/types/coercions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def to_symbol(input, &)
# @return [Boolean]
#
# @api private
def empty_str?(value)
EMPTY_STRING.eql?(value)
end
def empty_str?(value) = EMPTY_STRING.eql?(value)
end
end
end
4 changes: 1 addition & 3 deletions lib/dry/types/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def initialize(registry)
@registry = registry
end

def call(ast)
visit(ast)
end
def call(ast) = visit(ast)

def visit(node)
type, body = node
Expand Down
36 changes: 9 additions & 27 deletions lib/dry/types/composition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,55 +56,41 @@ def initialize(left, right, **options)
# @return [String]
#
# @api public
def name
[left, right].map(&:name).join(" #{self.class.operator} ")
end
def name = "#{left.name} #{self.class.operator} #{right.name}"

# @return [false]
#
# @api public
def default?
false
end
def default? = false

# @return [false]
#
# @api public
def constrained?
false
end
def constrained? = false

# @return [Boolean]
#
# @api public
def optional?
false
end
def optional? = false

# @param [Object] input
#
# @return [Object]
#
# @api private
def call_unsafe(input)
raise ::NotImplementedError
end
def call_unsafe(input) = raise ::NotImplementedError

# @param [Object] input
#
# @return [Object]
#
# @api private
def call_safe(input, &)
raise ::NotImplementedError
end
def call_safe(input, &) = raise ::NotImplementedError

# @param [Object] input
#
# @api public
def try(input)
raise ::NotImplementedError
end
def try(input) = raise ::NotImplementedError

# @api private
def success(input)
Expand All @@ -131,9 +117,7 @@ def failure(input, _error = nil)
# @return [Boolean]
#
# @api private
def primitive?(value)
raise ::NotImplementedError
end
def primitive?(value) = raise ::NotImplementedError

# @see Nominal#to_ast
#
Expand All @@ -148,9 +132,7 @@ def to_ast(meta: true)
# @return [Proc]
#
# @api public
def to_proc
proc { |value| self.(value) }
end
def to_proc = proc { |value| self.(value) }
end
end
end
20 changes: 5 additions & 15 deletions lib/dry/types/constrained.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,20 @@ def constrained(*nullary_rules, **unary_rules)
# @return [true]
#
# @api public
def constrained?
true
end
def constrained? = true

# @param [Object] value
#
# @return [Boolean]
#
# @api public
def ===(value)
valid?(value)
end
def ===(value) = valid?(value)

# Build lax type. Constraints are not applicable to lax types hence unwrapping
#
# @return [Lax]
# @api public
def lax
type.lax
end
def lax = type.lax

# @see Nominal#to_ast
# @api public
Expand All @@ -126,9 +120,7 @@ def to_ast(meta: true)
end

# @api private
def constructor_type
type.constructor_type
end
def constructor_type = type.constructor_type

private

Expand All @@ -137,9 +129,7 @@ def constructor_type
# @return [Boolean]
#
# @api private
def decorate?(response)
super || response.is_a?(Constructor)
end
def decorate?(response) = super || response.is_a?(Constructor)

# @param [Array] positional_args
#
Expand Down
Loading

0 comments on commit 65e9ae0

Please sign in to comment.