Class: SimpleMapper::Base

Inherits:
Object show all
Defined in:
lib/simple_mapper/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ Base

Returns a new instance of Base.



109
110
111
# File 'lib/simple_mapper/base.rb', line 109

def initialize(data=nil)
  self.data = data unless data.nil?
end

Class Attribute Details

.formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/simple_mapper/base.rb', line 6

def format
  @format
end

.format_nameObject (readonly)

Returns the value of attribute format_name.



45
46
47
# File 'lib/simple_mapper/base.rb', line 45

def format_name
  @format_name
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



112
113
114
# File 'lib/simple_mapper/base.rb', line 112

def identifier
  @identifier
end

#original_attributesObject (readonly)

Returns the value of attribute original_attributes.



119
120
121
# File 'lib/simple_mapper/base.rb', line 119

def original_attributes
  @original_attributes
end

#original_dataObject

Returns the value of attribute original_data.



118
119
120
# File 'lib/simple_mapper/base.rb', line 118

def original_data
  @original_data
end

Class Method Details

.add_connection_adapter(name_or_adapter, adapter = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_mapper/base.rb', line 14

def add_connection_adapter(name_or_adapter,adapter=nil,&block)
  if adapter.nil?
    adapter = name_or_adapter
    name = :default
  else
    name = name_or_adapter.to_sym
  end
  # Should complain if the adapter doesn't exist.
  connection_adapters[name][:adapter] = adapter
  require "#{File.dirname(__FILE__)}/adapters/#{adapter}_adapter"
  connection_adapters[name][:init_block] = block if block_given?
  connection_adapters[name][:debug] = @debug
  [name, adapter]
end

.clone_connection(klass, adapter_name = nil) ⇒ Object

This is only here to show you in the docs how to clone a connection connection_adapters = connection_adapters connections = klass.connection(adapter_name)



32
33
34
# File 'lib/simple_mapper/base.rb', line 32

def clone_connection(klass,adapter_name=nil)
  raise 'Not Implemented!'
end

.connection(name = :default, refresh = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_mapper/base.rb', line 50

def connection(name=:default,refresh=false)
  connections[name] = begin
    # Initialize the connection with the connection adapter.
    raise ArgumentError, "Must include :adapter!" unless connection_adapters[name][:adapter].to_s.camelize.length > 0
    adapter = Object.module_eval("::SimpleMapper::#{connection_adapters[name][:adapter].to_s.camelize}Adapter", __FILE__, __LINE__).new
    connection_adapters[name][:init_block].in_context(adapter).call if connection_adapters[name][:init_block].is_a?(Proc)
    adapter.set_headers format.mime_type_headers
    adapter.debug! if connection_adapters[name][:debug]
    adapter
  end if !connections[name] || refresh
  connections[name]
end

.connection_adaptersObject



10
11
12
# File 'lib/simple_mapper/base.rb', line 10

def connection_adapters
  @connection_adapters ||= Hash.new {|h,k| h[k] = {}}
end

.connectionsObject



47
48
49
# File 'lib/simple_mapper/base.rb', line 47

def connections
  @connections ||= {}
end

.create(*args) ⇒ Object

new.save



72
73
74
# File 'lib/simple_mapper/base.rb', line 72

def create(*args)
  new(*args).save
end

.debug!Object



8
# File 'lib/simple_mapper/base.rb', line 8

def debug!; @debug = true end

.debug?Boolean

Returns:

  • (Boolean)


7
# File 'lib/simple_mapper/base.rb', line 7

def debug?; @debug end

.extract_from(formatted_data) ⇒ Object



80
81
82
83
# File 'lib/simple_mapper/base.rb', line 80

def extract_from(formatted_data)
  objs = send("from_#{format_name}".to_sym, formatted_data)
  objs.is_a?(Array) ? objs.collect {|e| e.extended {@persisted = true}} : objs.extended {@persisted = true}
end

.extract_one(formatted_data, identifier = nil) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/simple_mapper/base.rb', line 85

def extract_one(formatted_data, identifier=nil)
  objs = extract_from(formatted_data)
  if objs.is_a?(Array)
    identifier.nil? ? objs.first : objs.reject {|e| e.identifier != identifier}[0]
  else
    identifier.nil? ? objs : (objs.identifier == identifier ? objs : nil)
  end
end

.get(*args) ⇒ Object

get



64
65
66
67
68
69
# File 'lib/simple_mapper/base.rb', line 64

def get(*args)
  adapter = adapter_from_args(*args)
  objs = extract_from(connection(adapter || :default).get(*args))
  objs.is_a?(Array) ? objs.each {|e| e.instance_variable_set(:@adapter, adapter)} : objs.instance_variable_set(:@adapter, adapter) if adapter
  objs
end

.load(data = nil) ⇒ Object



94
95
96
97
98
99
# File 'lib/simple_mapper/base.rb', line 94

def load(data=nil)
  obj = allocate
  obj.original_data = data
  obj.send(:initialize, data)
  obj
end

.persistent?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/simple_mapper/base.rb', line 76

def persistent?
  true
end

Instance Method Details

#dataObject



125
126
127
# File 'lib/simple_mapper/base.rb', line 125

def data
  to_hash
end

#data=(data) ⇒ Object Also known as: update_data



121
122
123
# File 'lib/simple_mapper/base.rb', line 121

def data=(data)
  instantiate(data)
end

#deleteObject

delete



167
168
169
170
171
172
173
174
175
# File 'lib/simple_mapper/base.rb', line 167

def delete
  if self.class.connection(@adapter || :default).delete(identifier)
    @persisted = false
    instance_variable_set('@'+self.class.identifier, nil)
    true
  else
    false
  end
end

#dirty?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/simple_mapper/base.rb', line 144

def dirty?
  data != original_data
end

#formatted_dataObject

Reads the data from the object for saving back to the persisted store. This is provided as a default method, but you can overwrite it in your model.



140
141
142
# File 'lib/simple_mapper/base.rb', line 140

def formatted_data
  send("to_#{self.class.format_name}".to_sym)
end

#instantiate(data) ⇒ Object

Sets the data into the object. This is provided as a default method, but your model can overwrite it any way you want. For example, you could set the data to some other object type, or to a Marshalled storage. The type of data you receive will depend on the format and parser you use. Of course you could make up your own spin-off of one of those, too.

Raises:

  • (TypeError)


133
134
135
136
# File 'lib/simple_mapper/base.rb', line 133

def instantiate(data)
  raise TypeError, "data must be a hash" unless data.is_a?(Hash)
  data.each {|k,v| instance_variable_set("@#{k}".to_sym, v)}
end

#persisted?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/simple_mapper/base.rb', line 177

def persisted?
  !!@persisted && (self.class.identifier.nil? || !instance_variable_get('@'+self.class.identifier).nil?)
end

#post(*args) ⇒ Object

sends a post request with self.data



160
161
162
163
164
# File 'lib/simple_mapper/base.rb', line 160

def post(*args)
  self.data = self.class.extract_one(self.class.connection(@adapter || :default).post(formatted_data)).to_hash
  @persisted = true
  self
end

#put(*args) ⇒ Object

sends a put request with self.data



154
155
156
157
# File 'lib/simple_mapper/base.rb', line 154

def put(*args)
  self.data = self.class.extract_one(self.class.connection(@adapter || :default).put(identifier, formatted_data), identifier).to_hash
  self
end

#saveObject

persisted? ? put : post



149
150
151
# File 'lib/simple_mapper/base.rb', line 149

def save
  persisted? ? put : post
end