Sha256: 34e7257e66ef2e9d5a82cbbf2f18bed000d7f2646eacfd74c404712ddc5e98cc

Contents?: true

Size: 1.52 KB

Versions: 24

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module TailwindDsl
  module Etl
    # Base class for all ETL data objects that load/persist JSON data
    class Element
      # Get the value of an argument from a hash
      #
      # @param args [Hash] The hash of arguments
      # @param key [String, Symbol] The key to look up
      # @param guard [String] The error message to raise if the key is not found. Guards are optional.
      # @param default [Object] The default value to return if the key is not found. Also optional, defaults to nil.
      def grab_arg(args, key, guard: nil, default: nil)
        value = args[key.to_sym] || args[key.to_s]

        raise guard if value.nil? && guard

        value || default
      end

      # Map data to a class
      #
      # @param klass [Class] The class to map to
      # @param data [Hash, Class] The data to map can be hash, an instance of the class or nil
      def map_to(klass, data)
        return nil if data.nil?

        return data if data.is_a?(klass)
        return klass.new(**data) if data.is_a?(Hash)

        puts "Data of type: #{data.class} cannot be converted to #{klass}"
        nil
      end

      # Add data onto an array
      #
      # @param target_list [Array] The array to add to
      # @param data [Hash, Class] The data to add can be hash or an instance of the class. Nil data will not be added
      def add_to_list(klass, target_list, data)
        item = map_to(klass, data)

        return nil if item.nil?

        target_list << item

        item
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
tailwind_dsl-0.0.32 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.31 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.30 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.29 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.28 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.27 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.26 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.25 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.24 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.23 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.22 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.21 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.20 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.19 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.18 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.17 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.16 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.15 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.14 lib/tailwind_dsl/etl/element.rb
tailwind_dsl-0.0.13 lib/tailwind_dsl/etl/element.rb