module EnumExt::Annotated

I wanted to add some quick live annotation to what’s defined and how it could be used but have no idea how to do this in a super-neat way, so it a little bit chaotic and experimental

Public Instance Methods

describe(short = true) click to toggle source
# File lib/enum_ext/annotated.rb, line 25
def describe(short = true)
  describe_basic
  short ? describe_short : describe_long
end
describe_basic() click to toggle source

call it to see what’s your enum current options are

# File lib/enum_ext/annotated.rb, line 6
def describe_basic
  puts yellow( "Basic #{enum_name} definition: \n" )
  print_hash(enum_values)
end
describe_enum_i(output = true) click to toggle source

————- per helpers describers ——————————-


# File lib/enum_ext/annotated.rb, line 46
  def describe_enum_i(output = true)
    description = basic_helpers_usage_header(:enum_i)
    description << <<~ENUM_I if enabled_features[:enum_i]
      #{black("instance")}.#{cyan( enabled_features[:enum_i] )} 
      # output will be same as #{base_class.to_s}.#{enum_name}[:#{enabled_features[:key_sample]}]
    ENUM_I

    output ? puts(description) : description
  end
describe_humanizations(output = true) click to toggle source
# File lib/enum_ext/annotated.rb, line 113
def describe_humanizations(output = true)
  description = if enabled_features[:humanization].blank?
    red( "\nHumanization not used!\n" )
  else
    red( "\nHumanization definitions (will skip instance dependent humanization)\n" ) <<
      inspect_hash(enabled_features[:humanization])
  end

  output ? puts(description) : description
end
describe_long() click to toggle source

call it to see which enum extensions are defined.

# File lib/enum_ext/annotated.rb, line 12
def describe_long
  puts yellow( "\n\nEnumExt extensions:" )

  puts [
    describe_enum_i(false),
    describe_mass_assign_enum(false),
    describe_multi_enum_scopes(false),
    describe_supersets(false),
    describe_translations(false),
    describe_humanizations(false)
  ].join( "\n" + "-" * 100 + "\n" )
end
describe_mass_assign_enum(output = true) click to toggle source
# File lib/enum_ext/annotated.rb, line 56
  def describe_mass_assign_enum(output = true)
    description = basic_helpers_usage_header(:mass_assign_enum)
    description << <<~MASS_ASSIGN if enabled_features[:mass_assign_enum]
      # To assign #{enabled_features[:key_sample]} to all elements of any_scope or relation call:
      #{black(base_class.to_s)}.any_scope.#{cyan( enabled_features[:mass_assign_enum] )}
    MASS_ASSIGN

    output ? puts(description) : description
  end
describe_multi_enum_scopes(output = true) click to toggle source
# File lib/enum_ext/annotated.rb, line 66
  def describe_multi_enum_scopes(output = true)
    description = basic_helpers_usage_header(:multi_enum_scopes)
    description << <<~MULTI_SCOPES if enabled_features[:multi_enum_scopes]
      # Two scopes: with_#{enum_name} and without_#{enum_name} are defined
      # To get elements with a given enums or supersets values call:
      #{black(base_class.to_s)}.#{cyan("with_#{enum_name}")}(:#{keys.sample(2).join(", :")})
      \n# To get all elements except for the ones with enums or supersets values call:
      #{black(base_class.to_s)}.#{cyan("without_#{enum_name}")}(:#{keys.sample(2).join(", :")})
    MULTI_SCOPES

    output ? puts(description) : description
  end
describe_short() click to toggle source
# File lib/enum_ext/annotated.rb, line 30
  def describe_short
    enabled, disabled = enabled_features.except(:key_sample).partition{!_2.blank?}.map{ |prt| prt.map(&:shift) }
    puts <<~SHORT
      #{yellow("EnumExt extensions:")}
      #{cyan("Enabled")}: #{enabled.join(", ")}
      #{red("Disabled")}: #{disabled.join(", ")}
    SHORT

    print_short(:supersets)
    print_short(:translations)
    print_short(:humanization)
  end
describe_supersets(output = true) click to toggle source
# File lib/enum_ext/annotated.rb, line 79
  def describe_supersets(output = true)
    description = if enabled_features[:supersets].blank?
      red( "\nSupersets not used!\n" )
    else
      superset_method = enabled_features[:supersets].keys.first
      red( "\nSupersets definitions:\n" ) << inspect_hash(enabled_features[:supersets]) << <<~SUPERSETS
          
          # Instance methods added: #{enabled_features[:supersets].keys.join("?, ")}?
          # Usage:
          #{black("instance")}.#{cyan(superset_method)}?
          # Will be equal true if any of: #{supersets_raw[superset_method].join("?, ")}? is true
          
          # Class level methods/scopes added: #{enabled_features[:supersets].keys.join(", ")}
          # Usage:
          #{black(base_class.to_s)}.#{cyan(superset_method)}
          # Will be getting all instances with #{enum_name} equals to any of: #{supersets_raw[superset_method].join(", ")}

       SUPERSETS
    end

    output ? puts(description) : description
  end
describe_translations(output = true) click to toggle source
# File lib/enum_ext/annotated.rb, line 102
def describe_translations(output = true)
  description = if enabled_features[:translations].blank?
    red( "\nTranslations not used!\n" )
  else
    red( "\nTranslations definitions (will skip instance dependent translation)\n" ) <<
      inspect_hash(enabled_features[:translations])
  end

  output ? puts(description) : description
end

Private Instance Methods

basic_helpers_usage_header(helper_name) click to toggle source
# File lib/enum_ext/annotated.rb, line 139
def basic_helpers_usage_header(helper_name)
  enabled_features[helper_name] ? "\n#{red(helper_name)} helpers enabled, usage:\n"
         : "\n#{helper_name} wasn't used\n"
end
black(comment) click to toggle source
# File lib/enum_ext/annotated.rb, line 174
def black(comment)
  # bright black bold ANSI color
  "\e[0;90;1;49m#{comment}\e[0m"
end
cyan(str) click to toggle source
# File lib/enum_ext/annotated.rb, line 164
def cyan(str)
  # cyan ANSI color
  "\e[0;36;49m#{str}\e[0m"
end
enabled_features() click to toggle source
# File lib/enum_ext/annotated.rb, line 126
def enabled_features
  enum_sample = keys.first
  {
    key_sample: enum_sample,
    enum_i: base_class.instance_methods.include?("#{enum_name}_i".to_sym) && "#{enum_name}_i",
    mass_assign_enum: base_class.respond_to?("#{enum_sample}!") && "#{enum_sample}!",
    multi_enum_scopes: base_class.respond_to?("with_#{enum_name.to_s.pluralize}") && "with_#{enum_name.to_s.pluralize}",
    supersets: supersets_raw,
    translations: try(:t_options),
    humanization: try(:t_options)
  }
end
inspect_hash(hsh) click to toggle source
# File lib/enum_ext/annotated.rb, line 148
def inspect_hash(hsh)
  defined?(ai) ? hsh.ai : hsh.inspect
end
print_hash(hsh) click to toggle source
print_short(feature) click to toggle source
red(str) click to toggle source
# File lib/enum_ext/annotated.rb, line 169
def red(str)
  # red ANSI color
  "\e[0;31;49m#{str}\e[0m"
end
yellow(str) click to toggle source
# File lib/enum_ext/annotated.rb, line 159
def yellow(str)
  # yellow ANSI color
  "\e[0;33;49m#{str}\e[0m"
end