lib/alba.rb in alba-3.4.0 vs lib/alba.rb in alba-3.5.0

- old
+ new

@@ -115,10 +115,11 @@ # @param inflector [Symbol, Class, Module] inflector # When it's a Symbol, it accepts `:default`, `:active_support` or `:dry` # When it's a Class or a Module, it should have some methods, see {Alba::DefaultInflector} def inflector=(inflector) @inflector = inflector_from(inflector) + reset_transform_keys end # @param block [Block] resource body # @return [Class<Alba::Resource>] resource class def resource_class(&block) @@ -142,15 +143,17 @@ end end # Configure Alba to symbolize keys def symbolize_keys! + reset_transform_keys unless @symbolize_keys @symbolize_keys = true end # Configure Alba to stringify (not symbolize) keys def stringify_keys! + reset_transform_keys if @symbolize_keys @symbolize_keys = false end # Regularize key to be either Symbol or String depending on @symbolize_keys # Returns nil if key is nil @@ -166,23 +169,26 @@ # Transform a key with given transform_type # # @param key [String] a target key # @param transform_type [Symbol] a transform type, either one of `camel`, `lower_camel`, `dash` or `snake` # @return [String] - def transform_key(key, transform_type:) + def transform_key(key, transform_type:) # rubocop:disable Metrics/MethodLength raise Alba::Error, 'Inflector is nil. You must set inflector before transforming keys.' unless inflector - key = key.to_s + @_transformed_keys[transform_type][key] ||= begin + key = key.to_s - k = case transform_type - when :camel then inflector.camelize(key) - when :lower_camel then inflector.camelize_lower(key) - when :dash then inflector.dasherize(key) - when :snake then inflector.underscore(key) - else raise Alba::Error, "Unknown transform type: #{transform_type}" - end - regularize_key(k) + k = case transform_type + when :camel then inflector.camelize(key) + when :lower_camel then inflector.camelize_lower(key) + when :dash then inflector.dasherize(key) + when :snake then inflector.underscore(key) + else raise Alba::Error, "Unknown transform type: #{transform_type}" + end + + regularize_key(k) + end end # Register types, used for both builtin and custom types # # @see Alba::Type @@ -206,10 +212,11 @@ @encoder = default_encoder @symbolize_keys = false @_on_error = :raise @_on_nil = nil @types = {} + reset_transform_keys register_default_types end # Get a resource object from arguments # If block is given, it creates a resource class with the block @@ -300,10 +307,14 @@ end inflector end + def reset_transform_keys + @_transformed_keys = Hash.new { |h, k| h[k] = {} } + end + def register_default_types # rubocop:disable Metrics/AbcSize [String, :String].each do |t| register_type(t, check: ->(obj) { obj.is_a?(String) }, converter: lambda(&:to_s)) end [Integer, :Integer].each do |t| @@ -316,5 +327,9 @@ end end reset! end + +# Monkey patch for TruffleRuby +# Delete this after 24.1.2 is released +File::SHARE_DELETE = 0 if defined?(Truffle)