Module: Reflexive::Helpers

Includes:
RoutingHelpers
Included in:
Application
Defined in:
lib/reflexive/helpers.rb

Constant Summary collapse

CODERAY_ENCODER_OPTIONS =
{
  :wrap => :div,
  :css => :class,
  :line_numbers => :inline
}.freeze

Instance Method Summary collapse

Methods included from RoutingHelpers

#apidock_path, #class_method_definition_path, #constant_lookup_path, constant_path, #dashboard_path, #file_path, #instance_method_definition_path, #load_path_lookup_path, #method_call_path, #method_definition_path, #method_documentation_path, #method_path, #new_method_definition_path, new_method_path, #up_path

Instance Method Details

#constant_name(klass) ⇒ Object



72
73
74
# File 'lib/reflexive/helpers.rb', line 72

def constant_name(klass)
  klass.name || klass.to_s
end


127
128
129
130
131
132
133
134
135
# File 'lib/reflexive/helpers.rb', line 127

def constants_links(base_constant, constants)
  constants.map do |constant|
    full_name = constant_name(constant)
    [ full_name, constant ]
  end.sort_by(&:first).map do |full_name, constant|
    link_text = full_name.gsub("#{ base_constant }::", "")
    link_to(link_text, constant_path(constant), :title => full_name)
  end      
end

#constants_list(base_constant, constants) ⇒ Object



123
124
125
# File 'lib/reflexive/helpers.rb', line 123

def constants_list(base_constant, constants)
  constants_links(base_constant, constants).join("<br/>")
end

#constants_table(base_constant, constants, width = 120) ⇒ Object



119
120
121
# File 'lib/reflexive/helpers.rb', line 119

def constants_table(base_constant, constants, width = 120)
  Reflexive::Columnizer.columnize(constants_links(base_constant, constants), width)
end

#filter_existing_constants(constants) ⇒ Object



43
44
45
46
47
# File 'lib/reflexive/helpers.rb', line 43

def filter_existing_constants(constants)
  constants.
          select { |c| Kernel.const_defined?(c) }.
          map { |c| Kernel.const_get(c) }
end


183
184
185
186
187
# File 'lib/reflexive/helpers.rb', line 183

def full_name_link_to_method(constant, level, method_name)
  link_text = "#{ constant }#{ level == :instance ? "#" : "." }#{ method_name }"
  link_to(Rack::Utils.escape_html(link_text),
          new_method_path(constant, level, method_name))
end

#highlight_file(path, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/reflexive/helpers.rb', line 55

def highlight_file(path, options = {})
  options = CODERAY_ENCODER_OPTIONS.merge(options)
  if path =~ /\.rb$/
    src = IO.read(path)
    tokens = CodeRayRubyScanner.new(src).tokenize
    encoder = CodeRayHtmlEncoder.new(options)
    encoder.encode_tokens(tokens)
#      elsif path =~ /\.(markdown|md)$/
#        require "rdiscount"
#        src = IO.read(path)
#        markdown = RDiscount.new(src)
#        markdown.to_html
  else
    CodeRay.scan_file(path).html(options).div
  end
end

#instance_methods_table(lookup_path) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/reflexive/helpers.rb', line 137

def instance_methods_table(lookup_path)
  linked_methods = []

  %w(public protected private).each do |visibility|
    methods = lookup_path.module.send("#{ visibility }_instance_methods", false)
    
    linked_methods += methods.map do |name|
      link_to_method(lookup_path.module_name.gsub(/\[|\]/, ""),
                     name,
                     visibility)
    end
  end

  Reflexive::Columnizer.columnize(linked_methods, 120)
end

#just_methods_table(klass) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/reflexive/helpers.rb', line 153

def just_methods_table(klass)
  linked_methods = []

  ancestors_with_methods = ActiveRecord::Base.ancestors.map do |a|
    [a, a.methods(false).sort] unless a.methods(false).empty?
  end.compact

  ancestors_with_methods.each do |ancestor, ancestor_methods|
    linked_methods += ancestor_methods.map do |name|
      link_to_method(ancestor.name, name)
    end
  end
  
  Reflexive::Columnizer.columnize(linked_methods, 120)
end


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/reflexive/helpers.rb', line 76

def link_to_file(path, options = {})
  link_text = if options[:file_name_only]
    File.basename(path) + (path[-1] == ?/ ? "/" : "")
  else
    shorten_file_path(path)
  end

  link_to(link_text,
          file_path(path),
          :title => path,
          :class => "path")
end


169
170
171
172
173
174
# File 'lib/reflexive/helpers.rb', line 169

def link_to_method(constant, method_name, visibility = nil)
  link_text = truncate(method_name)
  link_to(Rack::Utils.escape_html(link_text),
          method_path(constant, method_name),
          :title => (method_name if link_text.include?("...")))
end

#load_and_highlight(location) ⇒ Object



96
97
98
99
100
# File 'lib/reflexive/helpers.rb', line 96

def load_and_highlight(location)
  tokens = CodeRay.scan(IO.read(location), :ruby)

  tokens.html(:line_numbers => :inline, :wrap => :page)
end

#methods_table(constant, lookup_path) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/reflexive/helpers.rb', line 102

def methods_table(constant, lookup_path)
  linked_methods = lookup_path.map do |name, visibility|
    link_to_method(lookup_path.module_name.gsub(/\[|\]/, ""),
                   name,
                   visibility)
  end

  Reflexive::Columnizer.columnize(linked_methods, 120)
end


176
177
178
179
180
181
# File 'lib/reflexive/helpers.rb', line 176

def new_link_to_method(constant, level, method_name)
  link_text = truncate(method_name)
  link_to(Rack::Utils.escape_html(link_text),
          new_method_path(constant, level, method_name),
          :title => (method_name if link_text.include?("...")))
end

#new_methods_table(constant, level, methods) ⇒ Object



112
113
114
115
116
117
# File 'lib/reflexive/helpers.rb', line 112

def new_methods_table(constant, level, methods)
  linked_methods = methods.sort.map do |name|
    new_link_to_method(constant, level, name)
  end
  Reflexive::Columnizer.columnize(linked_methods, 120)
end

#shorten_file_path(path) ⇒ Object



89
90
91
92
93
94
# File 'lib/reflexive/helpers.rb', line 89

def shorten_file_path(path)
  require "rbconfig"
  path.
    gsub(/\A#{ Regexp.escape(Gem.dir) }\/gems/, '#{gems}').
    gsub(/\A#{ Config::CONFIG["rubylibdir"] }/, '#{rubylib}')
end

#truncate(text, options = {}) ⇒ Object

Truncates a given text after a given :length if text is longer than :length (defaults to 30). The last characters will be replaced with the :omission (defaults to “в_│”) for a total length not exceeding :length.

Examples

truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."


197
198
199
200
201
202
203
204
# File 'lib/reflexive/helpers.rb', line 197

def truncate(text, options={})
  options.reverse_merge!(:length => 30, :omission => "...")
  if text
    len = options[:length] - options[:omission].length
    chars = text
    (chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s
  end
end