Class: JSONRecord::Base
- Inherits:
-
JSONHash
show all
- Extended by:
- ActiveModel::Naming, JSONRecord
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, JSONRecord
- Defined in:
- lib/JSONRecord.rb,
lib/JSONRecord/relation.rb,
lib/JSONRecord/json_schema.rb,
lib/JSONRecord/instance_methods.rb,
lib/JSONRecord/active_model_inclusions.rb
Constant Summary
Constants included
from JSONRecord
COLUMN_ATTRIBUTES, JSON_TABLES, VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from JSONRecord
all, column_names, columns, connection, define_class_and_instance_method, find, first, inspect, last, method_missing, scoped, table_name
Methods inherited from JSONHash
#method_missing
Constructor Details
#initialize(args = {}) ⇒ Base
Returns a new instance of Base.
8
9
10
11
|
# File 'lib/JSONRecord.rb', line 8
def initialize(args={})
initialize_columns
args.each{|column| self[column.first.to_s] = column.last }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class JSONRecord
Instance Attribute Details
#all_data ⇒ Object
Returns the value of attribute all_data.
3
4
5
|
# File 'lib/JSONRecord/instance_methods.rb', line 3
def all_data
@all_data
end
|
Class Method Details
.belongs_to(*args) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/JSONRecord/relation.rb', line 22
def self.belongs_to(*args)
begin
args.each do |arg|
raise TableError,"Table Not Found : #{arg}" if JSON_TABLES["#{arg.to_s.pluralize}.json"].nil?
self.class_eval(
<<-METHOD
def #{arg}
puts "#{self} belongs_to #{arg.to_s}"
object = "#{arg.to_s.capitalize}".constantize
object.find(self.#{arg.to_s}_id)
end
METHOD
)
end
rescue => e
puts "#{e.class} : #{e.message}"
end
end
|
.column(name, type = String) ⇒ Object
7
8
9
|
# File 'lib/JSONRecord/json_schema.rb', line 7
def column(name , type=String)
COLUMN_ATTRIBUTES[table_name].push [name.to_s,type]
end
|
.has_many(*args) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/JSONRecord/relation.rb', line 3
def self.has_many(*args)
begin
args.each do |arg|
raise TableError,"Table Not Found : #{arg}" if JSON_TABLES["#{arg}.json"].nil?
self.class_eval(
<<-METHOD
def #{arg}
puts "#{self} has_many #{arg.to_s}"
object = "#{arg.to_s.capitalize.singularize}".constantize
object.find_by_#{self.to_s.downcase}_id(self.id.to_s)
end
METHOD
)
end
rescue => e
puts "#{e.class} : #{e.message}"
end
end
|
Instance Method Details
#destroy ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'lib/JSONRecord/instance_methods.rb', line 67
def destroy
self.all_data = self.class.send(:all)
begin
raise FileError if path_finder.nil?
self.all_data.delete_if{|data| data.id == self.id }
persist_data
end
end
|
#model_name ⇒ Object
31
32
33
|
# File 'lib/JSONRecord/instance_methods.rb', line 31
def model_name
self.class.model_name
end
|
#new_record? ⇒ Boolean
23
24
25
|
# File 'lib/JSONRecord/instance_methods.rb', line 23
def new_record?
self["id"].nil? || self["id"] == 0
end
|
#persisted? ⇒ Boolean
27
28
29
|
# File 'lib/JSONRecord/instance_methods.rb', line 27
def persisted?
new_record? ? false : true
end
|
#save ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/JSONRecord/instance_methods.rb', line 5
def save
self.all_data = self.class.send(:all)
begin
raise FileError if path_finder.nil?
sanitize_input
self.keys.each {|key| raise AttributeError,"Unknown Attribute #{key}" unless column_names.include? key }
if self["id"].nil?
increment_table_id
self.all_data << self
persist_data
else
true end
rescue => e
return "#{e.message} for #{self.class}"
end
end
|
#to_a ⇒ Object
35
36
37
|
# File 'lib/JSONRecord/instance_methods.rb', line 35
def to_a
[self]
end
|
#to_key ⇒ Object
76
77
78
|
# File 'lib/JSONRecord/instance_methods.rb', line 76
def to_key
persisted? ? [self.id] : nil
end
|
#update_attributes(attrs = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/JSONRecord/instance_methods.rb', line 39
def update_attributes(attrs={})
self.all_data = self.class.send(:all)
unless attrs.empty?
sanitize_input(attrs)
attrs.each do |key,value|
begin
raise AttributeError,"Unknown Attribute" unless column_names.include? key.to_s
data_type = self.class::COLUMN_ATTRIBUTES[self.class.to_s.downcase.pluralize].find{|i| i.first == key.to_s }
if data_type.last == Array
if value.is_a?(String)
self[key.to_s] = instance_eval(value)
else
self[key.to_s] = value
end
elsif data_type.last == Integer
self[key.to_s] = value.to_i
elsif data_type.last == String
self[key.to_s] = value.to_s
end
rescue => e
return "#{e.message} : #{key} => #{value}"
end
end
self.all_data[self.id.to_i.pred] = self
persist_data
end
end
|