Class: Feet::Model::FileModel

Inherits:
Object
  • Object
show all
Defined in:
lib/feet/file_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileModel

Returns a new instance of FileModel.



6
7
8
9
10
11
12
13
14
15
# File 'lib/feet/file_model.rb', line 6

def initialize(filename)
  @filename = filename

  # If filename is "dir/21.json", @id is 21
  basename = File.split(filename)[-1]
  @id = File.basename(basename, '.json').to_i

  row_object = File.read(filename)
  @hash = MultiJson.load(row_object)
end

Class Method Details

.allObject



39
40
41
42
# File 'lib/feet/file_model.rb', line 39

def self.all
  files = Dir['db/quotes/*.json']
  files.map { |f| FileModel.new f }
end

.create(attrs) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/feet/file_model.rb', line 44

def self.create(attrs)
  # Create hash
  hash = {}
  hash['attribution'] = attrs['attribution'] || ''
  hash['submitter'] = attrs['submitter'] || ''
  hash['quote'] = attrs['quote'] || ''

  # Find highest id
  files = Dir['db/quotes/*.json']
  names = files.map { |f| File.split(f)[-1] } # transform to_i here?
  highest = names.map(&:to_i).max
  id = highest + 1

  # Open and write the new file
  new_filename = "db/quotes/#{id}.json"
  File.open("db/quotes/#{id}.json", 'w') do |f|
    f.write <<~TEMPLATE
      {
          "submitter": "#{hash['submitter']}",
          "quote": "#{hash['quote']}",
          "attribution": "#{hash['attribution']}"
      }
    TEMPLATE
  end

  # Create new FileModel instance with the new file
  FileModel.new new_filename
end

.find(id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/feet/file_model.rb', line 25

def self.find(id)
  id = id.to_i
  @dm_style_cache ||= {}
  begin
    return @dm_style_cache[id] if @dm_style_cache[id]

    found = FileModel.new("db/quotes/#{id}.json")
    @dm_style_cache[id] = found
    found
  rescue Errno::ENOENT
    nil
  end
end

.find_all_by_attribute(attribute, value) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/feet/file_model.rb', line 91

def self.find_all_by_attribute(attribute, value)
  id = 1
  results = []
  loop do
    model = find(id)
    return results unless model

    results.push(model) if model[attribute] == value
    id += 1
  end
end

.find_all_by_submitter(name = '') ⇒ Object



103
104
105
106
107
# File 'lib/feet/file_model.rb', line 103

def self.find_all_by_submitter(name = '')
  return [] unless name

  find_all_by_attribute('submitter', name)
end

.method_missing(m, *args) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/feet/file_model.rb', line 109

def self.method_missing(m, *args)
  base = /^find_all_by_(.*)/
  if m.to_s.start_with? base
    key = m.to_s.match(base)[1]
    find_all_by_attribute(key, args[0])
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/feet/file_model.rb', line 119

def self.respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('find_all_by') || super
end

Instance Method Details

#[](name) ⇒ Object



17
18
19
# File 'lib/feet/file_model.rb', line 17

def [](name)
  @hash[name.to_s]
end

#[]=(name, value) ⇒ Object



21
22
23
# File 'lib/feet/file_model.rb', line 21

def []=(name, value)
  @hash[name.to_s] = value
end

#saveObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/feet/file_model.rb', line 73

def save
  return 'No valid hash' unless @hash

  # Write JSON to file
  File.open(@filename, 'w') do |f|
    f.write <<~TEMPLATE
      {
          "submitter": "#{@hash['submitter']}",
          "quote": "#{@hash['quote']}",
          "attribution": "#{@hash['attribution']}"
      }
    TEMPLATE
  end

  # Return the hash
  @hash
end