Sha256: 808df98b7e745d13952c4c41fbf85cfb2c62ac664eed251e22eec5243d042f58

Contents?: true

Size: 950 Bytes

Versions: 6

Compression:

Stored size: 950 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
        fields[key.to_sym] || fields[key.pluralize.to_sym]
      end

    private

      ActiveModelSerializers.silence_warnings do
      attr_reader :raw_fields, :root
      end

      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

6 entries across 6 versions & 1 rubygems

Version Path
cheap_ams-0.10.5 lib/active_model/serializer/fieldset.rb
cheap_ams-0.10.4 lib/active_model/serializer/fieldset.rb
cheap_ams-0.10.3 lib/active_model/serializer/fieldset.rb
cheap_ams-0.10.2 lib/active_model/serializer/fieldset.rb
cheap_ams-0.10.1 lib/active_model/serializer/fieldset.rb
cheap_ams-0.10.0.rc2 lib/active_model/serializer/fieldset.rb