Sha256: b9e36a9ca64f4b2a54489249b3f032962b934700cc4dadf55d9bb5948befbd0a

Contents?: true

Size: 1.56 KB

Versions: 13

Compression:

Stored size: 1.56 KB

Contents

module OasRails
  module Spec
    module Specable
      def oas_fields
        []
      end

      def to_spec
        hash = {}
        oas_fields.each do |var|
          key = var.to_s

          camel_case_key = key.camelize(:lower).to_sym
          value = send(var)

          processed_value = if value.respond_to?(:to_spec)
                              value.to_spec
                            elsif value.is_a?(Array) && value.all? { |elem| elem.respond_to?(:to_spec) }
                              value.map(&:to_spec)
                            # elsif value.is_a?(Hash)
                            #   hash = {}
                            #   value.each do |key, object|
                            #     hash[key] = object.to_spec
                            #   end
                            #   hash
                            else
                              value
                            end

          hash[camel_case_key] = processed_value unless valid_processed_value?(processed_value)
        end
        hash
      end

      # rubocop:disable Lint/UnusedMethodArgument
      def as_json(options = nil)
        to_spec
      end
      # rubocop:enable Lint/UnusedMethodArgument

      private

      def valid_processed_value?(processed_value)
        ((processed_value.is_a?(Hash) || processed_value.is_a?(Array)) && processed_value.empty?) || processed_value.nil?
      end

      def snake_to_camel(snake_str)
        words = snake_str.to_s.split('_')
        words[1..].map!(&:capitalize)
        (words[0] + words[1..].join).to_sym
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
oas_rails-0.9.0 lib/oas_rails/spec/specable.rb
oas_rails-0.8.4 lib/oas_rails/spec/specable.rb
oas_rails-0.8.3 lib/oas_rails/spec/specable.rb
oas_rails-0.8.2 lib/oas_rails/spec/specable.rb
oas_rails-0.8.1 lib/oas_rails/spec/specable.rb
oas_rails-0.8.0 lib/oas_rails/spec/specable.rb
oas_rails-0.7.0 lib/oas_rails/spec/specable.rb
oas_rails-0.6.0 lib/oas_rails/spec/specable.rb
oas_rails-0.5.0 lib/oas_rails/spec/specable.rb
oas_rails-0.4.5 lib/oas_rails/spec/specable.rb
oas_rails-0.4.4 lib/oas_rails/spec/specable.rb
oas_rails-0.4.3 lib/oas_rails/spec/specable.rb
oas_rails-0.4.2 lib/oas_rails/spec/specable.rb