Sha256: e4bd6dab410597e95ff13577443d9ec3ddb87c99f11a0d0b9d4ecab9e9ca0f56

Contents?: true

Size: 980 Bytes

Versions: 12

Compression:

Stored size: 980 Bytes

Contents

# frozen_string_literal: true
module Eac
  module DataTableHelper
    class Column
      EMPTY_VALUE = '-'

      attr_reader :label

      def initialize(label, path, block)
        @label = label
        @path = path.to_s.split('.')
        @block = block
      end

      def record_value(r)
        v = Node.new(r, @path).value
        if v.present?
          @block ? @block.call(v) : v
        else
          EMPTY_VALUE
        end
      end

      private

      def node_value(node, subpath)
        return node if subpath.empty?
      end

      class Node
        def initialize(node, path)
          @node = node
          @path = path
        end

        def value
          return @node if @node.nil? || @path.empty?
          subpath = @path.dup
          n = subpath.shift
          return Node.new(@node.send(n), subpath).value if @node.respond_to?(n)
          raise "Instance of #{@node.class} does not respond to #{n}"
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
eac_rails_utils-0.8.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.7.1 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.7.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.6.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.5.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.4.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.3.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.2.2 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.2.1 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.2.0 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.1.15 lib/eac/data_table_helper/column.rb
eac_rails_utils-0.1.14 lib/eac/data_table_helper/column.rb