Module: Redbreast::Helper::General
- Included in:
- Command::ColorGenerator, Command::ColorTestGenerator, Command::ConfigurationInstaller, Command::ImageGenerator, Command::ImageTestGenerator, Command::Setup, Serializer::ObjC, Serializer::Swift, Serializer::SwiftUI
- Defined in:
- lib/redbreast/helpers/general.rb
Overview
Module with general metods used in creating files
Constant Summary collapse
- ESCAPE_KEYWORDS =
['associatedtype', 'class', 'deinit', 'enum', 'extension', 'fileprivate', 'func', 'import', 'init', 'inout', 'internal', 'let', 'operator', 'private', 'protocol', 'public', 'static', 'struct', 'subscript', 'typealias', 'var', 'break', 'case', 'continue', 'default', 'defer', 'do', 'else', 'fallthrough', 'for', 'guard', 'if', 'in', 'repeat', 'return', 'switch', 'where', 'while', 'as', 'Any', 'catch', 'false', 'is', 'nil', 'rethrows', 'super', 'self', 'Self', 'throw', 'throws', 'true', 'try', '_'].freeze
Instance Method Summary collapse
- #app_name ⇒ Object
- #bundles ⇒ Object
- #clean_case_name(name) ⇒ Object
- #clean_enum_name(name) ⇒ Object
- #clean_variable_name(name) ⇒ Object
- #config ⇒ Object
- #escape_keyword_if_needed(name) ⇒ Object
- #escape_with_underscore_if_needed(name) ⇒ Object
- #indent(level = 0, initial = '') ⇒ Object
- #programming_language ⇒ Object
- #upper_camel_case(name) ⇒ Object
Instance Method Details
#app_name ⇒ Object
28 29 30 |
# File 'lib/redbreast/helpers/general.rb', line 28 def app_name @app_name ||= config[:app_name] end |
#bundles ⇒ Object
24 25 26 |
# File 'lib/redbreast/helpers/general.rb', line 24 def bundles @bundles ||= config[:bundles] end |
#clean_case_name(name) ⇒ Object
59 60 61 |
# File 'lib/redbreast/helpers/general.rb', line 59 def clean_case_name(name) clean_variable_name(name) end |
#clean_enum_name(name) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/redbreast/helpers/general.rb', line 38 def clean_enum_name(name) clean_name = name .split(/[^0-9a-zA-Z]/) .reject(&:empty?) .map(&:capitalize) .join escape_with_underscore_if_needed(clean_name) end |
#clean_variable_name(name) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/redbreast/helpers/general.rb', line 63 def clean_variable_name(name) clean_name = name .split(/[^0-9a-zA-Z]/) .reject(&:empty?) .each_with_index .map { |v, i| i.zero? ? v.tap { |char| char[0] = char[0].downcase } : v.capitalize } .join escaped_underscore = escape_with_underscore_if_needed(clean_name) escape_keyword_if_needed(escaped_underscore) end |
#config ⇒ Object
16 17 18 |
# File 'lib/redbreast/helpers/general.rb', line 16 def config @config ||= Redbreast::IO::Config.read end |
#escape_keyword_if_needed(name) ⇒ Object
81 82 83 84 85 |
# File 'lib/redbreast/helpers/general.rb', line 81 def escape_keyword_if_needed(name) return name unless ESCAPE_KEYWORDS.include? name "`#{name}`" end |
#escape_with_underscore_if_needed(name) ⇒ Object
75 76 77 78 79 |
# File 'lib/redbreast/helpers/general.rb', line 75 def escape_with_underscore_if_needed(name) return name if name.match(/^[A-Za-z_]/) '_' + name end |
#indent(level = 0, initial = '') ⇒ Object
32 33 34 35 36 |
# File 'lib/redbreast/helpers/general.rb', line 32 def indent(level = 0, initial = '') (1..level) .to_a.reduce('') { |result, _| result + ' ' } .concat(initial) end |
#programming_language ⇒ Object
20 21 22 |
# File 'lib/redbreast/helpers/general.rb', line 20 def programming_language @programming_language ||= config[:language] end |
#upper_camel_case(name) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/redbreast/helpers/general.rb', line 48 def upper_camel_case(name) clean_name = name .split(/[^0-9a-zA-Z]/) .reject(&:empty?) .each_with_index .map { |v, i| i.zero? ? v.tap { |char| char[0] = char[0].upcase } : v.capitalize } .join escape_with_underscore_if_needed(clean_name) end |