lib/fear/struct.rb in fear-1.2.0 vs lib/fear/struct.rb in fear-2.0.0

- old
+ new

@@ -1,7 +1,9 @@ # frozen_string_literal: true +require "fear/pattern_match" + module Fear # Structs are like regular classes and good for modeling immutable data. # # A minimal struct requires just a list of attributes: # @@ -34,33 +36,20 @@ # admin_john = john.copy(admin: true) # # john.admin #=> false # admin_john.admin #=> true # - # It's possible to match against struct attributes. The following example extracts email from - # user only if user is admin - # - # john = User.new(id: 2, email: 'john@example.com', admin: false) - # john.match |m| - # m.xcase('Fear::Struct(_, email, true)') do |email| - # email - # end - # end - # - # Note, parameters got extracted in order they was defined. - # class Struct include PatternMatch.mixin @attributes = [].freeze class << self # @param base [Fear::Struct] # @api private def inherited(base) base.instance_variable_set(:@attributes, attributes) - Fear.register_extractor(base, Fear.case(base, &:to_a).lift) end # Defines attribute # # @param name [Symbol] @@ -130,10 +119,10 @@ # john.admin #=> false # admin_john = john.copy(admin: true) # admin_john.admin #=> true # def copy(**attributes) - self.class.new(to_h.merge(attributes)) + self.class.new(**to_h.merge(attributes)) end # Returns the struct attributes as an array of symbols # @return [<Symbol>] #