Module: SimpleMapper::Persistence::ClassMethods
- Defined in:
- lib/simple_mapper/persistence.rb
Instance Attribute Summary collapse
- #entity_name ⇒ Object
-
#format ⇒ Object
Returns the value of attribute format.
-
#format_name ⇒ Object
readonly
Returns the value of attribute format_name.
Instance 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 Works with pagination, provided the last object in the returned array responds to .meta and that meta information includes a total and a url for the next page of results.
- #get_from_url(url, adapter = :default) ⇒ Object
- #load(data = nil) ⇒ Object
- #persistent? ⇒ Boolean
Instance Attribute Details
#entity_name ⇒ Object
56 57 58 |
# File 'lib/simple_mapper/persistence.rb', line 56 def entity_name @entity_name ||= name end |
#format ⇒ Object
Returns the value of attribute format.
15 16 17 |
# File 'lib/simple_mapper/persistence.rb', line 15 def format @format end |
#format_name ⇒ Object (readonly)
Returns the value of attribute format_name.
54 55 56 |
# File 'lib/simple_mapper/persistence.rb', line 54 def format_name @format_name end |
Instance Method Details
#add_connection_adapter(name_or_adapter, adapter = nil, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/simple_mapper/persistence.rb', line 23 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[adapter_name] = connection_adapters[adapter_name]
connections[adapter_name] = klass.connection(adapter_name)
41 42 43 |
# File 'lib/simple_mapper/persistence.rb', line 41 def clone_connection(klass,adapter_name=nil) raise 'Not Implemented!' end |
#connection(name = :default, refresh = false) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/simple_mapper/persistence.rb', line 65 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
19 20 21 |
# File 'lib/simple_mapper/persistence.rb', line 19 def connection_adapters @connection_adapters ||= Hash.new {|h,k| h[k] = {}} end |
#connections ⇒ Object
62 63 64 |
# File 'lib/simple_mapper/persistence.rb', line 62 def connections @connections ||= {} end |
#create(*args) ⇒ Object
new.save
119 120 121 |
# File 'lib/simple_mapper/persistence.rb', line 119 def create(*args) new(*args).save end |
#debug! ⇒ Object
17 |
# File 'lib/simple_mapper/persistence.rb', line 17 def debug!; @debug = true end |
#debug? ⇒ Boolean
16 |
# File 'lib/simple_mapper/persistence.rb', line 16 def debug?; @debug end |
#extract_from(formatted_data) ⇒ Object
127 128 129 130 |
# File 'lib/simple_mapper/persistence.rb', line 127 def extract_from(formatted_data) objs = send(:"from_#{format_name}", 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
132 133 134 135 136 137 138 139 |
# File 'lib/simple_mapper/persistence.rb', line 132 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 Works with pagination, provided the last object in the returned array responds to .meta and that meta information includes a total and a url for the next page of results.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/simple_mapper/persistence.rb', line 81 def get(*args) adapter = adapter_from_args(*args) objs = extract_from(connection(adapter || :default).get(*args)) # puts "#{objs ? objs.length : 0} objects." # puts "Like #{objs[0].inspect}" if objs if objs.is_a?(Array) safe = 1 while(objs[-1].respond_to?(:meta) && objs[-1].['total'] > objs.length && objs[-1].['next']) safe += 1 objs.concat(extract_from(connection(adapter || :default).raw_get(objs[-1].['next']))) break if safe >= 50 # Safeguard: if we do 50 requests we're probably on a runaway. Paginating at 50/page would be 2500 records... end objs.each {|e| e.instance_variable_set(:@adapter, adapter)} if adapter else objs.instance_variable_set(:@adapter, adapter) if adapter end # puts "TOTAL #{objs ? objs.length : 0} objects in all." # puts "Like #{objs[-1].inspect}" if objs objs end |
#get_from_url(url, adapter = :default) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/simple_mapper/persistence.rb', line 102 def get_from_url(url, adapter=:default) objs = extract_from(connection(adapter).raw_get(url)) if objs.is_a?(Array) safe = 1 while(objs[-1].respond_to?(:meta) && objs[-1].['total'] > objs.length && objs[-1].['next']) safe += 1 objs.concat(extract_from(connection(adapter || :default).raw_get(objs[-1].['next']))) break if safe >= 50 # Safeguard: if we do 50 requests we're probably on a runaway. Paginating at 50/page would be 2500 records... end objs.each {|e| e.instance_variable_set(:@adapter, adapter)} if adapter else objs.instance_variable_set(:@adapter, adapter) if adapter end objs end |
#load(data = nil) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/simple_mapper/persistence.rb', line 141 def load(data=nil) obj = allocate obj.send(:initialize, data) obj.original_data = data obj end |
#persistent? ⇒ Boolean
123 124 125 |
# File 'lib/simple_mapper/persistence.rb', line 123 def persistent? true end |