Class: Medea::JasonObject
- Extended by:
- ActiveModel::Naming
- Includes:
- JasonObjectMetaProperties, ActiveModelMethods
- Defined in:
- lib/medea/jasonobject.rb
Constant Summary collapse
- HTTP_VERBS =
these verbs are able to be made public
[:GET, :POST, :PUT, :DELETE]
Instance Attribute Summary collapse
-
#attachments ⇒ Object
Returns the value of attribute attachments.
Class Method Summary collapse
-
.all(opts = nil) ⇒ Object
create a JasonDeferredQuery for all records of this class & with some optional conditions.
-
.get_by_key(key, mode = :eager) ⇒ Object
returns the JasonObject by directly querying the URL if mode is :lazy, we return a GHOST, if mode is :eager, we return a STALE JasonObject.
-
.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) find_by_<property>(value) Will return a JasonDeferredQuery for this class with the appropriate data filter set.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
“flexihash” access interface.
- #add_public(*args) ⇒ Object
-
#initialize(initialiser = nil, mode = :eager) ⇒ JasonObject
constructor
A new instance of JasonObject.
-
#method_missing(name, *args, &block) ⇒ Object
The “Magic” component of candy (github.com/SFEley/candy), repurposed to make this a “weak object” that can take any attribute.
- #persist_changes(method = :post) ⇒ Object
- #remove_public(*args) ⇒ Object
-
#sanitize(hash) ⇒ Object
end “flexihash” access.
-
#save ⇒ Object
POSTs the current values of this object back to JasonDB on successful post, sets state to STALE and updates eTag.
- #save! ⇒ Object
-
#serialise ⇒ Object
converts the data hash (that is, @__jason_data) to JSON format.
- #set_public(*args) ⇒ Object
- #to_public_url ⇒ Object
- #to_s ⇒ Object
- #to_url(mode = :secure) ⇒ Object
-
#update_attributes(attributes) ⇒ Object
object persistence methods.
Methods included from JasonObjectMetaProperties
Methods included from ActiveModelMethods
#errors, #persisted?, #to_model, #valid?
Methods inherited from JasonBase
#==, #build_url, #delete!, #jason_etag, #jason_key, #jason_parent, #jason_parent=, #jason_parent_key, #jason_parent_key=, #jason_parent_list, #jason_parent_list=, #jason_state, #jason_timestamp, #load_from_jasondb, resolve
Methods included from ClassLevelInheritableAttributes
Constructor Details
#initialize(initialiser = nil, mode = :eager) ⇒ JasonObject
Returns a new instance of JasonObject.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/medea/jasonobject.rb', line 116 def initialize initialiser = nil, mode = :eager @attachments = {} if opts[:attachments] opts[:attachments].each do |k| @attachments[k] = nil end end @public = [] if opts[:public] opts[:public].each {|i| @public << i} end if initialiser if initialiser.is_a? Hash @__jason_state = :new @__jason_data = sanitize initialiser else @__id = initialiser if mode == :eager load_from_jasondb else @__jason_state = :ghost end end else @__jason_state = :new @__jason_data = {} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
The “Magic” component of candy (github.com/SFEley/candy), repurposed to make this a “weak object” that can take any attribute. Assigning any attribute will add it to the object’s hash (and then be POSTed to JasonDB on the next save)
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/medea/jasonobject.rb', line 90 def method_missing(name, *args, &block) load_from_jasondb if @__jason_state == :ghost field = name.to_s if field =~ /(.*)=$/ # We're assigning self[$1] = args[0] elsif field =~ /(.*)\?$/ # We're asking (self[$1] ? true : false) else self[field] end end |
Instance Attribute Details
#attachments ⇒ Object
Returns the value of attribute attachments.
19 20 21 |
# File 'lib/medea/jasonobject.rb', line 19 def @attachments end |
Class Method Details
.all(opts = nil) ⇒ Object
create a JasonDeferredQuery for all records of this class & with some optional conditions
25 26 27 28 29 30 31 32 33 |
# File 'lib/medea/jasonobject.rb', line 25 def JasonObject.all(opts=nil) q = JasonDeferredQuery.new :class => self, :filters => {:VERSION0 => nil, :FILTER => {:HTTP_X_CLASS => self, :HTTP_X_ACTION => :POST}} if opts q.limit = opts[:limit] if opts[:limit] q.since = opts[:since] if opts[:since] end q end |
.get_by_key(key, mode = :eager) ⇒ Object
returns the JasonObject by directly querying the URL if mode is :lazy, we return a GHOST, if mode is :eager, we return a STALE JasonObject
59 60 61 |
# File 'lib/medea/jasonobject.rb', line 59 def self.get_by_key(key, mode=:eager) return self.new key, mode end |
.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) find_by_<property>(value) Will return a JasonDeferredQuery for this class with the appropriate data filter set
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/medea/jasonobject.rb', line 39 def JasonObject.method_missing(name, *args, &block) q = all if name.to_s =~ /^members_of$/ #use the type and key of the first arg (being a JasonObject) return q.members_of args[0] elsif name.to_s =~ /^find_by_(.*)$/ #use the property name from the name variable, and the value from the first arg q.add_data_filter $1, args[0] return q else #no method! super end end |
Instance Method Details
#[](key) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/medea/jasonobject.rb', line 75 def [](key) if @attachments.keys.include? key.to_sym if not @attachments[key.to_sym] #retrieve the JasonBlob for this key @attachments[key.to_sym] = Medea::JasonBlob.new({:parent => self, :name => key}) end return @attachments[key.to_sym] end @__jason_data[key] end |
#[]=(key, value) ⇒ Object
“flexihash” access interface
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/medea/jasonobject.rb', line 64 def []=(key, value) if @attachments.keys.include? key.to_sym @attachments[key.to_sym] = Medea::JasonBlob.new({:parent => self, :name => key, :content => value}) return end @__jason_data ||= {} @__jason_state = :dirty if jason_state == :stale @__jason_data[key] = value end |
#add_public(*args) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/medea/jasonobject.rb', line 195 def add_public *args args.reject! do |i| not HTTP_VERBS.include? i end @public += args @public.uniq! end |
#persist_changes(method = :post) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/medea/jasonobject.rb', line 227 def persist_changes method = :post payload = self.serialise post_headers = { :content_type => 'application/json', "X-KEY" => self.jason_key, "X-CLASS" => self.class.name #also want to add the eTag here! #may also want to add any other indexable fields that the user specifies? } post_headers["IF-MATCH"] = @__jason_etag if @__jason_state == :dirty post_headers["X-PARENT"] = self.jason_parent.jason_key if self.jason_parent post_headers["X-LIST"] = self.jason_parent_list if self.jason_parent_list if opts[:located] #set the location headers if geohash? post_headers["X-GEOHASH"] = geohash end if latitude? && longitude? post_headers["X-LATITUDE"] = latitude post_headers["X-LONGITUDE"] = longitude end end if @public.any? post_headers["X-PUBLIC"] = @public.join(",") end url = to_url() #puts "Saving to #{url}" if method == :post response = RestClient.post(url, payload, post_headers) elsif method == :delete response = RestClient.delete(url, post_headers) else raise "Unknown method '#{method.to_s}'" end if response.code == 201 #save successful! #store the new eTag for this object #puts response.raw_headers @__jason_etag = response.headers[:Etag] @__jason_timestamp = response.headers[:timestamp] else raise "#{method.to_s.upcase} failed! Could not persist changes" end @__jason_state = :stale end |
#remove_public(*args) ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/medea/jasonobject.rb', line 203 def remove_public *args args.reject! do |i| not HTTP_VERBS.include? i end @public -= args @public.uniq! end |
#sanitize(hash) ⇒ Object
end “flexihash” access
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/medea/jasonobject.rb', line 103 def sanitize hash #remove the keys in hash that aren't allowed forbidden_keys = ["jason_key", "jason_state", "jason_parent", "jason_parent_key", "jason_parent_list"] hash.delete_if { |k,v| forbidden_keys.include? k } result = {} hash.each { |k, v| result[k.to_s] = v } result end |
#save ⇒ Object
POSTs the current values of this object back to JasonDB on successful post, sets state to STALE and updates eTag
166 167 168 169 170 171 172 173 174 |
# File 'lib/medea/jasonobject.rb', line 166 def save return false if @__jason_state == :stale or @__jason_state == :ghost begin save! return true rescue return false end end |
#save! ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/medea/jasonobject.rb', line 176 def save! @attachments.each do |k, v| if v v.save! end end #no changes? no save! return if @__jason_state == :stale or @__jason_state == :ghost #validations if self.class.owned #the parent object needs to be defined! raise "#{self.class.name} cannot be saved without setting a parent and list!" unless self.jason_parent && self.jason_parent_list end persist_changes :post end |
#serialise ⇒ Object
converts the data hash (that is, @__jason_data) to JSON format
152 153 154 |
# File 'lib/medea/jasonobject.rb', line 152 def serialise JSON.generate(@__jason_data) end |
#set_public(*args) ⇒ Object
211 212 213 214 215 216 217 |
# File 'lib/medea/jasonobject.rb', line 211 def set_public *args args.reject! do |i| not HTTP_VERBS.include? i end @public = args @public.uniq! end |
#to_public_url ⇒ Object
223 224 225 |
# File 'lib/medea/jasonobject.rb', line 223 def to_public_url to_url :public end |
#to_s ⇒ Object
147 148 149 |
# File 'lib/medea/jasonobject.rb', line 147 def to_s jason_key end |
#to_url(mode = :secure) ⇒ Object
219 220 221 |
# File 'lib/medea/jasonobject.rb', line 219 def to_url mode=:secure "#{JasonDB::db_auth_url mode}#{self.class.name}/#{self.jason_key}" end |
#update_attributes(attributes) ⇒ Object
object persistence methods
158 159 160 161 162 |
# File 'lib/medea/jasonobject.rb', line 158 def update_attributes attributes @__jason_data = sanitize attributes @__jason_state = :dirty unless @__jason_state == :new save end |