Class: Tins::LinesFile

### Home Classes Methods

### In Files

- lib/tins/lines_file.rb

### Parent

Object

### Namespace

- MODULE Tins::LinesFile::LineExtension

### Methods

- ::for_file
- ::for_filename
- ::for_lines
- ::new
- #each
- #empty?
- #file_linenumber
- #inspect
- #last_line_number
- #line
- #line_number=
- #match_backward
- #match_forward
- #next!
- #previous!
- #rewind
- #to_s

### Included Modules

- Enumerable

### Class/Module Index

Quicksearch

- Tins
- Tins
- Tins
- Tins::Annotate
- Tins::AskAndSend
- Tins::Attempt
- Tins::Bijection
- Tins::Blank
- Tins::Blank::Array
- Tins::Blank::FalseClass
- Tins::Blank::Hash
- Tins::Blank::NilClass
- Tins::Blank::Numeric
- Tins::Blank::Object
- Tins::Blank::String
- Tins::Blank::TrueClass
- Tins::BlankSlate
- Tins::BlockSelf
- Tins::CasePredicate
- Tins::ClassMethod
- Tins::Concern
- Tins::Concern::ModuleMixin
- Tins::Constant
- Tins::ConstantMaker
- Tins::CountBy
- Tins::DSLAccessor
- Tins::DateDummy
- Tins::DateTimeDummy
- Tins::DeepConstGet
- Tins::DeepDup
- Tins::Deflect
- Tins::Deflect::DeflectError
- Tins::Deflect::Deflector
- Tins::Deflect::DeflectorCollection
- Tins::Delegate
- Tins::DynamicScope
- Tins::DynamicScope::Context
- Tins::Eigenclass
- Tins::ExtractLastArgumentOptions
- Tins::FileBinary
- Tins::FileBinary::ClassMethods
- Tins::FileBinary::Constants
- Tins::Find
- Tins::Find::Finder
- Tins::Find::Finder::PathExtension
- Tins::FromModule
- Tins::Full
- Tins::GO
- Tins::GO::EnumerableExtension
- Tins::Generator
- Tins::HashSymbolizeKeysRecursive
- Tins::HashUnion
- Tins::IfPredicate
- Tins::InstanceExec
- Tins::Interpreter
- Tins::Limited
- Tins::LinesFile
- Tins::LinesFile::LineExtension
- Tins::Memoize
- Tins::Memoize::CacheMethods
- Tins::MethodDescription
- Tins::MethodMissingDelegator
- Tins::MethodMissingDelegator::DelegatorClass
- Tins::MethodMissingDelegator::DelegatorModule
- Tins::Minimize
- Tins::ModuleGroup
- Tins::NamedSet
- Tins::Null
- Tins::Null::Kernel
- Tins::NullClass
- Tins::NullPlus
- Tins::Once
- Tins::P
- Tins::ParameterizedModule
- Tins::PartialApplication
- Tins::ProcCompose
- Tins::ProcPrelude
- Tins::RangePlus
- Tins::RequireMaybe
- Tins::Responding
- Tins::Rotate
- Tins::Scope
- Tins::SecureWrite
- Tins::SexySingleton
- Tins::SexySingleton::SingletonClassMethods
- Tins::Shuffle
- Tins::StringByteOrderMark
- Tins::StringCamelize
- Tins::StringUnderscore
- Tins::StringVersion
- Tins::StringVersion::Version
- Tins::Subhash
- Tins::SymbolMaker
- Tins::Terminal
- Tins::ThreadGlobal
- Tins::ThreadLocal
- Tins::TimeDummy
- Tins::TimeFreezer
- Tins::To
- Tins::ToProc
- Tins::Token
- Tins::UniqBy
- Tins::Write
- Object
- Object::DSLKit
- Object::Spruz
- Array
- Date
- DateTime
- Enumerable
- File
- Hash
- IO
- IRB
- Method
- Module
- Proc
- Range
- String
- Symbol
- Time
- UnboundMethod

No matching classes.

# Tins::LinesFile

### Attributes

filename[RW]
line_number[R]

### Public Class Methods

for_file(file, line_number = nil) click to toggle source

    # File lib/tins/lines_file.rb, line 17
    def self.for_file(file, line_number = nil)
      obj = new(file.readlines, line_number)
      obj.filename = file.path
      obj
    end

for_filename(filename, line_number = nil) click to toggle source

    # File lib/tins/lines_file.rb, line 11
    def self.for_filename(filename, line_number = nil)
      obj = new(File.readlines(filename), line_number)
      obj.filename = filename
      obj
    end

for_lines(lines, line_number = nil) click to toggle source

    # File lib/tins/lines_file.rb, line 23
    def self.for_lines(lines, line_number = nil)
      new(lines, line_number)
    end

new(lines, line_number = nil) click to toggle source

    # File lib/tins/lines_file.rb, line 27
    def initialize(lines, line_number = nil)
      @lines = lines
      @lines.each_with_index do |line, i|
        line.extend LineExtension
        line.instance_variable_set :@line_number, i + 1
        line.instance_variable_set :@lines_file, self
      end
      instance_variable_set :@line_number, line_number || (@lines.empty? ? 0 : 1)
    end

### Public Instance Methods

each(&block) click to toggle source

    # File lib/tins/lines_file.rb, line 73
    def each(&block)
      empty? and return self
      old_line_number = line_number
      1.upto(last_line_number) do |number|
        self.line_number = number
        block.call(line)
      end
      self
    ensure
      self.line_number = old_line_number
    end

empty?() click to toggle source

    # File lib/tins/lines_file.rb, line 69
    def empty?
      @lines.empty?
    end

file_linenumber() click to toggle source

    # File lib/tins/lines_file.rb, line 91
    def file_linenumber
      "#{filename}:#{line_number}"
    end

inspect() click to toggle source

    # File lib/tins/lines_file.rb, line 117
    def inspect
      "#<#{self.class}: #{to_s.inspect}>"
    end

last_line_number() click to toggle source

    # File lib/tins/lines_file.rb, line 65
    def last_line_number
      @lines.size
    end

line() click to toggle source

    # File lib/tins/lines_file.rb, line 86
    def line
      index = line_number - 1
      @lines[index] if index >= 0
    end

line_number=(number) click to toggle source

    # File lib/tins/lines_file.rb, line 58
    def line_number=(number)
      number = number.to_i
      if number > 0 && number <= last_line_number
        @line_number = number
      end
    end

match_backward(regexp, previous_after_match = false) click to toggle source

    # File lib/tins/lines_file.rb, line 95
    def match_backward(regexp, previous_after_match = false)
      begin
        if line =~ regexp
          previous_after_match and previous!
          return $~.captures
        end
      end while previous!
    end

match_forward(regexp, next_after_match = false) click to toggle source

    # File lib/tins/lines_file.rb, line 104
    def match_forward(regexp, next_after_match = false)
      begin
        if line =~ regexp
          next_after_match and next!
          return $~.captures
        end
      end while next!
    end

next!() click to toggle source

    # File lib/tins/lines_file.rb, line 46
    def next!
      old = line_number
      self.line_number += 1
      line_number > old ? self : nil
    end

previous!() click to toggle source

    # File lib/tins/lines_file.rb, line 52
    def previous!
      old = line_number
      self.line_number -= 1
      line_number < old ? self : nil
    end

rewind() click to toggle source

    # File lib/tins/lines_file.rb, line 41
    def rewind
      self.line_number = 1
      self
    end

to_s() click to toggle source

    # File lib/tins/lines_file.rb, line 113
    def to_s
      "#{line_number} #{line.chomp}"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.