Module: Loquor::ResourceMock

Defined in:
lib/loquor/resource_mock.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(x) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/loquor/resource_mock.rb', line 4

def self.extended(x)
  x.class_eval do
    def method_missing(name, *args)
      if name[-1] == "="
        if self.class.attributes.keys.map{ |k| :"#{k}=" }.include?(name)
          attr = name.to_s[0..-2].to_sym
          @data[attr] = args[0]
        else
          raise NameError.new("undefined local variable or method '#{name}' for #{self.class.name}")
        end
      else
        super(name, *args)
      end
    end
  end
end

Instance Method Details

#attributesObject



21
22
23
# File 'lib/loquor/resource_mock.rb', line 21

def attributes
  @attributes
end

#attributes=(attrs) ⇒ Object



25
26
27
# File 'lib/loquor/resource_mock.rb', line 25

def attributes=(attrs)
  @attributes = attrs
end

#create(*attrs) ⇒ Object



49
50
51
# File 'lib/loquor/resource_mock.rb', line 49

def create(*attrs)
  self.new(*attrs)
end

#custom_sample(attrs = {}) ⇒ Object



37
38
39
# File 'lib/loquor/resource_mock.rb', line 37

def custom_sample(attrs = {})
  self.new(attrs)
end

#find(id) ⇒ Object



41
42
43
# File 'lib/loquor/resource_mock.rb', line 41

def find(id)
  self.new(attributes.merge(id: id))
end

#sample(overrides = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/loquor/resource_mock.rb', line 29

def sample(overrides = {})
  arbitary_attributes = (overrides.keys - attributes.keys)
  unless arbitary_attributes.empty?
    raise NameError.new("undefined local variable or method '#{arbitary_attributes.first}' for #{self.name}")
  end
  self.new(attributes.merge(overrides))
end

#update(*attrs) ⇒ Object



53
54
55
# File 'lib/loquor/resource_mock.rb', line 53

def update(*attrs)
  true
end

#where(*args) ⇒ Object



45
46
47
# File 'lib/loquor/resource_mock.rb', line 45

def where(*args)
  [ find(1), find(2) ]
end