Sha256: 379a19f3660244aa26b4b461702a63bee5c2a8b3add98f89d91896b6e7f31485

Contents?: true

Size: 888 Bytes

Versions: 2

Compression:

Stored size: 888 Bytes

Contents

module ActiveModel
  class Serializer
    class Fieldset

      def initialize(fields, root = nil)
        @root       = root
        @raw_fields = fields
      end

      def fields
        @fields ||= parsed_fields
      end

      def fields_for(serializer)
        key = serializer.json_key || serializer.class.root_name
        fields[key.to_sym]
      end

    private

      attr_reader :raw_fields, :root

      def parsed_fields
        if raw_fields.is_a?(Hash)
          raw_fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h}
        elsif raw_fields.is_a?(Array)
          if root.nil?
            raise ArgumentError, 'The root argument must be specified if the fileds argument is an array.'
          end
          hash = {}
          hash[root.to_sym] = raw_fields.map(&:to_sym)
          hash
        else
          {}
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
cm-active_model_serializers-0.10.0.rc1.1 lib/active_model/serializer/fieldset.rb
active_model_serializers-0.10.0.rc1 lib/active_model/serializer/fieldset.rb