Class: LocalModel::CSV

Inherits:
Model
  • Object
show all
Extended by:
CSVInteractable::ClassMethods
Includes:
CSVInteractable::InstanceMethods
Defined in:
lib/local_model/csv.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CSVInteractable::ClassMethods

all_records, append_row, delete_all_rows, delete_row, each_record, find_record, get_adapter, mutate_csv, mutate_row, new_from_record, prep_row_read!, prep_row_write!, read_from_record, write_headers

Methods included from CSVInteractable::InstanceMethods

#save, #save!

Methods inherited from Model

#==, belongs_to, has_many, has_one, storage_path, #update, #update!

Constructor Details

#initialize(**args) ⇒ CSV

Returns a new instance of CSV.



149
150
151
152
153
154
155
156
157
# File 'lib/local_model/csv.rb', line 149

def initialize(**args)
  self.schema_defaults.each do |property, value|
    self.send("#{property}=", value)
  end
  args.each do |k,v|
    self.send("#{k}=", v)
  end
  self.id = nil
end

Class Method Details

.allObject



51
52
53
54
55
56
57
# File 'lib/local_model/csv.rb', line 51

def self.all
  all_instances = []
  each_record do |row|
    all_instances << new_from_record(row)
  end
  all_instances
end

.countObject



59
60
61
62
63
# File 'lib/local_model/csv.rb', line 59

def self.count
  total = 0
  self.each_record{ total += 1 }
  total
end

.create(**args) ⇒ Object

TODO: Move necessary methods to model.rb



137
138
139
140
141
# File 'lib/local_model/csv.rb', line 137

def self.create(**args)
  inst = new(**args)
  inst.save
  inst
end

.create!(**args) ⇒ Object



143
144
145
146
147
# File 'lib/local_model/csv.rb', line 143

def self.create!(**args)
  inst = new(**args)
  inst.save!
  inst
end

.destroy_allObject



65
66
67
# File 'lib/local_model/csv.rb', line 65

def self.destroy_all
  delete_all_rows
end

.find(id) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/local_model/csv.rb', line 120

def self.find(id)
  found_record = self.find_by(id: id)
  if !found_record
    raise LocalModel::RecordNotFound.new
  else
    found_record
  end
end

.find_by(**args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/local_model/csv.rb', line 88

def self.find_by(**args)
  found_record = find_record do |row|
    matched = true
    args.each do |k,v|
      if row[k] != v
        matched = false
        break
      end
    end
    return new_from_record(row) if matched
  end
  nil
end

.find_or_create_by(**args) ⇒ Object



129
130
131
132
133
# File 'lib/local_model/csv.rb', line 129

def self.find_or_create_by(**args)
  found_obj = find_by(**args)
  return found_obj if found_obj
  create(**args)
end

.firstObject



102
103
104
# File 'lib/local_model/csv.rb', line 102

def self.first
  all.first
end

.lastObject



110
111
112
# File 'lib/local_model/csv.rb', line 110

def self.last
  all.last
end

.new_from_record(row) ⇒ Object



114
115
116
117
118
# File 'lib/local_model/csv.rb', line 114

def self.new_from_record(row)
  obj = new(**row.to_h)
  obj.id = row[:id]
  obj
end

.schema {|builder| ... } ⇒ Object

Yields:

  • (builder)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/local_model/csv.rb', line 6

def self.schema(&block)
  builder = SchemaBuilder.new(self)
  yield builder

  schema_dict = builder.schema.dup
  self.define_method :get_schema do
    schema_dict
  end

  self.define_singleton_method :get_schema do
    schema_dict
  end

  cols = builder.schema.keys.dup
  self.define_method :columns do
    cols
  end

  self.define_singleton_method :columns do
    cols
  end

  schema_data = cols.each_with_index.reduce({}) do |mem, (key,i)|
    mem[key] = {}
    mem[key][:adapter] = get_adapter(schema_dict[key])
    mem[key][:type] = schema_dict[key]
    mem[key][:column_number] = i
    mem
  end

  self.define_method :schema_data do
    schema_data
  end

  self.define_method :schema_defaults do 
    builder.defaults
  end

  if !File.file?(self.storage_path)
    CSV.open(self.storage_path, 'wb') do |csv|
      csv << cols.map(&:to_s)
    end
  end
end

.secondObject



106
107
108
# File 'lib/local_model/csv.rb', line 106

def self.second
  all[1]
end

.where(**args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/local_model/csv.rb', line 69

def self.where(**args)
  arr = all_records do |row|
    found = true
    args.each do |k,v|
      if row[k] != v
        found = false
        break
      end
    end
    found
  end.map{|r| new_from_record(r) }
  LocalModel::Collection.create_from(
    array: arr,
    for_model: self,
    for_collection_class: nil,
    add_to_collection_proc: ->(a,b) { raise NoMethodError.new("Cannot add to this collection") }
  )
end

Instance Method Details

#destroyObject



168
169
170
# File 'lib/local_model/csv.rb', line 168

def destroy
  self.class.delete_row({id: self.id})
end

#reloadObject



163
164
165
166
# File 'lib/local_model/csv.rb', line 163

def reload
  raise LocalModel::RecordNotFound if !self.id
  self.class.find(self.id)
end

#saved?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/local_model/csv.rb', line 159

def saved?
  !self.id.nil?
end