Class: ROM::FMP::Dataset
- Inherits:
-
Object
- Object
- ROM::FMP::Dataset
- Includes:
- Rfm::Scope
- Defined in:
- lib/rom/fmp/mini.rb,
lib/rom/fmp/dataset.rb,
lib/rom/fmp/micro02.rb
Constant Summary collapse
- DEFAULT_REQUEST_OPTIONS =
{}
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
include Equalizer.new(:name, :connection).
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
-
#name ⇒ Object
readonly
include Equalizer.new(:name, :connection).
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
-
#table ⇒ Object
readonly
include Equalizer.new(:name, :connection).
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #any(options = {}) ⇒ Object
-
#call ⇒ Object
Combines all queries, sends to FM, returns result in new dataset.
-
#compile_query ⇒ Object
Mixes chained queries together into single query.
- #count(*args) ⇒ Object
- #create(args = {}) ⇒ Object
- #delete(record_id) ⇒ Object
-
#each ⇒ Object
Triggers actual fm action.
-
#find(*args) ⇒ Object
Creates new dataset with existing data & queries, plus new query.
-
#get_results(_method, query = queries, _layout = layout) ⇒ Object
Send method & query to layout, and wrap results in new dataset.
-
#initialize(_layout, _data = [], _queries = []) ⇒ Dataset
constructor
Store layout, data, query in new dataset.
-
#to_a ⇒ Object
Triggers actual fm action.
- #update(record_id, args = {}) ⇒ Object
-
#where(*args) ⇒ Object
Creates new dataset with current args and resultset.
-
#wrap_data(_data = data, _queries = queries, _layout = layout) ⇒ Object
Returns new dataset containing, data, layout, query.
Constructor Details
#initialize(_layout, _data = [], _queries = []) ⇒ Dataset
Store layout, data, query in new dataset.
27 28 29 30 31 32 33 |
# File 'lib/rom/fmp/dataset.rb', line 27 def initialize(name, connection) puts "INITIALIZING DATASET WITH name: #{name} connection: #{connection}" @name = name @connection = connection @table = connection[name.to_s] self end |
Instance Attribute Details
#connection ⇒ Object (readonly)
include Equalizer.new(:name, :connection)
60 61 62 |
# File 'lib/rom/fmp/mini.rb', line 60 def connection @connection end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
24 25 26 |
# File 'lib/rom/fmp/dataset.rb', line 24 def data @data end |
#layout ⇒ Object (readonly)
Returns the value of attribute layout.
24 25 26 |
# File 'lib/rom/fmp/dataset.rb', line 24 def layout @layout end |
#name ⇒ Object (readonly)
include Equalizer.new(:name, :connection)
60 61 62 |
# File 'lib/rom/fmp/mini.rb', line 60 def name @name end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
24 25 26 |
# File 'lib/rom/fmp/dataset.rb', line 24 def queries @queries end |
#table ⇒ Object (readonly)
include Equalizer.new(:name, :connection)
60 61 62 |
# File 'lib/rom/fmp/mini.rb', line 60 def table @table end |
Instance Method Details
#all(options = {}) ⇒ Object
53 54 55 |
# File 'lib/rom/fmp/dataset.rb', line 53 def all(={}) wrap_data(layout.all(DEFAULT_REQUEST_OPTIONS.merge())) end |
#any(options = {}) ⇒ Object
49 50 51 |
# File 'lib/rom/fmp/dataset.rb', line 49 def any(={}) wrap_data(layout.any()) end |
#call ⇒ Object
Combines all queries, sends to FM, returns result in new dataset.
88 89 90 91 |
# File 'lib/rom/fmp/dataset.rb', line 88 def call compiled_query = compile_query wrap_data(compiled_query ? layout.find(*compiled_query) : layout.all(DEFAULT_REQUEST_OPTIONS)) end |
#compile_query ⇒ Object
Mixes chained queries together into single query. Now works with multiple-request queries (using new rfm scope feature). Other ways: consider mixing multi-request queries with intersection: (result1 & result2), or with the new scope feature: query1(scope:query2(scope:query3))
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rom/fmp/dataset.rb', line 97 def compile_query #puts "DATASET COMPILE self #{self}" #puts "DATASET COMPILE queries #{queries}" # Old way: works but doesn't handle fmp compound queries. #query.each_with_object([{},{}]){|x,o| o[0].merge!(x[0] || {}); o[1].merge!(x[1] || {})} # New way: handles compound queries. Reqires ginjo-rfm 3.0.11. return unless queries # This should help introspecting dataset that results from record deletion. TODO: test this. queries.inject {|new_query,scope| apply_scope(new_query, scope)} ##puts "SCOPE INJECTION scope:#{scope} new_query:#{new_query}"; end |
#count(*args) ⇒ Object
57 58 59 60 |
# File 'lib/rom/fmp/dataset.rb', line 57 def count(*args) compiled_query = compile_query compiled_query ? layout.count(*compiled_query) : layout.total_count end |
#create(args = {}) ⇒ Object
62 63 64 |
# File 'lib/rom/fmp/dataset.rb', line 62 def create(args={}) get_results(:create, [args]) unless args.empty? end |
#delete(record_id) ⇒ Object
70 71 72 |
# File 'lib/rom/fmp/dataset.rb', line 70 def delete(record_id) get_results(:delete, record_id) end |
#each ⇒ Object
Triggers actual fm action.
82 83 84 |
# File 'lib/rom/fmp/dataset.rb', line 82 def each(&block) table.all(:max_records=>10).each(&block) end |
#find(*args) ⇒ Object
Creates new dataset with existing data & queries, plus new query
44 45 46 |
# File 'lib/rom/fmp/dataset.rb', line 44 def find(*args) table.find(*args) end |
#get_results(_method, query = queries, _layout = layout) ⇒ Object
Send method & query to layout, and wrap results in new dataset.
115 116 117 |
# File 'lib/rom/fmp/dataset.rb', line 115 def get_results(_method, query=queries, _layout=layout) wrap_data(_layout.send(_method, *query), query, _layout) end |
#to_a ⇒ Object
Triggers actual fm action.
77 78 79 |
# File 'lib/rom/fmp/dataset.rb', line 77 def to_a table.all(:max_records=>10) end |
#update(record_id, args = {}) ⇒ Object
66 67 68 |
# File 'lib/rom/fmp/dataset.rb', line 66 def update(record_id, args={}) get_results(:edit, [record_id, args]) unless args.empty? end |
#where(*args) ⇒ Object
Creates new dataset with current args and resultset. Not lazy. This may not be how rom or sql uses ‘where’. Find out more.
38 39 40 41 |
# File 'lib/rom/fmp/dataset.rb', line 38 def where(*args) #self.class.new(layout, layout.find(*args), args) get_results(:find, args) end |
#wrap_data(_data = data, _queries = queries, _layout = layout) ⇒ Object
Returns new dataset containing, data, layout, query.
110 111 112 |
# File 'lib/rom/fmp/dataset.rb', line 110 def wrap_data(_data=data, _queries=queries, _layout=layout) self.class.new(_layout, _data, _queries) end |