Module: ResourceFull::ClassMethods

Defined in:
lib/resource_full/base.rb

Instance Method Summary collapse

Instance Method Details

#exposes(model_class) ⇒ Object

Indicates that the CRUD methods should be called on the given class. Accepts either a class object or the name of the desired model.



98
99
100
101
102
# File 'lib/resource_full/base.rb', line 98

def exposes(model_class)
  remove_retrieval_methods!
  @model_class = model_class.to_s.singularize.camelize.constantize
  alias_retrieval_methods!
end

#identified_by(*args, &block) ⇒ Object

Indicates that this resource is identified by a database column other than the default :id.

TODO This should honor the model’s primary key column but needn’t be bound by it. TODO Refactor this. TODO Improve the documentation.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/resource_full/base.rb', line 64

def identified_by(*args, &block)
  opts = args.extract_options!
  column = args.first
  if !block.nil?
    self.resource_identifier = block
  elsif !column.nil?
    if !opts.empty? && ( opts.has_key?(:if) || opts.has_key?(:unless) )
      if opts[:unless] == :id_numeric
        opts[:unless] = lambda { |id| id =~ /^[0-9]+$/ }
      end
      
      # Negate the condition to generate an :if from an :unless.
      condition = opts[:if] || lambda { |id| not opts[:unless].call(id) }
      
      self.resource_identifier = lambda do |id|
        if condition.call(id)
          column
        else :id end
      end
    else
      self.resource_identifier = column
    end
  else
    raise ArgumentError, "identified_by expects either a block or a column name and some options"
  end
end

#model_classObject

The class of the model exposed by this resource. Derived from the model name. See exposes.



92
93
94
# File 'lib/resource_full/base.rb', line 92

def model_class
  @model_class ||= model_name.camelize.constantize
end

#model_nameObject

The name of the model exposed by this resource. Derived from the name of the controller by default. See exposes.



55
56
57
# File 'lib/resource_full/base.rb', line 55

def model_name
  @model_class ? @model_class.name.demodulize.underscore : self.controller_name.singularize
end

#paginatable?Boolean

Returns true if this resource is paginatable, which is to say, it recognizes and honors the :limit and :offset parameters if present in a query. True by default.

Returns:

  • (Boolean)


51
# File 'lib/resource_full/base.rb', line 51

def paginatable?; paginatable; end

#to_xml(opts = {}) ⇒ Object

Renders the resource as XML.



105
106
107
108
109
110
# File 'lib/resource_full/base.rb', line 105

def to_xml(opts={})
  { :name       => self.controller_name,
    :parameters => self.queryable_params,
    :identifier => self.xml_identifier
  }.to_xml(opts.merge(:root => "resource"))
end