Sha256: fbe7a4c094f1ec3cff63804054f5e92c1e7c71f80bf4085d8c03a18fdf504bf1

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Sparrow
  module Strategies
    ##
    # Superclass for all JSON format strategies.
    # Contains no own instance logic, but keeps track of the registration
    # of all JSON format strategies with its Singleton class methods.
    # @abstract Not exactly a abstract class but contains no own logic but
    #   singleton class methods
    class JsonFormatStrategy
      ##
      # Empty constructor. Does nothing.
      def initialize(*args)
      end

      ##
      # Register a new JSON Format strategy
      # @param [Object] args the arguments for the new strategy
      # @return [Array] args the updated registered JSON Format strategies
      #   available
      def self.register_json_format(*args)
        init(args)
        @@json_format_strategies << self.new(args)
      end

      ##
      # Start a JSON conversion by its given string
      # @param [Object] body a JSON object representation.
      #  can be any type a JSON format strategy is registered,
      #  i.e. an Array, a String or a RackBody
      # @return [String] the formatted JSON
      def self.convert(body)
        strategy = json_format_strategies.detect do |strategy|
          strategy.match?(body)
        end
        strategy.convert(body)
      end

      private
      def self.init(*args)
        @@json_format_strategies ||= Array.new(args)
      end

      def self.json_format_strategies
        init
        default = Sparrow::Strategies::DefaultJsonFormatStrategy.instance
        @@json_format_strategies.reject(&:blank?) + [default]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cp-sparrow-0.0.14 lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb