Sha256: 49eac599787e1d1f49ccf6f0de0ad1ddfa830f5d59a2ff7b0715b983f23fc0cf

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

module {{namespace_module}}
  module {{resource_module}}
    module List
      class Normalizer
        include Shogun::Normalizer
        include {{resource_module}}

        METADATA = %w|limit filters includes fields|
        LIST_DELIMITER = /\s*,\s*/
        DEFAULT_FIELD = "id"
        AUTOMATIC_FIELDS = %w|id href meta links linked|

        def initialize(raw:)
          @limit = raw["limit"]
          @filters = raw["filter"]
          @includes = raw["include"]
          @fields = raw["fields"]
        end

        def limit
          Integer(@limit) rescue 10
        end

        def filters
          if @filters.is_a?(Hash)
            @filters
          else
            Hash.new
          end
        end

        def includes
          case @includes
          when String
            @includes.split(LIST_DELIMITER)
          when Array
            @includes
          else
            Array.new
          end
        end

        def fields
          case
          when @fields.is_a?(Array)
            @fields + AUTOMATIC_FIELDS
          when @fields.is_a?(String) && @fields.match(LIST_DELIMITER)
            @fields.split(LIST_DELIMITER) + AUTOMATIC_FIELDS
          when @fields.is_a?(String)
            [@fields.strip] + AUTOMATIC_FIELDS
          else
            [DEFAULT_FIELD]
          end.uniq
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
shogun-6.4.2 scaffold/generate/resource/list/normalizer.rb
shogun-6.4.1 scaffold/generate/resource/list/normalizer.rb
shogun-6.4.0 scaffold/generate/resource/list/normalizer.rb
shogun-6.3.0 scaffold/generate/resource/list/normalizer.rb
shogun-6.2.3 scaffold/generate/resource/list/normalizer.rb
shogun-6.2.2 scaffold/generate/resource/list/normalizer.rb