Class: ActiveTsv::Base

Inherits:
Object
  • Object
show all
Extended by:
Querying, Reflection
Defined in:
lib/active_tsv/base.rb

Overview

Examples:

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

Direct Known Subclasses

ActiveCsv::Base

Constant Summary collapse

SEPARATER =
"\t"
DEFAULT_PRIMARY_KEY =
"id"

Constants included from Querying

Querying::METHODS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reflection

belongs_to, has_many, has_one

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



76
77
78
79
80
81
82
83
84
85
# File 'lib/active_tsv/base.rb', line 76

def initialize(attrs = {})
  case attrs
  when Hash
    @attrs = attrs
  when Array
    @attrs = self.class.column_names.zip(attrs).to_h
  else
    raise ArgumentError, "#{attrs.class} is not supported value"
  end
end

Class Attribute Details

.primary_keyObject



54
55
56
# File 'lib/active_tsv/base.rb', line 54

def primary_key
  @primary_key ||= DEFAULT_PRIMARY_KEY
end

.table_pathObject

Returns the value of attribute table_path.



16
17
18
# File 'lib/active_tsv/base.rb', line 16

def table_path
  @table_path
end

Class Method Details

.allObject



38
39
40
# File 'lib/active_tsv/base.rb', line 38

def all
  Relation.new(self)
end

.column_namesObject



50
51
52
# File 'lib/active_tsv/base.rb', line 50

def column_names
  @column_names ||= open { |csv| csv.gets }
end

.encodingObject



60
61
62
# File 'lib/active_tsv/base.rb', line 60

def encoding
  @encoding ||= Encoding::UTF_8
end

.encoding=(enc) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/active_tsv/base.rb', line 64

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

.open(&block) ⇒ Object



46
47
48
# File 'lib/active_tsv/base.rb', line 46

def open(&block)
  CSV.open(table_path, "r:#{encoding}:UTF-8", col_sep: self::SEPARATER, &block)
end

.reload(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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
  @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) ⇒ Object



42
43
44
# File 'lib/active_tsv/base.rb', line 42

def scope(name, proc)
  define_singleton_method(name, &proc)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



103
104
105
# File 'lib/active_tsv/base.rb', line 103

def ==(other)
  super || other.instance_of?(self.class) && @attrs == other.attributes
end

#[](key) ⇒ Object



91
92
93
# File 'lib/active_tsv/base.rb', line 91

def [](key)
  @attrs[key.to_s]
end

#[]=(key, value) ⇒ Object



95
96
97
# File 'lib/active_tsv/base.rb', line 95

def []=(key, value)
  @attrs[key.to_s] = value
end

#attributesObject



99
100
101
# File 'lib/active_tsv/base.rb', line 99

def attributes
  @attrs.dup
end

#inspectObject



87
88
89
# File 'lib/active_tsv/base.rb', line 87

def inspect
  "#<#{self.class} #{@attrs.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
end