base.rb 5.4 KB
Newer Older
1 2 3 4
require 'active_resource/connection'

module ActiveResource
  class Base
5 6 7 8
    # The logger for logging diagnostic and trace information during ARes
    # calls.
    cattr_accessor :logger

9
    class << self
10
      attr_reader :site
J
Jeremy Kemper 已提交
11

12 13
      def site=(site)
        @site = site.is_a?(URI) ? site : URI.parse(site)
14 15
        @connection = nil
        @site
16 17 18 19 20 21
      end

      def connection(refresh = false)
        @connection = Connection.new(site) if refresh || @connection.nil?
        @connection
      end
22

23
      attr_accessor_with_default(:element_name)    { to_s.underscore }
24 25 26
      attr_accessor_with_default(:collection_name) { element_name.pluralize }
      attr_accessor_with_default(:primary_key, 'id')
      
27 28 29
      def prefix(options={})
        default = site.path
        default << '/' unless default[-1..-1] == '/'
30
        self.prefix = default
31 32
        prefix(options)
      end
33

34
      def prefix=(value = '/')
35 36 37 38
        prefix_call = value.gsub(/:\w+/) { |s| "\#{options[#{s}]}" }
        method_decl = %(def self.prefix(options={}) "#{prefix_call}" end)
        eval method_decl
      end
39
      alias_method :set_prefix, :prefix=
40

41 42
      alias_method :set_element_name, :element_name=
      alias_method :set_collection_name, :collection_name=
43 44 45 46

      def element_path(id, options = {})
        "#{prefix(options)}#{collection_name}/#{id}.xml"
      end
47

48 49 50
      def collection_path(options = {})
        "#{prefix(options)}#{collection_name}.xml"
      end
51

52
      alias_method :set_primary_key, :primary_key=
53

54 55
      # Person.find(1) # => GET /people/1.xml
      # StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml
56
      def find(*arguments)
57 58
        scope   = arguments.slice!(0)
        options = arguments.slice!(0) || {}
59 60

        case scope
61 62 63
          when :all   then find_every(options)
          when :first then find_every(options).first
          else             find_single(scope, options)
64 65
        end
      end
66

67 68 69 70
      def delete(id)
        connection.delete(element_path(id))
      end

71 72
      private
        def find_every(options)
73 74
          collection = connection.get(collection_path(options)) || []
          collection.collect! { |element| new(element, options) }
75
        end
76

77 78
        # { :person => person1 }
        def find_single(scope, options)
79
          new(connection.get(element_path(scope, options)), options)
80
        end
81 82 83
    end

    attr_accessor :attributes
84
    attr_accessor :prefix_options
85

86
    def initialize(attributes = {}, prefix_options = {})
87 88
      @attributes = {}
      self.load attributes
89
      @prefix_options = prefix_options
90
    end
91

92
    def new?
93 94 95
      id.nil?
    end

96
    def id
97
      attributes[self.class.primary_key]
98
    end
99

100
    def id=(id)
101
      attributes[self.class.primary_key] = id
102
    end
103

104
    def save
105
      new? ? create : update
106 107 108
    end

    def destroy
109
      connection.delete(element_path)
110
    end
111

112 113
    def to_xml(options={})
      attributes.to_xml({:root => self.class.element_name}.merge(options))
114
    end
115 116 117

    # Reloads the attributes of this object from the remote web service.
    def reload
118 119 120 121 122 123
      self.load self.class.find(id, @prefix_options).attributes
    end

    # Manually load attributes from a hash. Recursively loads collections of
    # resources.
    def load(attributes)
J
Jeremy Kemper 已提交
124
      raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
125 126 127 128 129 130 131
      attributes.each do |key, value|
        @attributes[key.to_s] =
          case value
            when Array
              resource = find_or_create_resource_for_collection(key)
              value.map { |attrs| resource.new(attrs) }
            when Hash
132 133
              resource = find_or_create_resource_for(key)
              resource.new(value)
134 135 136 137 138 139
            when ActiveResource::Base
              value.class.new(value.attributes)
            else
              value.dup rescue value
          end
      end
140 141 142
      self
    end

143 144 145 146
    protected
      def connection(refresh = false)
        self.class.connection(refresh)
      end
147

148
      def update
149
        connection.put(element_path, to_xml)
150
        true
151
      end
152 153

      def create
154
        resp = connection.post(collection_path, to_xml)
155 156
        self.id = id_from_response(resp)
        true
157 158
      end

159 160 161 162 163
      # takes a response from a typical create post and pulls the ID out
      def id_from_response(response)
        response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
      end

164 165 166 167 168 169 170 171
      def element_path(options = nil)
        self.class.element_path(id, options || prefix_options)
      end

      def collection_path(options = nil)
        self.class.collection_path(options || prefix_options)
      end

172 173 174 175 176 177 178 179 180 181 182
    private
      def find_or_create_resource_for_collection(name)
        find_or_create_resource_for(name.to_s.singularize)
      end

      def find_or_create_resource_for(name)
        resource_name = name.to_s.camelize
        resource_name.constantize
      rescue NameError
        resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
        resource.prefix = self.class.prefix
183
        resource.site   = self.class.site
184 185 186
        resource
      end

187 188
      def method_missing(method_symbol, *arguments)
        method_name = method_symbol.to_s
189

190 191 192 193
        case method_name.last
          when "="
            attributes[method_name.first(-1)] = arguments.first
          when "?"
194
            attributes[method_name.first(-1)] == true
195
          else
196
            attributes.has_key?(method_name) ? attributes[method_name] : super
197 198 199
        end
      end
  end
J
Jeremy Kemper 已提交
200
end