Class: Garden::Mediators::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/garden/mediators.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namish) ⇒ Table

Returns a new instance of Table.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/garden/mediators.rb', line 26

def initialize namish
  
  @name = Table.parse_table_name namish
  
  # Get the ActiveRecord model from the tablename.
  begin
    @clazz = @name.classify.constantize
    raise "Class #{@clazz.to_s} is not an ActiveRecord subclass." unless @clazz.new.is_a?(ActiveRecord::Base)
  rescue Exception => e
    puts " ** Could not derive ActiveRecord model from the provided name: #{namish}. Exception: #{e.message}"
  end
  
  @instance = @clazz.new
  
end

Class Method Details

.get_instance(table_name, id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/garden/mediators.rb', line 8

def self.get_instance table_name, id

  # puts "Find #{id} from #{table_name}"
  # puts id.class.to_s

  begin
    clazz = table_name.to_s.classify.constantize
    raise "Class #{clazz.to_s} is not an ActiveRecord subclass." unless clazz.new.is_a?(ActiveRecord::Base)
    instance = clazz.find id.to_s
    return instance
  rescue Exception => e
    puts "Could not find #{id} from table #{table_name}: #{e.message}"
    return nil
  end
end

.parse_table_name(namish) ⇒ Object



5
6
7
# File 'lib/garden/mediators.rb', line 5

def self.parse_table_name namish
  namish.parameterize.underscore
end

Instance Method Details

#create_instance(attributes) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/garden/mediators.rb', line 60

def create_instance attributes
  relationships = @relationships
  

  attributes = parse_row_relationships attributes, relationships

  instance = @clazz.new

  rejected = {}
  attributes.each_key do |key|
    rejected[key] = attributes.delete(key) if !instance.attributes.include?(key.to_s) && !relationships.include?(key.to_s)
  end

  instance.attributes = attributes

  # instance.id = rejected[:id] if rejected.key?(:id)

  # puts "Valid? #{instance.valid?}"
  # puts instance.errors.to_s

  valid = instance.valid?

  if instance.valid?
    instance.save!
    puts ".Saved instance: #{@clazz} #{instance.to_param}"
  else
    puts "Invalid instance."
    puts "#{@name}:"
    puts " - Valid attributes: #{attributes.keys}"
    puts " - Invalid attributes: #{rejected.keys}"
    puts " - Association attributes: #{relationships}"
    puts " - #{instance.errors}"
  end
end

#parse_headers(array) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/garden/mediators.rb', line 46

def parse_headers array
  @headers = array.map { |header| header.to_s.parameterize.underscore }
  @relationships = []
  
  @headers.each do |header|
    @relationships.push header if @clazz.reflections.keys.include?(header.to_sym)
  end
end

#parse_row_relationships(hash, relationships) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/garden/mediators.rb', line 95

def parse_row_relationships hash, relationships
  relationships.each do |r|
    # puts "Parsing row relationship: #{hash[r.to_sym]}"
    instance = get_instance r, hash[r.to_sym]
    hash[r.to_sym] = instance
  end
  hash
end

#relationshipsObject



56
57
58
# File 'lib/garden/mediators.rb', line 56

def relationships
  @relationships
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/garden/mediators.rb', line 42

def valid?
  @clazz != nil
end