Class: Mockery

Inherits:
Mockumentary::Model show all
Defined in:
lib/mockumentary/mockery.rb

Constant Summary

Constants inherited from Mockumentary::Model

Mockumentary::Model::DUMP_NAME

Class Method Summary collapse

Methods inherited from Mockumentary::Model

classes, constantize, evaluate, fake_data, generate, init_defaults, init_opts, #initialize, mock, mock!, mock_defaults, mock_opts, overrides, relationships, #save, save_defaults, save_fields, save_opts, uid

Constructor Details

This class inherits a constructor from Mockumentary::Model

Class Method Details

.ar_classObject



2
3
4
# File 'lib/mockumentary/mockery.rb', line 2

def self.ar_class
  @ar_class ||= infer_ar_class
end

.ar_class=(klass) ⇒ Object



6
7
8
9
10
# File 'lib/mockumentary/mockery.rb', line 6

def self.ar_class=(klass)
  @ar_class = klass
  introspect if klass
  klass
end

.build(klass) ⇒ Object



20
21
22
23
# File 'lib/mockumentary/mockery.rb', line 20

def self.build(klass)
  super
  class_eval "#{container_name}::#{klass}.ar_class = ::#{klass}"
end

.container_nameObject



12
13
14
# File 'lib/mockumentary/mockery.rb', line 12

def self.container_name
  "Mockery"
end

.data_dumpObject



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
# File 'lib/mockumentary/mockery.rb', line 59

def self.data_dump
  classes.inject({}) do |result, klass|
    init = klass.init_defaults.dup
    init.merge!(klass.overrides[:init]) if klass.overrides && klass.overrides[:init]
    
    save = klass.save_defaults.dup
    save.merge!(klass.overrides[:save]) if klass.overrides && klass.overrides[:save]

    mock = klass.mock_defaults.dup
    mock.merge!(klass.overrides[:mock]) if klass.overrides && klass.overrides[:mock]

    relationships = klass.relationships.inject({}) do |result, array|
      key = array.first
      value = array.last # a lambda 
      relationship_type = value.call.type.to_s.gsub(/^Mockery::/, '')        
      result[key] = relationship_type
      result
    end

    result[klass.ar_class.to_s] = {
      :init => init,
      :save => save,
      :mock => mock,
      :relationships => relationships
    }
    result
  end
end

.dump(dir = "#{Rails.root}/config") ⇒ Object



53
54
55
56
57
# File 'lib/mockumentary/mockery.rb', line 53

def self.dump dir = "#{Rails.root}/config"
  File.open("#{dir}/#{DUMP_NAME}", 'w') do |f|
    f.write(YAML.dump(data_dump))
  end
end

.infer_ar_classObject



16
17
18
# File 'lib/mockumentary/mockery.rb', line 16

def self.infer_ar_class
  self.to_s.gsub(/^#{container_name}/, '').constantize
end

.introspectObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mockumentary/mockery.rb', line 32

def self.introspect
  reset_defaults

  # introspect columns
  ar_class.columns.each do |c|
    name = c.name.to_sym
    if save_fields.include?(name) 
      save_defaults[name] = c.type if name != :id
    else
      mock_defaults[name] = c.type
    end 
  end

  # introspect relationships
  ar_class.reflections.each do |name, reflection|
    attr_accessor name
    class_name = reflection.options[:class_name] || name.to_s.classify
    relationships[name] = lambda { Mockumentary::Collection.new(build(class_name)) } if reflection.collection?
  end
end

.reset_defaultsObject



25
26
27
28
29
30
# File 'lib/mockumentary/mockery.rb', line 25

def self.reset_defaults
  @relationships = nil
  @save_defaults = nil
  @mock_defaults = nil
  @uid = nil
end