Module: ApiLogic::ClassMethods

Defined in:
lib/api_logic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#model_classObject (readonly) Also known as: model

Returns the value of attribute model_class.



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

def model_class
  @model_class
end

#model_collectionObject (readonly)

Returns the value of attribute model_collection.



27
28
29
# File 'lib/api_logic.rb', line 27

def model_collection
  @model_collection
end

#model_singularObject (readonly)

Returns the value of attribute model_singular.



28
29
30
# File 'lib/api_logic.rb', line 28

def model_singular
  @model_singular
end

Instance Method Details

#exposes_model(model_class) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/api_logic.rb', line 46

def exposes_model(model_class)
  # puts "[api_logic] exposing model #{model_class} on #{self.name}"
  
  @model_class = model_class
  @model_collection = model_class.name.tableize
  @model_singular = @model_collection.singularize
  
  before_filter :find_model, :only => [:show, :update, :destroy]
  
public
  
  define_method :index do
    collection = find_models
    instance_variable_set "@#{model_collection}", collection
    respond_with collection
  end
  
  define_method :show do
    respond_with @model
  end
  
  define_method :create do
    @model = create_model
    instance_variable_set "@#{model_singular}", @model
    respond_with @model
  end
  
  define_method :update do
    @model.update_attributes(update_attributes)
    respond_with @model
  end
  
  define_method :destroy do
    @model.destroy
    respond_with @model
  end
  
end

#guess_modelObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/api_logic.rb', line 31

def guess_model
  # puts "[api_logic] guessing model for #{self.name}"
  if self.name =~ /(?:.*::)(.*?)ApiController/
    model_name = $1.singularize
    begin
      exposes_model $1.singularize.constantize
    rescue NameError
      # puts "  there is no model \"#{model_name}\""
    end
  else
    # puts "  the controller does not match the pattern /(?:.*::)(.*?)ApiController/"
  end
end