Module: JsonModel::ClassMethods

Defined in:
lib/json_model.rb

Instance Method Summary collapse

Instance Method Details

#allObject



29
30
31
32
33
34
35
36
# File 'lib/json_model.rb', line 29

def all
  entries = []
  for entry in load_entries
    entries << create_object(entry)
  end

  entries
end

#attr_accessor(*vars) ⇒ Object



15
16
17
18
19
# File 'lib/json_model.rb', line 15

def attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end

#attributesObject



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

def attributes
  @attributes
end

#field(name) ⇒ Object



25
26
27
# File 'lib/json_model.rb', line 25

def field(name)
  attr_accessor name
end

#filenameObject



66
67
68
69
# File 'lib/json_model.rb', line 66

def filename
  @filename = "#{ancestors.first}.json".downcase
  File.expand_path(@filename)
end

#filename=(str) ⇒ Object



62
63
64
# File 'lib/json_model.rb', line 62

def filename=(str)
  @filename = str
end

#find(value, key = 'id') ⇒ Object



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

def find(value, key='id')
  entry = all.find{|e| e.send(key) == value }
end

#find_all(hash) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/json_model.rb', line 48

def find_all(hash)
  entries = []
  if hash.instance_of? Hash
    hash.each {| key, value | entries = all.find_all{|e| e.send(key) == value} }
  end

  entries
end

#find_by(hash) ⇒ Object



42
43
44
45
46
# File 'lib/json_model.rb', line 42

def find_by(hash)
  if hash.instance_of? Hash
    hash.each {| key, value | return find(value, key) }
  end
end

#new_idObject



57
58
59
60
# File 'lib/json_model.rb', line 57

def new_id
  ids = all.map(&:id)
  ids.max.to_i + 1
end