Class: SimpleMapper::Base
Class Attribute Summary collapse
-
.format ⇒ Object
Returns the value of attribute format.
-
.format_name ⇒ Object
readonly
Returns the value of attribute format_name.
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#original_attributes ⇒ Object
readonly
Returns the value of attribute original_attributes.
-
#original_data ⇒ Object
Returns the value of attribute original_data.
Class Method Summary collapse
- .add_connection_adapter(name_or_adapter, adapter = nil, &block) ⇒ Object
-
.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).
- .connection(name = :default, refresh = false) ⇒ Object
- .connection_adapters ⇒ Object
- .connections ⇒ Object
-
.create(*args) ⇒ Object
new.save.
- .debug! ⇒ Object
- .debug? ⇒ Boolean
- .extract_from(formatted_data) ⇒ Object
- .extract_one(formatted_data, identifier = nil) ⇒ Object
-
.get(*args) ⇒ Object
get.
- .load(data = nil) ⇒ Object
- .persistent? ⇒ Boolean
Instance Method Summary collapse
- #data ⇒ Object
- #data=(data) ⇒ Object (also: #update_data)
-
#delete ⇒ Object
delete.
- #dirty? ⇒ Boolean
-
#formatted_data ⇒ Object
Reads the data from the object for saving back to the persisted store.
-
#initialize(data = nil) ⇒ Base
constructor
A new instance of Base.
-
#instantiate(data) ⇒ Object
Sets the data into the object.
- #persisted? ⇒ Boolean
-
#post(*args) ⇒ Object
sends a post request with self.data.
-
#put(*args) ⇒ Object
sends a put request with self.data.
-
#save ⇒ Object
persisted? ? put : post.
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
.format ⇒ Object
Returns the value of attribute format.
6 7 8 |
# File 'lib/simple_mapper/base.rb', line 6 def format @format end |
.format_name ⇒ Object (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
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
112 113 114 |
# File 'lib/simple_mapper/base.rb', line 112 def identifier @identifier end |
#original_attributes ⇒ Object (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_data ⇒ Object
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_adapters ⇒ Object
10 11 12 |
# File 'lib/simple_mapper/base.rb', line 10 def connection_adapters @connection_adapters ||= Hash.new {|h,k| h[k] = {}} end |
.connections ⇒ Object
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
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
76 77 78 |
# File 'lib/simple_mapper/base.rb', line 76 def persistent? true end |
Instance Method Details
#data ⇒ Object
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 |
#delete ⇒ Object
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
144 145 146 |
# File 'lib/simple_mapper/base.rb', line 144 def dirty? data != original_data end |
#formatted_data ⇒ Object
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.
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
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 |
#save ⇒ Object
persisted? ? put : post
149 150 151 |
# File 'lib/simple_mapper/base.rb', line 149 def save persisted? ? put : post end |