Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dao/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.record_to_dao(record, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dao/active_record.rb', line 28

def Base.record_to_dao(record, *args)
# setup for eff'ing madness        
#
  model = record.class
  map = Dao.map
  map[:model] = model.name.underscore
  map[:id] = record.id

# yank out options if they are patently obvious...
#
  if args.size == 2 and args.first.is_a?(Array) and args.last.is_a?(Hash)
    options = Dao.map(args.last)
    args = args.first
  else
    options = nil
  end

# search for options inside args...
#
  if options.nil?
    last = args.last
    options = Dao.map

    if last.is_a?(Hash)
      last = Dao.map(last)
      keys = %w( include includes with )
      if keys.any?{|key| last.has_key?(key)}
        options.update(last)
        args.pop
      end
    end
  end

# refine the list with includes iff passed as options
#
  if options.has_key?(:include) or options.has_key?(:includes) or options.has_key?(:with)
    args.replace(model.to_dao) if args.empty?
    args.push(options[:include]) if options[:include]
    args.push(options[:includes]) if options[:includes]
    args.push(options[:with]) if options[:with]
  end

# take passed in args or model defaults
#
  list = args.empty? ? model.to_dao : args

# okay - go!
#
  list.each do |attr|
    if attr.is_a?(Array)
      related, *argv = attr
      v = record.send(related)
      value = v.respond_to?(:to_dao) ? v.to_dao(*argv) : v
      map[related] = value
      next
    end

    if attr.is_a?(Hash)
      attr.each do |related, argv|
        v = record.send(related)
        value = v.respond_to?(:to_dao) ? v.to_dao(*argv) : v
        map[related] = value
      end
      next
    end

    value = record.send(attr)

    if value.respond_to?(:to_dao)
      map[attr] = value.to_dao
      next
    end

    if value.is_a?(Array)
      map[attr] = value.map{|val| val.respond_to?(:to_dao) ? val.to_dao : val}
      next
    end

    map[attr] = value
  end

  if map.has_key?(:_id) and not map.has_key?(:id)
    map[:id] = map[:_id]
  end

  map
end

.to_dao(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dao/active_record.rb', line 12

def Base.to_dao(*args)
  if args.first.is_a?(Base)
    record_to_dao(record = args.shift, *args)
  else
    @to_dao ||= (
      names = column_names ### + reflect_on_all_associations.map(&:name)
    )
    @to_dao = Array(args) unless args.empty?
    @to_dao
  end
end

.to_dao=(*args) ⇒ Object



24
25
26
# File 'lib/dao/active_record.rb', line 24

def Base.to_dao=(*args)
  to_dao(*args)
end

Instance Method Details

#to_dao(*args) ⇒ Object



116
117
118
119
120
# File 'lib/dao/active_record.rb', line 116

def to_dao(*args)
  record = self
  model = record.class
  model.record_to_dao(record, *args)
end