Sha256: 799f77c6022c4ac5499a66cb5ceafce28119d8f897429d94852fd84733a2be06

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

class TokyoMetro::Document::MakeExamples::Static::EachFile::EachMethod

  def initialize ( indent , title , h , value_in_block , displayed )
    @indent = indent
    @title = title
    @hash_const = h.gsub( /\Ahash \: / , "" )
    @value_in_block = value_in_block.gsub( /\Avalue in block \: / , "" )
    @displayed = displayed.gsub( /\Adisplayed \: / , "" )
  end

  def to_s
    puts ""
    puts @title
    # puts ""
    # puts code_for_making_example

    [ top , results ].join( "\n" )
  end

  private

  def top
    ary = ::Array.new
    ary << @title
    [ "\# \@example" , "\#   #{code_to_output}" , "\#   \=\>" ].each do | row |
      ary << ( " " * @indent + row )
    end
    ary.join( "\n" )
  end

  def code_to_output
    "#{@hash_const}.each_value { | #{@value_in_block} | puts #{@displayed} }"
  end

  def code_for_making_example
    "#{@hash_const}.values.map { | #{@value_in_block} | #{@displayed} }"
  end

  def results
    # puts actual_code_for_making_example
    return eval( actual_code_for_making_example )
  end

  def actual_code_for_making_example
    str = <<-CODE
      #{@hash_const}.values.map { | #{@value_in_block} |
        " " * @indent + "\#   " + #{evaluated_string}
      }
    CODE

    return str
  end

  def evaluated_string
    @displayed.split( / \+ / ).map { | str |
      code = <<-CODE
        Proc.new { | #{@value_in_block} |
        if #{str} == nil
          "\(nil\)"
        elsif #{str} == ""
          "(空文字列)"
        # elsif #{str}.instance_of?( ::String )
          # "\\\"" + #{str} + "\\\""
        else
          #{str}.to_s
        end
        }.call( #{@value_in_block} )
        CODE
      code.gsub( /\n *\Z/ , "" )
    }.join( " + " )
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tokyo_metro-0.10.1 lib/tokyo_metro/document/make_examples/static/each_file/each_method.rb