Skip to content

Commit

Permalink
Update syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 6, 2025
1 parent 580b6e2 commit 95e3395
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 53 deletions.
4 changes: 1 addition & 3 deletions lib/dry/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ def inspect
# Pattern matching support
#
# @api private
def deconstruct_keys(_keys)
attributes
end
def deconstruct_keys(_keys) = attributes
end
end

Expand Down
41 changes: 10 additions & 31 deletions lib/dry/struct/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,48 +313,32 @@ def try_struct(input)

# @param [({Symbol => Object})] args
# @return [Dry::Types::Result::Success]
def success(*args)
result(Types::Result::Success, *args)
end
def success(*args) = result(Types::Result::Success, *args)

# @param [({Symbol => Object})] args
# @return [Dry::Types::Result::Failure]
def failure(*args)
result(Types::Result::Failure, *args)
end
def failure(*args) = result(Types::Result::Failure, *args)

# @param [Class] klass
# @param [({Symbol => Object})] args
def result(klass, *args)
klass.new(*args)
end
def result(klass, *args) = klass.new(*args)

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

# @param [Object, Dry::Struct] other
# @return [Boolean]
def ===(other)
other.is_a?(self)
end
def ===(other) = other.is_a?(self)
alias_method :primitive?, :===

# @return [true]
def constrained?
true
end
def constrained? = true

# @return [self]
def primitive
self
end
def primitive = self

# @return [false]
def optional?
false
end
def optional? = false

# @return [Proc]
def to_proc
Expand All @@ -365,9 +349,7 @@ def to_proc
#
# @param [Symbol] key Attribute name
# @return [Boolean]
def has_attribute?(key)
schema.key?(key)
end
def has_attribute?(key) = schema.key?(key)

# Gets the list of attribute names
#
Expand Down Expand Up @@ -483,10 +465,7 @@ def #{key} # def email
private :define_accessors

# @api private
def valid_method_name?(key)
key.to_s.match?(/\A[a-zA-Z_]\w*\z/)
end
private :valid_method_name?
private def valid_method_name?(key) = key.to_s.match?(/\A[a-zA-Z_]\w*\z/)
end
end
end
2 changes: 1 addition & 1 deletion lib/dry/struct/extensions/pretty_print.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "pp" # rubocop:disable Lint/RedundantRequireStatement
require "pp"

module Dry
class Struct
Expand Down
6 changes: 3 additions & 3 deletions lib/dry/struct/hashify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ module Hashify
# @return [Hash, Array]
def self.[](value)
if value.is_a?(Struct)
value.to_h.transform_values { |current| self[current] }
value.to_h.transform_values { self[_1] }
elsif value.respond_to?(:to_hash)
value.to_hash.transform_values { |current| self[current] }
value.to_hash.transform_values { self[_1] }
elsif value.respond_to?(:to_ary)
value.to_ary.map { |item| self[item] }
value.to_ary.map { self[_1] }
else
value
end
Expand Down
12 changes: 3 additions & 9 deletions lib/dry/struct/struct_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@ def call(attr_name, type, &)

private

def type?(type)
type.is_a?(Types::Type)
end
def type?(type) = type.is_a?(Types::Type)

def array?(type)
type?(type) && !type.optional? && type.primitive.equal?(::Array)
end

def optional?(type)
type?(type) && type.optional?
end
def optional?(type) = type?(type) && type.optional?

def parent(type)
if array?(type)
Expand Down Expand Up @@ -95,9 +91,7 @@ def visit_array(node)
visit(member)
end

def visit_nominal(*)
Undefined
end
def visit_nominal(*) = Undefined

def visit_constructor(node)
definition, * = node
Expand Down
4 changes: 1 addition & 3 deletions lib/dry/struct/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def |(type)
end

# @return [boolean]
def ===(value)
left === value || right === value
end
def ===(value) = left === value || right === value

protected

Expand Down
4 changes: 1 addition & 3 deletions lib/dry/struct/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class Value < self
# @param (see ClassInterface#new)
# @return [Value]
# @see https://github.com/dkubb/ice_nine
def self.new(*)
::IceNine.deep_freeze(super)
end
def self.new(*) = ::IceNine.deep_freeze(super)
end

deprecate_constant :Value
Expand Down

0 comments on commit 95e3395

Please sign in to comment.