Class: Smartmock::Mock

Inherits:
Object show all
Defined in:
lib/smartmock/mock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attribs = {}) ⇒ Mock



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/smartmock/mock.rb', line 38

def initialize(klass, attribs={})
  @attributes = {}
  @stubs = {}
  @dirty = false
  @columns = if Smartmock.active_record?(klass)
    klass.columns.map { |col| col.name.to_s }
  elsif Smartmock.data_mapper?(klass)
    klass.properties.map { |prop| prop.name.to_s }
  else
    []
  end
  @columns << "id"
  attribs.each do |key, val|
    @columns << key.to_s unless @columns.include?(key.to_s)
    self.send("#{key}=".to_sym, val)
  end
  @klass = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Raises:

  • (NameError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smartmock/mock.rb', line 65

def method_missing(meth, *args)
  @columns.each do |col|
    if meth.to_s == col
      return @attributes[col]
    elsif meth.to_s == "#{col}="
      @dirty = true
      @attributes[col] = args.first
      return
    end
  end
  @stubs.each do |stub,value|
    if stub.to_s == meth.to_s
      return value
    end
  end
  raise NameError, "Method #{meth.to_s} is not defined"
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



36
37
38
# File 'lib/smartmock/mock.rb', line 36

def klass
  @klass
end

Instance Method Details

#dirty?Boolean



83
84
85
# File 'lib/smartmock/mock.rb', line 83

def dirty?
  @dirty
end

#idObject



57
58
59
# File 'lib/smartmock/mock.rb', line 57

def id
  @attributes["id"]
end

#saveObject Also known as: save!



87
88
89
# File 'lib/smartmock/mock.rb', line 87

def save
  @dirty = false
end

#stubs=(stubs) ⇒ Object



61
62
63
# File 'lib/smartmock/mock.rb', line 61

def stubs=(stubs)
  @stubs = stubs
end