Module: I18nOnSteroids::ConsoleHelpers

Defined in:
lib/i18n_on_steroids/console_helpers.rb

Class Method Summary collapse

Class Method Details

.demoObject



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
# File 'lib/i18n_on_steroids/console_helpers.rb', line 5

def self.demo
  puts "\n" + "=" * 80
  puts "I18nOnSteroids Interactive Demo"
  puts "=" * 80

  # Setup demo translations
  I18n.backend.store_translations(:en, {
    demo: {
      simple: "Hello ${name | upcase}!",
      chained: "${text | titleize | truncate: 20}",
      conditional: "${value | upcase if: admin}",
      composition: "${count | pluralize: ${unit}}",
      formatting: "${price | currency: โ‚ฌ} on ${date | date_format: %B %d, %Y}"
    }
  })

  include TranslationHelper

  puts "\n๐Ÿ“ Demo Translations:"
  puts "-" * 80

  demos = [
    {
      key: "demo.simple",
      opts: { name: "world" },
      desc: "Basic pipe transformation"
    },
    {
      key: "demo.chained",
      opts: { text: "hello world from ruby" },
      desc: "Chained pipes"
    },
    {
      key: "demo.conditional",
      opts: { value: "secret", admin: true },
      desc: "Conditional pipe (admin=true)"
    },
    {
      key: "demo.conditional",
      opts: { value: "secret", admin: false },
      desc: "Conditional pipe (admin=false)"
    },
    {
      key: "demo.composition",
      opts: { count: 5, unit: "item" },
      desc: "Pipe composition"
    },
    {
      key: "demo.formatting",
      opts: { price: 1234.56, date: Time.now },
      desc: "Complex formatting"
    }
  ]

  demos.each_with_index do |demo, idx|
    puts "\n#{idx + 1}. #{demo[:desc]}"
    puts "   Key: #{demo[:key]}"
    puts "   Options: #{demo[:opts].inspect}"
    puts "   Result: #{translate(demo[:key].to_sym, **demo[:opts])}"
  end

  puts "\n" + "=" * 80
  puts "Available Commands:"
  puts "  I18nOnSteroids.demo              # Show this demo"
  puts "  I18nOnSteroids.test_pipe         # Test a pipe interactively"
  puts "  I18nOnSteroids.list_pipes        # List all available pipes"
  puts "  I18nOnSteroids.pipe_info(name)   # Show info about a pipe"
  puts "=" * 80 + "\n"
end

.list_pipesObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/i18n_on_steroids/console_helpers.rb', line 111

def self.list_pipes
  pipes = TranslationHelper.available_pipes
  aliases = TranslationHelper.pipe_aliases

  puts "\n๐Ÿ“‹ Available Pipes"
  puts "=" * 80

  puts "\nโœจ Built-in Pipes (#{pipes[:built_in].length}):"
  pipes[:built_in].sort.each do |pipe|
    puts "  โ€ข #{pipe}"
  end

  if pipes[:custom].any?
    puts "\n๐Ÿ”ง Custom Pipes (#{pipes[:custom].length}):"
    pipes[:custom].sort.each do |pipe|
      puts "  โ€ข #{pipe}"
    end
  end

  if aliases.any?
    puts "\n๐Ÿ”— Pipe Aliases (#{aliases.length}):"
    aliases.sort.each do |alias_name, target|
      puts "  โ€ข #{alias_name} โ†’ #{target}"
    end
  end

  puts "\n๐Ÿ’ก Tip: Use I18nOnSteroids.pipe_info(:pipe_name) for details"
  puts "=" * 80 + "\n"
end

.pipe_info(pipe_name) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/i18n_on_steroids/console_helpers.rb', line 141

def self.pipe_info(pipe_name)
  pipe_docs = {
    upcase: "Convert string to uppercase",
    downcase: "Convert string to lowercase",
    capitalize: "Capitalize first character",
    titleize: "Convert to title case",
    humanize: "Make string human-readable",
    parameterize: "Convert to URL-safe format (params: separator)",
    strip: "Remove leading/trailing whitespace",
    squish: "Remove excess whitespace",
    truncate: "Truncate to length (params: length)",
    pluralize: "Pluralize word (params: count)",
    round: "Round number (params: precision)",
    number_with_delimiter: "Format number with thousands separator",
    currency: "Format as currency (params: unit)",
    date_format: "Format date (params: strftime pattern)",
    time_format: "Format time (params: strftime pattern)",
    default: "Provide fallback value (params: default value)",
    format: "Custom format string (params: format)",
    html_safe: "Mark string as HTML safe"
  }

  puts "\n๐Ÿ“– Pipe Info: #{pipe_name}"
  puts "=" * 80

  doc = pipe_docs[pipe_name.to_sym]
  if doc
    puts "\nDescription: #{doc}"

    examples = {
      upcase: "\${name | upcase}",
      downcase: "\${NAME | downcase}",
      capitalize: "\${text | capitalize}",
      titleize: "\${text | titleize}",
      humanize: "\${field_name | humanize}",
      parameterize: "\${title | parameterize} or \${title | parameterize: _}",
      strip: "\${text | strip}",
      squish: "\${text | squish}",
      truncate: "\${text | truncate: 50}",
      pluralize: "\${word | pluralize: %{count}}",
      round: "\${number | round: 2}",
      currency: "\${price | currency} or \${price | currency: โ‚ฌ}",
      date_format: "\${date | date_format: %Y-%m-%d}",
      time_format: "\${time | time_format: %H:%M}",
      default: "\${optional | default: N/A}",
      format: "\${value | format: %.2f}"
    }

    if examples[pipe_name.to_sym]
      puts "\nExample: #{examples[pipe_name.to_sym]}"
    end

    # Check for aliases
    aliases = TranslationHelper.pipe_aliases.select { |_, target| target == pipe_name.to_s }
    if aliases.any?
      puts "\nAliases: #{aliases.keys.join(', ')}"
    end

  else
    puts "\nNo documentation available for '#{pipe_name}'"
    puts "\nUse I18nOnSteroids.list_pipes to see all available pipes"
  end

  puts "=" * 80 + "\n"
end

.test_pipe(pipe_name = nil, value = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/i18n_on_steroids/console_helpers.rb', line 75

def self.test_pipe(pipe_name = nil, value = nil)
  include TranslationHelper

  if pipe_name.nil?
    puts "\nUsage: I18nOnSteroids.test_pipe(:pipe_name, value, param: 'optional')"
    puts "\nExample:"
    puts "  I18nOnSteroids.test_pipe(:upcase, 'hello')"
    puts "  I18nOnSteroids.test_pipe(:truncate, 'long text', '10')"
    puts "  I18nOnSteroids.test_pipe(:currency, 1234.56, 'โ‚ฌ')"
    return
  end

  if value.nil?
    puts "Error: Please provide a value to test"
    return
  end

  puts "\n๐Ÿงช Testing Pipe: #{pipe_name}"
  puts "-" * 80

  I18n.backend.store_translations(:en, {
    test: "Result: ${value | #{pipe_name}}"
  })

  begin
    result = translate(:test, value: value)
    puts "Input:  #{value.inspect}"
    puts "Output: #{result}"
    puts "โœ“ Success"
  rescue StandardError => e
    puts "โœ— Error: #{e.message}"
  end

  puts
end