Class: ActiveRecordMocks::Mock::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_mocks/mock/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
15
16
17
# File 'lib/active_record_mocks/mock/table.rb', line 9

def initialize(*args, &block)
  @model_methods = []
  @table_name = nil
  @includes = []
  @args = args
  @layout = nil
  @model_name = nil
  @parent_class = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



159
160
161
162
163
164
165
# File 'lib/active_record_mocks/mock/table.rb', line 159

def method_missing(methud, *args, &block)
  model_methods.push({
    :block => block,
    :method => methud,
    :args => args
  })
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/active_record_mocks/mock/table.rb', line 7

def args
  @args
end

#model_methodsObject (readonly)

Returns the value of attribute model_methods.



6
7
8
# File 'lib/active_record_mocks/mock/table.rb', line 6

def model_methods
  @model_methods
end

Instance Method Details

#includes(*incs) ⇒ Object


Allows you to set the files that should be included into the model, you must use t.includes because t.include is already a method on the object you are in.




44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_record_mocks/mock/table.rb', line 44

def includes(*incs)
  if setup? || incs.size == 0
    @includes
  else
    incs.each do |i|
      unless i.blank?
        @includes.push(i)
      end
    end
  end
end

#layout(&block) ⇒ Object


Allows you to set the layout for the table you are building.




60
61
62
# File 'lib/active_record_mocks/mock/table.rb', line 60

def layout(&block)
  setup? || ! block_given? ? @layout ||= nil : @layout = block
end

#modelObject


Gives the proper object of the model for you.




32
33
34
35
36
# File 'lib/active_record_mocks/mock/table.rb', line 32

def model
  if setup?
    Object.const_get(@model_name)
  end
end

#model_name(mname = nil) ⇒ Object


Allows for the setting of or setup of and returning of the name of the model being used, this should not be confused with model which returns the actual object. The model need not match the table and sometimes it won’t if you chose to be that way.




87
88
89
90
91
92
93
94
# File 'lib/active_record_mocks/mock/table.rb', line 87

def model_name(mname = nil)
  if setup? || (! mname && @model_name)
    @model_name
  else
    @model_name = mname ? mname : \
      SecureRandom.hex(10).tr("^a-z", "").capitalize
  end
end

#parent_class(cname = nil) ⇒ Object


Allows the setting of or setup of and returning of the name of the parent class. If this is not customized it will default to ActiveRecord::Base




102
103
104
105
106
107
108
109
# File 'lib/active_record_mocks/mock/table.rb', line 102

def parent_class(cname=nil)
  if setup? || (! cname && @parent_class)
    @parent_class
  else
    @parent_class = cname ? cname.to_s.constantize : \
      ActiveRecord::Base
  end
end

#setup?Boolean


Tells us if we have already setup this model and object so that we don’t keep setting stuff up.


Returns:

  • (Boolean)


24
25
26
# File 'lib/active_record_mocks/mock/table.rb', line 24

def setup?
  @already_setup ? true : false
end

#setup_mocking!Object



111
112
113
114
115
116
117
# File 'lib/active_record_mocks/mock/table.rb', line 111

def setup_mocking!
  if ! setup?
    setup_table!
    setup_model!
    @already_setup = true
  end
end

#table_name(tname = nil) ⇒ Object


Allows the setting of or setuping up of and returning of the name of the table that is being used for the model. If you do not customize this then it will be a tabelized name of the model, the same way that normal active_record would do.




71
72
73
74
75
76
77
78
# File 'lib/active_record_mocks/mock/table.rb', line 71

def table_name(tname = nil)
  if setup? || (! tname && @table_name)
    @table_name
  else
    @table_name = \
      tname ? tname : model_name.to_s.tableize
  end
end