File: source.rb

package info (click to toggle)
ruby-faker 2.21.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,076 kB
  • sloc: ruby: 19,088; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 2,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

module Faker
  class Source < Base
    class << self
      ##
      # Produces source code for Hello World in a given language.
      #
      # @param lang [Symbol] The programming language to use
      # @return [String]
      #
      # @example
      #   Faker::Source.hello_world #=> "puts 'Hello World!'"
      #
      # @example
      #   Faker::Source.hello_world(lang: :javascript)
      #     #=> "alert('Hello World!');"
      #
      # @faker.version 1.9.0
      def hello_world(legacy_lang = NOT_GIVEN, lang: :ruby)
        warn_for_deprecated_arguments do |keywords|
          keywords << :lang if legacy_lang != NOT_GIVEN
        end

        fetch("source.hello_world.#{lang}")
      end

      ##
      # Produces source code for printing a string in a given language.
      #
      # @param str [String] The string to print
      # @param lang [Symbol] The programming language to use
      # @return [String]
      #
      # @example
      #   Faker::Source.print #=> "puts 'faker_string_to_print'"
      # @example
      #   Faker::Source.print(str: 'foo bar', lang: :javascript)
      #     #=> "console.log('foo bar');"
      #
      # @faker.version 1.9.0
      def print(legacy_str = NOT_GIVEN, legacy_lang = NOT_GIVEN, str: 'some string', lang: :ruby)
        warn_for_deprecated_arguments do |keywords|
          keywords << :str if legacy_str != NOT_GIVEN
        end
        warn_for_deprecated_arguments do |keywords|
          keywords << :lang if legacy_lang != NOT_GIVEN
        end
        code = fetch("source.print.#{lang}")
        code.gsub('faker_string_to_print', str)
      end

      ##
      # Produces source code for printing 1 through 10 in a given language.
      #
      # @param lang [Symbol] The programming language to use
      # @return [String]
      #
      # @example
      #   Faker::Source.print_1_to_10 #=> "(1..10).each { |i| puts i }"
      # @example
      #   Faker::Source.print_1_to_10(lang: :javascript)
      #   # => "for (let i=0; i<10; i++) {
      #   #       console.log(i);
      #   #    }"
      #
      # @faker.version 1.9.0
      def print_1_to_10(legacy_lang = NOT_GIVEN, lang: :ruby)
        warn_for_deprecated_arguments do |keywords|
          keywords << :lang if legacy_lang != NOT_GIVEN
        end
        fetch("source.print_1_to_10.#{lang}")
      end
    end
  end
end