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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

Class Attribute Details

.primary_keyObject



72
73
74
# File 'lib/active_tsv/base.rb', line 72

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



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

def all
  Relation.new(self)
end

.column_namesObject



51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
# 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?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/active_tsv/base.rb', line 68

def custom_column_name?
  @custom_column_name
end

.encodingObject



78
79
80
# File 'lib/active_tsv/base.rb', line 78

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

.encoding=(enc) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# 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

.open(&block) ⇒ Object



47
48
49
# 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

.reload(path) ⇒ Object



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



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

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

Instance Method Details

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



131
132
133
# File 'lib/active_tsv/base.rb', line 131

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

#[](key) ⇒ Object



119
120
121
# File 'lib/active_tsv/base.rb', line 119

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

#[]=(key, value) ⇒ Object



123
124
125
# File 'lib/active_tsv/base.rb', line 123

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

#attributesObject



127
128
129
# File 'lib/active_tsv/base.rb', line 127

def attributes
  @attrs.dup
end

#inspectObject



115
116
117
# File 'lib/active_tsv/base.rb', line 115

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