Module: JSONRecord

Included in:
Base, Base
Defined in:
lib/JSONRecord.rb,
lib/JSONRecord/errors.rb,
lib/JSONRecord/version.rb,
lib/JSONRecord/relation.rb,
lib/JSONRecord/json_hash.rb,
lib/JSONRecord/json_schema.rb,
lib/JSONRecord/meth_missing.rb,
lib/JSONRecord/class_methods.rb,
lib/JSONRecord/module_methods.rb,
lib/JSONRecord/instance_methods.rb,
lib/JSONRecord/active_model_inclusions.rb

Defined Under Namespace

Classes: AttributeError, Base, FileError, JSONHash, JSONRecordError, RecordNotFound, TableError

Constant Summary collapse

VERSION =
"0.0.10"
JSON_TABLES =
{}
COLUMN_ATTRIBUTES =
{}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



2
3
4
5
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
# File 'lib/JSONRecord/meth_missing.rb', line 2

def method_missing(name, *args , &block)
  if name.to_s[/^find_by_id$/]
    find(args)
  elsif name.to_s[/^find_by_\w+/]
    columns = $&.split('_by_').last.split('_and_')
    begin
      raise NoMethodError,"undefined method '#{$&}' for #{self}" unless (columns - self.column_names).empty?
      raise ArgumentError, "wrong number of arguments #{args.size} for #{columns.size}" unless columns.size.eql?(args.size)
      all.select { |record| record.select { |key| columns.include?(key) }.values == args }
    rescue => e
      "#{e.class} :: #{e.message}"
    end
  elsif name.to_s[/^find_all_by_\w+/]
    column = $&.split('_by_').last
     begin
       raise NoMethodError,"undefined method '#{column.last}' for #{self.class}" unless self.column_names.member?(column)
       all.select{|record| record.send(column.intern) == args.first }
     rescue => e
       "#{e.class} :: #{e.message}"
     end
  else
    begin
      unless name.to_s == "new"
        raise NoMethodError,"undefined method '#{name}' for #{self}" unless self.column_names.include? name.to_s
        raise ArgumentError, "wrong number of arguments #{args.size} for 0" unless args.empty?
      end
      return self[name.to_s] if self.column_names.include? name.to_s
    rescue => e
      puts "#{e.class} :: #{e.message}"
    end
  end
end

Instance Method Details

#allObject



47
48
49
# File 'lib/JSONRecord/class_methods.rb', line 47

def all
  json_data.map{|datum| send(:new , datum)}
end

#column_namesObject



55
56
57
# File 'lib/JSONRecord/class_methods.rb', line 55

def column_names
  COLUMN_ATTRIBUTES[table_name].map{|key| key.first }
end

#columnsObject



51
52
53
# File 'lib/JSONRecord/class_methods.rb', line 51

def columns
  Hash[COLUMN_ATTRIBUTES[table_name].map{|i| [i.first,i.last.to_s]}].to_s.gsub("=>",":").gsub(/\"/,"")
end

#connectionObject



26
27
28
# File 'lib/JSONRecord/class_methods.rb', line 26

def connection
  self.class
end

#define_class_and_instance_method(method_name, &block) ⇒ Object



2
3
4
5
# File 'lib/JSONRecord/module_methods.rb', line 2

def define_class_and_instance_method(method_name, &block)
  define_method method_name, &block
  define_singleton_method method_name, &block
end

#find(id) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/JSONRecord/class_methods.rb', line 30

def find(id)
  record = find_record(id.to_i)
  if record.nil? or record.empty?
    raise RecordNotFound,"Record Not Found for #{self}" rescue "#{$!.class}::#{$!.message}"
  else
    record
  end
end

#first(numb = nil) ⇒ Object



39
40
41
# File 'lib/JSONRecord/class_methods.rb', line 39

def first(numb=nil)
  get_terminal_record(:first, numb) unless json_data.empty?
end

#inspectObject



2
3
4
5
6
7
8
# File 'lib/JSONRecord/class_methods.rb', line 2

def inspect
  if path_finder
    self.is_a?(Class) ? "#{self.to_s}(#{columns})" : "#{self.to_s}"
  else
    self.parent_name.eql?("JSONRecord") ? self.to_s : "#{self.to_s}(JSON Table not Found!)"
  end
end

#last(numb = nil) ⇒ Object



43
44
45
# File 'lib/JSONRecord/class_methods.rb', line 43

def last(numb=nil)
  get_terminal_record(:last, numb) unless json_data.empty?
end

#scopedObject Also known as: unscoped



19
20
21
# File 'lib/JSONRecord/class_methods.rb', line 19

def scoped
  self.class
end

#table_nameObject



10
11
12
13
14
15
16
17
# File 'lib/JSONRecord/class_methods.rb', line 10

def table_name
  if self.is_a? Class
    raise AttributeError if self.parents.include? JSONRecord
    self.to_s.pluralize.downcase
  else
    self.class.to_s.pluralize.downcase
  end
end