Module: EnumExt::Annotated

Included in:
EnumWrapper
Defined in:
lib/enum_ext/annotated.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#describe(short = true) ⇒ Object



25
26
27
28
# File 'lib/enum_ext/annotated.rb', line 25

def describe(short = true)
  describe_basic
  short ? describe_short : describe_long
end

#describe_basicObject

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



6
7
8
9
# 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) ⇒ Object


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




46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# 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_longObject

call it to see which enum extensions are defined.



12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# 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_shortObject



30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# 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