Class: I18nliner::PreProcessors::ErbPreProcessor::Helper

Inherits:
Object
  • Object
show all
Includes:
Extractors::SexpHelper
Defined in:
lib/i18nliner/pre_processors/erb_pre_processor.rb

Constant Summary collapse

DEFINITIONS =
[
  {:method => :link_to, :pattern => /link_to/, :arg => 0}
]
RUBY2RUBY =
Ruby2Ruby.new
PARSER =
RubyParser.new
SEXP_ARG_OFFSET =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extractors::SexpHelper

#raw, #string_concatenation?, #string_from, #stringish?

Constructor Details

#initialize(info, source) ⇒ Helper

Returns a new instance of Helper.



53
54
55
56
57
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 53

def initialize(info, source)
  @arg = info[:arg]
  @method = info[:method]
  @source = source
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



51
52
53
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 51

def content
  @content
end

#placeholderObject (readonly)

Returns the value of attribute placeholder.



50
51
52
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 50

def placeholder
  @placeholder
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



50
51
52
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 50

def wrapper
  @wrapper
end

Class Method Details

.match_for(string) ⇒ Object



43
44
45
46
47
48
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 43

def self.match_for(string)
  DEFINITIONS.each do |info|
    return Helper.new(info, string) if string =~ info[:pattern]
  end
  nil
end

Instance Method Details

#extract_content!(sexps) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 73

def extract_content!(sexps)
  sexp = sexps[@arg + SEXP_ARG_OFFSET]
  if stringish?(sexp)
    @content = string_from(sexp)
  else
    @placeholder = RUBY2RUBY.process(sexp)
  end
  sexps[@arg + SEXP_ARG_OFFSET] = Sexp.new(:str, "\\1")
  @wrapper = RUBY2RUBY.process(sexps)
end

#wrappable?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 60

def wrappable?
  return @wrappable if !@wrappable.nil?
  begin
    sexps = PARSER.parse(@source)
    @wrappable = sexps.sexp_type == :call &&
                 sexps[1].nil? &&
                 sexps[2] == @method &&
                 sexps[@arg + SEXP_ARG_OFFSET]
    extract_content!(sexps) if @wrappable
    @wrappable
  end
end