Class: Mocksimile

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

Constant Summary

Constants inherited from Mockumentary::Model

Mockumentary::Model::DUMP_NAME

Class Method Summary collapse

Methods inherited from Mockumentary::Model

build, classes, constantize, evaluate, fake_data, 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

.build_relationships(opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/mockumentary/mocksimile.rb', line 15

def self.build_relationships(opts)
  attr_accessor *(opts.keys)
  @relationships = opts.inject({}) do |result, arr|
    key = arr.first
    klass = generate( arr.last )
    result[key] = lambda { Mockumentary::Collection.new(klass) }

    result
  end
end

.container_nameObject



26
27
28
# File 'lib/mockumentary/mocksimile.rb', line 26

def self.container_name
  "Mocksimile"
end

.defaulterize(opts) ⇒ Object



8
9
10
11
12
13
# File 'lib/mockumentary/mocksimile.rb', line 8

def self.defaulterize(opts)
  @init_defaults = opts[:init]
  @mock_defaults = opts[:mock]
  @save_defaults = opts[:save]
  build_relationships(opts[:relationships])
end

.generate(ar_class_name, opts = nil) ⇒ Object



2
3
4
5
6
# File 'lib/mockumentary/mocksimile.rb', line 2

def self.generate(ar_class_name, opts=nil)
  mock_class = super(ar_class_name)
  mock_class.defaulterize(opts) if opts
  mock_class
end

.load(dir = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mockumentary/mocksimile.rb', line 30

def self.load(dir=nil)
  unless dir
    dir = if defined?(Rails) 
            Rails.root
          elsif defined?(RAILS_ROOT)
            RAILS_ROOT
          else
            ''
          end
  end

  path =  "#{dir}/config/#{DUMP_NAME}"
  unless File.exist?(path)
    raise ArgumentError, "Could not find mockumentary.yml. Please include a path or define RAILS_ROOT"
  end
  config = YAML.load(File.read(path))
  config.each{ |klass_name, options| Mocksimile.generate(klass_name, options) }
end

.releaseObject



49
50
51
52
53
54
55
56
# File 'lib/mockumentary/mocksimile.rb', line 49

def self.release
  if self.to_s == container_name
    classes.map{|c| c.release }
  else
    release_name = self.to_s.gsub(/^#{container_name}::/, '')
    eval "::#{release_name} = #{self} unless defined?(::#{release_name})"
  end
end