Sha256: 56734dcabab904cd27a0e848d899d75161c68dd45b022486c3b23a09122908b0

Contents?: true

Size: 1.19 KB

Versions: 13

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true
# author: Vadim Shaveiko <@vshaveyko>
# :nodoc:
class RailsApiDoc::Controller::AttributeParser

  # TODO : Change to I18n. Added on: 08.10.16. Added by: <@vshaveyko>
  WRONG_NAME_ERROR_STRING = 'Name should consist only of letters\ciphers\underscores'

  class << self

    def parse_attributes(params)
      type = :enum if params[:enum].present?

      {
        name: parse_name(params[:name]),
        type: type || parse_type(params[:type]),
        enum: parse_enum(params[:enum])
      }.compact
    end

    private

    def parse_name(name_string)
      return if name_string.blank?
      raise ArgumentError, WRONG_NAME_ERROR_STRING unless name_string =~ /[A-z0-9_]*/
      name_string.underscore.to_sym
    end

    def parse_enum(enum_string)
      return if enum_string.blank?
      enum_string.split(',').map do |enum_value|
        parse_enum_value(enum_value)
      end
    end

    def parse_enum_value(value)
      case value
      when /^\d+$/
        value.to_i
      when 'true'
        true
      when 'false'
        false
      else
        value
      end
    end

    def parse_type(type)
      return if type.blank?
      type.constantize
    end

  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rails_api_documentation-0.2.2 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.2.1 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.2.0 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.9 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.8 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.7 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.6 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.5 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.4 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.3 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.2 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.1 lib/rails_api_doc/controller/attribute_parser.rb
rails_api_documentation-0.1.0 lib/rails_api_doc/controller/attribute_parser.rb