Changeset 807

Show
Ignore:
Timestamp:
10/31/07 22:06:10 (1 year ago)
Author:
e.@brainspl.at
Message:

use class << self block instead of def self.foo when there are a bunch of class methods being defined. use block form of class_eval instead of string form

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/mixins/render.rb

    r806 r807  
    113113    #     end 
    114114    #   end 
    115     #   
     115    #   
    116116    #  This will first check to see if a index.xml.* template extists, if not 
    117117    #  it will call @people.to_xml (as defined in the add_mime_type method) on the passed 
  • trunk/lib/merb/mixins/responder.rb

    r798 r807  
    1 require 'enumerator' 
     1  require 'enumerator' 
    22 
    33module Merb 
     4  class << self 
     5    # Provides the currently implemented mime types as a hash 
     6    def available_mime_types 
     7      ResponderMixin::Rest::TYPES 
     8    end 
     9     
     10    # Any specific outgoing headers should be included here.  These are not 
     11    # the content-type header but anything in addition to it. 
     12    # +tranform_method+ should be set to a symbol of the method used to 
     13    # transform a resource into this mime type. 
     14    # For example for the :xml mime type an object might be transformed by 
     15    # calling :to_xml, or for the :js mime type, :to_json. 
     16    # If there is no transform method, use nil. 
     17    def add_mime_type(key,transform_method, values,new_response_headers = {}) 
     18      raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Array) 
     19      ResponderMixin::Rest::TYPES.update(key => values) 
     20      add_response_headers!(key, new_response_headers)    
     21      ResponderMixin::Rest::TRANSFORM_METHODS.merge!(key => transform_method)  
     22    end     
     23     
     24    def remove_mime_type(key) 
     25      key == :all ? false : ResponderMixin::Rest::TYPES.delete(key) 
     26    end 
     27     
     28    def mime_transform_method(key) 
     29      ResponderMixin::Rest::TRANSFORM_METHODS[key] 
     30    end 
     31     
     32     
     33    # Adds outgoing headers to a mime type.  This can be done with the Merb.add_mime_type method 
     34    # or directly here.   
     35    # ===Example 
     36    # {{[ 
     37    #   Merb.outgoing_headers!(:xml => { :Encoding => "UTF-8" }) 
     38    # ]}} 
     39    # 
     40    # This method is destructive on any already defined outgoing headers 
     41    def add_response_headers!(key, values = {}) 
     42      raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Hash) 
     43      response_headers[key] = values 
     44    end 
     45     
     46    def response_headers 
     47      ResponderMixin::Rest::RESPONSE_HEADERS 
     48    end 
     49     
     50    # Completely removes any headers set that are additional to the content-type header. 
     51    def remove_response_headers!(key) 
     52      raise ArgumentError unless key.is_a?(Symbol) 
     53      response_headers[key] = {} 
     54    end 
     55     
     56    # Sets the mime types and outgoing headers to their original states 
     57    def reset_default_mime_types! 
     58      available_mime_types.clear 
     59      response_headers.clear 
     60      Merb.add_mime_type(:all,nil,%w[*/*]) 
     61      Merb.add_mime_type(:yaml,:to_yaml,%w[application/x-yaml text/yaml]) 
     62      Merb.add_mime_type(:text,:to_text,%w[text/plain]) 
     63      Merb.add_mime_type(:html,nil,%w[text/html application/xhtml+xml application/html]) 
     64      Merb.add_mime_type(:xml,:to_xml,%w[application/xml text/xml application/x-xml], :Encoding => "UTF-8") 
     65      Merb.add_mime_type(:js,:to_json,%w[ text/javascript  application/javascript application/x-javascript]) 
     66      Merb.add_mime_type(:json,:to_json,%w[application/json  text/x-json  ]) 
     67    end 
    468   
    5   # Provides the currently implemented mime types as a hash 
    6   def self.available_mime_types 
    7     ResponderMixin::Rest::TYPES 
    869  end 
    9    
    10   # Any specific outgoing headers should be included here.  These are not 
    11   # the content-type header but anything in addition to it. 
    12   # +tranform_method+ should be set to a symbol of the method used to 
    13   # transform a resource into this mime type. 
    14   # For example for the :xml mime type an object might be transformed by 
    15   # calling :to_xml, or for the :js mime type, :to_json. 
    16   # If there is no transform method, use nil. 
    17   def self.add_mime_type(key,transform_method, values,new_response_headers = {}) 
    18     raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Array) 
    19     ResponderMixin::Rest::TYPES.update(key => values) 
    20     add_response_headers!(key, new_response_headers)    
    21     ResponderMixin::Rest::TRANSFORM_METHODS.merge!(key => transform_method)  
    22   end     
    23    
    24   def self.remove_mime_type(key) 
    25     key == :all ? false : ResponderMixin::Rest::TYPES.delete(key) 
    26   end 
    27    
    28   def self.mime_transform_method(key) 
    29     ResponderMixin::Rest::TRANSFORM_METHODS[key] 
    30   end 
    31    
    32    
    33   # Adds outgoing headers to a mime type.  This can be done with the Merb.add_mime_type method 
    34   # or directly here.   
    35   # ===Example 
    36   # {{[ 
    37   #   Merb.outgoing_headers!(:xml => { :Encoding => "UTF-8" }) 
    38   # ]}} 
    39   # 
    40   # This method is destructive on any already defined outgoing headers 
    41   def self.add_response_headers!(key, values = {}) 
    42     raise ArgumentError unless key.is_a?(Symbol) && values.is_a?(Hash) 
    43     response_headers[key] = values 
    44   end 
    45    
    46   def self.response_headers 
    47     ResponderMixin::Rest::RESPONSE_HEADERS 
    48   end 
    49    
    50   # Completely removes any headers set that are additional to the content-type header. 
    51   def self.remove_response_headers!(key) 
    52     raise ArgumentError unless key.is_a?(Symbol) 
    53     response_headers[key] = {} 
    54   end 
    55    
    56   # Sets the mime types and outgoing headers to their original states 
    57   def self.reset_default_mime_types! 
    58     available_mime_types.clear 
    59     response_headers.clear 
    60     Merb.add_mime_type(:all,nil,%w[*/*]) 
    61     Merb.add_mime_type(:yaml,:to_yaml,%w[application/x-yaml text/yaml]) 
    62     Merb.add_mime_type(:text,:to_text,%w[text/plain]) 
    63     Merb.add_mime_type(:html,nil,%w[text/html application/xhtml+xml application/html]) 
    64     Merb.add_mime_type(:xml,:to_xml,%w[application/xml text/xml application/x-xml], :Encoding => "UTF-8") 
    65     Merb.add_mime_type(:js,:to_json,%w[ text/javascript  application/javascript application/x-javascript]) 
    66     Merb.add_mime_type(:json,:to_json,%w[application/json  text/x-json  ]) 
    67   end 
    68    
    6970 
    7071  # The ResponderMixin adds methods that help you manage what 
     
    165166    def self.included(base) # :nodoc: 
    166167      base.extend(ClassMethods) 
    167       base.class_eval "class_inheritable_accessor :class_provided_formats" 
     168      base.class_eval { class_inheritable_accessor :class_provided_formats } 
    168169      base.class_provided_formats = [:html] 
    169170    end 
  • trunk/lib/merb/view_context.rb

    r700 r807  
    1818                       @thrown_content 
    1919                       @_route 
    20                        @_benchmarks] 
     20                       @_benchmarks 
     21                       @_provided_formats 
     22                       @_format_value 
     23                       @_content_type 
     24                       @_merb_unmatched 
     25                       @_template_format 
     26                       @_provided_formats 
     27                       @template] 
    2128 
    2229