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
# 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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



139
140
141
142
143
144
145
# File 'lib/active_record_mocks/mock/table.rb', line 139

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.




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

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.




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

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

#modelObject


Gives the proper object of the model for you.




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

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.




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

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

#setup?Boolean


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


Returns:

  • (Boolean)


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

def setup?
  @already_setup ? true : false
end

#setup_mocking!Object



95
96
97
98
99
100
101
# File 'lib/active_record_mocks/mock/table.rb', line 95

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.




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

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