Module: Brice::Shortcuts::ObjectShortcuts

Defined in:
lib/brice/shortcuts.rb

Instance Method Summary collapse

Instance Method Details

#aorta(obj = self, editor = nil) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/brice/shortcuts.rb', line 130

def aorta(obj = self, editor = nil)
  tempfile = Tempfile.new('aorta')
  YAML.dump(obj, tempfile)
  tempfile.close

  if editor ||= File.which_command(EDITORS)
    system(editor, path = tempfile.path)
    return obj unless File.exists?(path)
  else
    warn 'No suitable editor found. Please specify.'
    return obj
  end

  content = YAML.load_file(path)
  tempfile.unlink
  content
end

#cgrep(needle) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/brice/shortcuts.rb', line 95

def cgrep(needle)
  needle = %r{#{Regexp.escape(needle)}}i unless needle.is_a?(Regexp)
  klass = is_a?(Class) ? self : self.class
  res = []

  ObjectSpace.each_object(Class) { |obj|
    next unless obj <= klass

    name = obj.name
    next unless name =~ needle

    res.push(name.empty? ? obj.inspect : name)
  }

  res
end

#mgrep(needle) ⇒ Object


112
113
114
115
116
# File 'lib/brice/shortcuts.rb', line 112

def mgrep(needle)
  methods.grep(
    needle.is_a?(Regexp) ? needle : %r{#{Regexp.escape(needle)}}i
  )
end

#po(obj = self) ⇒ Object

Print object methods, sorted by name. (excluding methods that exist in the class Object)


120
121
122
# File 'lib/brice/shortcuts.rb', line 120

def po(obj = self)
  obj.methods.sort - Object.methods
end

#poc(obj = self) ⇒ Object

Print object constants, sorted by name.


125
126
127
# File 'lib/brice/shortcuts.rb', line 125

def poc(obj = self)
  obj.constants.sort if obj.respond_to?(:constants)
end