Sha256: effc9b58e117c8bb6b347389bc4f3d0f2f40679906ab1206bad9f15872a19591

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require "key_mapable/version"
require "key_mapable/mapper"

# Used to map the instance of the extended class to a new format.
#
# Usage:
#
#   class MyClass
#     extend KeyMapable
#
#     # Define a method called `#to_h` that use the provided map. The keys
#     # will be accessed on the provided subject.
#     define_map(:to_h, subject: :my_reader) do
#       # Map the value of `#name` to the key 'Name'.
#       key_map(:name, 'Name')
#
#       # Map the value of `#maybe_value` to the key 'GuaranteedValue'.
#       # Transform the value by calling `#to_s` first.
#       key_map(:maybe_value, 'GuaranteedValue', &:to_s)
#
#       # Map the key 'Name' to the value provided by the block.
#       key_value('AConstant') { 'Foo' }
#
#       # Map every item returned from `#rows`.
#       array_key_map(:rows, 'Rows') do
#         # Map the value of `#id` to the key 'Id'.
#         key_map(:id, 'Id')
#       end
#     end
#   end
module KeyMapable
  def define_map(method_name, subject: :self, &block)
    define_method(method_name) do
      value = public_send(subject)
      mapper = Mapper.new(value)
      mapper.instance_eval(&block)
      mapper.resolve
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
key_mapable-0.1.0 lib/key_mapable.rb