Sha256: 94633ed95f7020bfc7755ee092d6aed8e93dcbfc82701cb44581ed06875d991a

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module CsvRowModel
  module Import
    class Representation
      include Concerns::CheckOptions
      VALID_OPTIONS = %i[memoize empty_value dependencies].freeze

      attr_reader :name, :options, :row_model

      def initialize(name, options, row_model)
        @name = name
        @options = options
        @row_model = row_model
      end

      def value
        memoize? ? memoized_value : dependencies_value
      end

      def memoized_value
        @memoized_value ||= dependencies_value
      end

      def memoize?
        options.has_key?(:memoize) ? !!options[:memoize] : true
      end

      def dependencies_value
        valid_dependencies? ? lambda_value : empty_value
      end

      # @return [Boolean] if the dependencies are valid
      def valid_dependencies?
        dependencies.each { |attribute_name| return false if row_model.public_send(attribute_name).blank? }
        true
      end

      def empty_value
        options[:empty_value]
      end

      def lambda_value
        row_model.public_send(self.class.lambda_name(name))
      end

      def dependencies
        Array(options[:dependencies])
      end

      class << self
        def lambda_name(representation_name)
          :"__#{representation_name}"
        end

        def define_lambda_method(row_model_class, representation_name, &block)
          row_model_class.send(:define_method, lambda_name(representation_name), &block)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_row_model-1.0.0.beta1 lib/csv_row_model/import/representation.rb