Class: Sandwich::Model::Materializer

Inherits:
Object
  • Object
show all
Defined in:
lib/sandwich/model/materializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, model) ⇒ Materializer

Returns a new instance of Materializer.



31
32
33
34
35
36
37
# File 'lib/sandwich/model/materializer.rb', line 31

def initialize(key, model)
  @key, @model = if (key.to_s == model.name.underscore)
                   [:master, model]
                 else
                   [key, model]
                 end
end

Class Method Details

.[](key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/sandwich/model/materializer.rb', line 8

def self.[](key)
  key   = prepare_key(key)
  key_s = key.to_s

  if model = named_fixtures[key]
    new(key, model)
  else
    new(key, key_s.classify.constantize) rescue nil
  end
end

.[]=(key, value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/sandwich/model/materializer.rb', line 19

def self.[]=(key, value)
  key = prepare_key(key)

  raise "Named fixture '#{key}' already exists for class #{self[key]}" if self[key]

  named_fixtures[key] = value
end

.named_fixturesObject



4
5
6
# File 'lib/sandwich/model/materializer.rb', line 4

def self.named_fixtures
  @named_fixtures ||= {}
end

.prepare_key(key) ⇒ Object



27
28
29
# File 'lib/sandwich/model/materializer.rb', line 27

def self.prepare_key(key)
  key.to_s.underscore.to_sym
end

Instance Method Details

#get(args) ⇒ Object



39
40
41
42
43
# File 'lib/sandwich/model/materializer.rb', line 39

def get(args)
  @model.find(:first, :conditions => @model.prepare_args(args, :find)).tap do |result|
    raise "Could not find model for args: #{args.inspect}" if result.nil?
  end
end