class ActiveTsv::Base

@example

class User < ActiveTsv::Base
  self.table_path = "table/product_masters.tsv"
end

Constants

DEFAULT_PRIMARY_KEY
SEPARATER

Attributes

primary_key[W]
table_path[R]

Public Class Methods

all() click to toggle source
# File lib/active_tsv/base.rb, line 39
def all
  Relation.new(self)
end
column_names() click to toggle source
# File lib/active_tsv/base.rb, line 51
def column_names
  @column_names ||= begin
    @custom_column_name = false
    open { |csv| csv.gets }
  end
end
column_names=(names) click to toggle source
# File lib/active_tsv/base.rb, line 58
def column_names=(names)
  @custom_column_name = true
  column_names = names.map(&:to_s)
  column_names.each do |k|
    define_method(k) { @attrs[k] }
    define_method("#{k}=") { |v| @attrs[k] = v }
  end
  @column_names = column_names
end
custom_column_name?() click to toggle source
# File lib/active_tsv/base.rb, line 68
def custom_column_name?
  @custom_column_name
end
encoding() click to toggle source
# File lib/active_tsv/base.rb, line 78
def encoding
  @encoding ||= Encoding::UTF_8
end
encoding=(enc) click to toggle source
# File lib/active_tsv/base.rb, line 82
def encoding=(enc)
  case enc
  when String
    @encoding = Encoding.find(enc)
  when Encoding
    @encoding = enc
  else
    raise ArgumentError, "#{enc.class} dose not support"
  end
end
new(attrs = {}) click to toggle source
# File lib/active_tsv/base.rb, line 94
def initialize(attrs = {})
  case attrs
  when Hash
    h = {}
    self.class.column_names.each do |name|
      h[name] = nil
    end
    attrs.each do |name, v|
      unless respond_to?("#{name}=")
        raise UnknownAttributeError, "unknown attribute '#{name}' for #{self.class}."
      end
      h[name.to_s] = v
    end
    @attrs = h
  when Array
    @attrs = self.class.column_names.zip(attrs).to_h
  else
    raise ArgumentError, "#{attrs.class} is not supported value"
  end
end
open(&block) click to toggle source
# File lib/active_tsv/base.rb, line 47
def open(&block)
  CSV.open(table_path, "r:#{encoding}:UTF-8", col_sep: self::SEPARATER, &block)
end
primary_key() click to toggle source
# File lib/active_tsv/base.rb, line 72
def primary_key
  @primary_key ||= DEFAULT_PRIMARY_KEY
end
reload(path) click to toggle source
# File lib/active_tsv/base.rb, line 22
def reload(path)
  if @column_names
    column_names.each do |k|
      remove_method(k)
      remove_method("#{k}=")
    end
  end

  @column_names = nil
  @custom_column_name = false
  @table_path = path
  column_names.each do |k|
    define_method(k) { @attrs[k] }
    define_method("#{k}=") { |v| @attrs[k] = v }
  end
end
scope(name, proc) click to toggle source
# File lib/active_tsv/base.rb, line 43
def scope(name, proc)
  define_singleton_method(name, &proc)
end
table_path=(path) click to toggle source
# File lib/active_tsv/base.rb, line 18
def table_path=(path)
  reload(path)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/active_tsv/base.rb, line 131
def ==(other)
  super || other.instance_of?(self.class) && @attrs == other.attributes
end
Also aliased as: eql?
[](key) click to toggle source
# File lib/active_tsv/base.rb, line 119
def [](key)
  @attrs[key.to_s]
end
[]=(key, value) click to toggle source
# File lib/active_tsv/base.rb, line 123
def []=(key, value)
  @attrs[key.to_s] = value
end
attributes() click to toggle source
# File lib/active_tsv/base.rb, line 127
def attributes
  @attrs.dup
end
eql?(other)
Alias for: ==
inspect() click to toggle source
# File lib/active_tsv/base.rb, line 115
def inspect
  "#<#{self.class} #{@attrs.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
end