Class: Medea::JasonDeferredQuery
- Inherits:
-
Object
- Object
- Medea::JasonDeferredQuery
- Defined in:
- lib/medea/jasondeferredquery.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#result_format ⇒ Object
Returns the value of attribute result_format.
-
#state ⇒ Object
Returns the value of attribute state.
-
#time_limit ⇒ Object
Returns the value of attribute time_limit.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#[](index) ⇒ Object
array access interface.
-
#add_data_filter(property, value) ⇒ Object
end query interface.
- #count ⇒ Object
- #each(&block) ⇒ Object
-
#execute_query(content = true) ⇒ Object
end array interface.
- #first ⇒ Object
- #include?(item) ⇒ Boolean
-
#initialize(opts = {}) ⇒ JasonDeferredQuery
constructor
A new instance of JasonDeferredQuery.
- #last ⇒ Object
- #limit(n) ⇒ Object
- #limit=(n) ⇒ Object
-
#method_missing(name, *args, &block) ⇒ Object
here we will capture: members_of(object) (where object is an instance of a class that this class can be a member of) members_of_<classname>(key) find_by_<property>(value) Will return a JasonDeferredQuery for this class with the appropriate data filter set.
- #since(t) ⇒ Object
- #since=(t) ⇒ Object
- #to_url ⇒ Object
-
#top(n) ⇒ Object
synonym for limit.
Constructor Details
#initialize(opts = {}) ⇒ JasonDeferredQuery
Returns a new instance of JasonDeferredQuery.
8 9 10 11 12 13 14 15 |
# File 'lib/medea/jasondeferredquery.rb', line 8 def initialize opts={} self.type = opts[:class] if opts[:class] self.filters = opts[:filters] if opts[:filters] self.result_format = opts[:format] ? opts[:format] : :search self.time_limit = opts[:time_limit] ? opts[:time_limit] : 0 self.state = :prefetch self.contents = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
here we will capture: members_of(object) (where object is an instance of a class that this class can be a member of) members_of_<classname>(key) find_by_<property>(value) Will return a JasonDeferredQuery for this class with the appropriate data filter set
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/medea/jasondeferredquery.rb', line 24 def method_missing(name, *args, &block) #if we are postfetch, we throw away all our cached results if self.state == :postfetch self.state = :prefetch self.contents = [] end field = name.to_s if field =~ /^members_of$/ #use the type and key of the first arg (being a JasonObject) #args[0] must be a JasonObject (or child) raise ArgumentError, "When looking for members, you must pass a JasonObject" unless args[0].is_a? JasonObject self.filters[:DATA_FILTER] ||= {} self.filters[:DATA_FILTER]["__member_of"] ||= [] self.filters[:DATA_FILTER]["__member_of"] << args[0].jason_key elsif field =~ /^find_by_(.*)$/ #use the property name from the name variable, and the value from the first arg add_data_filter $1, args[0].to_s else #no method! super return end #return self, so that we can chain up query refinements self end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
6 7 8 |
# File 'lib/medea/jasondeferredquery.rb', line 6 def contents @contents end |
#filters ⇒ Object
Returns the value of attribute filters.
6 7 8 |
# File 'lib/medea/jasondeferredquery.rb', line 6 def filters @filters end |
#result_format ⇒ Object
Returns the value of attribute result_format.
6 7 8 |
# File 'lib/medea/jasondeferredquery.rb', line 6 def result_format @result_format end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/medea/jasondeferredquery.rb', line 6 def state @state end |
#time_limit ⇒ Object
Returns the value of attribute time_limit.
6 7 8 |
# File 'lib/medea/jasondeferredquery.rb', line 6 def time_limit @time_limit end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/medea/jasondeferredquery.rb', line 6 def type @type end |
Instance Method Details
#[](index) ⇒ Object
array access interface
116 117 118 119 |
# File 'lib/medea/jasondeferredquery.rb', line 116 def [](index) execute_query unless self.state == :postfetch self.contents[index] end |
#add_data_filter(property, value) ⇒ Object
end query interface
75 76 77 78 |
# File 'lib/medea/jasondeferredquery.rb', line 75 def add_data_filter property, value self.filters[:DATA_FILTER] ||= {} self.filters[:DATA_FILTER][property] = value end |
#count ⇒ Object
126 127 128 129 |
# File 'lib/medea/jasondeferredquery.rb', line 126 def count execute_query unless self.state == :postfetch self.contents.count end |
#each(&block) ⇒ Object
121 122 123 124 |
# File 'lib/medea/jasondeferredquery.rb', line 121 def each(&block) execute_query unless self.state == :postfetch self.contents.each &block end |
#execute_query(content = true) ⇒ Object
end array interface
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/medea/jasondeferredquery.rb', line 148 def execute_query content=true #hit the URL #fill self.contents with :ghost versions of JasonObjects self.contents = [] response = RestClient.get to_url if response.code == 200 #OK result = JSON.parse(response) #results are in a hash, their keys are just numbers result.keys.each do |k| if k =~ /^[0-9]+$/ #this is a result! get the key item = type.new(result[k]["HTTP_X_KEY"], :lazy) if content && result[k].has_key?("CONTENT") && result[k]["CONTENT"] != "" item.instance_variable_set(:@__jason_data, result[k]["CONTENT"]) item.instance_variable_set(:@__jason_state, :stale) end if result[k].has_key?("HTTP_X_PARENT") && result[k]["HTTP_X_PARENT"] != "" item.jason_parent_key = result[k]["HTTP_X_PARENT"] end if result[k].has_key?("TIMESTAMP") item.instance_variable_set :@__jason_timestamp, result[k]["TIMESTAMP"] end self.contents << item end end self.state = :postfetch self.contents elsif response.code == 204 #No Content self.state = :postfetch self.contents else #response wasn't OK or empty! self.state = :prefetch end end |
#first ⇒ Object
143 144 145 |
# File 'lib/medea/jasondeferredquery.rb', line 143 def first self[0] end |
#include?(item) ⇒ Boolean
131 132 133 134 135 136 137 |
# File 'lib/medea/jasondeferredquery.rb', line 131 def include?(item) execute_query unless self.state == :postfetch self.contents.each do |i| return true if i.jason_key == item.jason_key end false end |
#last ⇒ Object
139 140 141 |
# File 'lib/medea/jasondeferredquery.rb', line 139 def last self[-1] end |
#limit(n) ⇒ Object
53 54 55 |
# File 'lib/medea/jasondeferredquery.rb', line 53 def limit n self.filters[:TOP] = n end |
#limit=(n) ⇒ Object
57 58 59 |
# File 'lib/medea/jasondeferredquery.rb', line 57 def limit= n limit n end |
#since(t) ⇒ Object
66 67 68 |
# File 'lib/medea/jasondeferredquery.rb', line 66 def since t self.filters[:SINCE] = t end |
#since=(t) ⇒ Object
70 71 72 |
# File 'lib/medea/jasondeferredquery.rb', line 70 def since= t since t end |
#to_url ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/medea/jasondeferredquery.rb', line 80 def to_url url = "#{JasonDB::db_auth_url}@#{self.time_limit}.#{self.result_format}?" filter_array = [] unsafe = Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") self.filters.each do |name, val| if not val filter_array << URI.escape(name.to_s, unsafe) next else #FILTER's value is a hash (to avoid dupes) #DATA_FILTER's value is a hash if val.is_a? Hash #for each k/v in the hash, we want to add an entry to filter_array val.each do |field ,value| if value.is_a? Array if field == :HTTP_X_PARENT filter_array << URI.escape("#{name.to_s}={#{field}:[#{value.join ","}]}", unsafe) else value.each do |i| filter_array << URI.escape("#{name.to_s}=#{field}:#{i}", unsafe) end end else filter_array << URI.escape("#{name.to_s}=#{field.to_s}:#{value.to_s}", unsafe) end end else filter_array << URI.escape("#{name.to_s}=#{val.to_s}", unsafe) end end end url + filter_array.join("&") end |
#top(n) ⇒ Object
synonym for limit
62 63 64 |
# File 'lib/medea/jasondeferredquery.rb', line 62 def top n limit n end |