module AutoAttrInit::Refinments

Utility refinments for this AutoAttrInit.

Public Instance Methods

detect_deep(ifnone = ->{ nil) click to toggle source

Array#detect のdeep search版。 @return [Object|nil]

# File lib/auto_attr_init/refinments.rb, line 34
def detect_deep ifnone = ->{ nil }, &p
  target = nil
  each do |elm|
    (target = elm; break) if p.call(elm)
    next unless elm.has_method?(:find_deep)
    v = elm.detect_deep ifnone, &p
    (target = v; break) if v != nil
    nil
  end
  target
end
Also aliased as: find_deep
find_deep(ifnone = ->{ nil)
Alias for: detect_deep
has_method?(name) click to toggle source

@param name [Stirng] @return [Boolean]

# File lib/auto_attr_init/refinments.rb, line 9
def has_method? name
  methods.any?{|m| m.to_s == name.to_s }
end
source_code() click to toggle source

method本体のsource codeを文字列で取得する。 @return [String]

# File lib/auto_attr_init/refinments.rb, line 17
def source_code
  source = ''
  File.open source_location[0], 'r:utf-8' do |f|
    lines = f.read.each_line.to_a
    source = lines[source_location[1] - 1]
    (source_location[1] .. lines.length - 1).each do |i|
      break if Ripper.sexp_raw(source, source_location[0], source_location[1])
      source += lines[i]
    end
  end
  source
end