Sha256: aa2371fad3cb5e5a879b1b23b6d6101dbbc5bd11ef93a00a0fae7a572beb470d

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Transcriber
  class Resource
    class Key
      attr_accessor :name
      attr_accessor :model
      attr_accessor :options
      attr_accessor :summarize

      def initialize(name, options = {})
        @name    = name
        @options = options
      end

      def present?(resource)
        return true unless options[:if]
        resource.instance_eval &options[:if]
      end

      def visible?(resource)
        if present? resource
          return true if options[:visible].nil?
          return options[:visible] unless options[:visible].kind_of?(Proc)
          return resource.instance_eval &options[:visible]
        end
        false
      end

      def input_path
        return @input_path if @input_path

        path = InputPath.resolve(options, convert_input_keys)

        if path.empty? and !root_path?
          path = convert_input_keys.call([name])
        end

        @input_path = path
      end

      def root_path?
        path_definition = options.slice(:start_key, :field)
        path_definition.any? and path_definition.first[1].empty?
      end

      def convert_input_keys
        options.fetch(:convert_input_keys, model.try(:convert_input_keys) ||
                      Transcriber.configuration.convert_input_keys)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
transcriber-0.0.10 lib/transcriber/resource/key.rb
transcriber-0.0.9 lib/transcriber/resource/key.rb
transcriber-0.0.8 lib/transcriber/resource/key.rb