Class: Medea::JasonObject
- Extended by:
- ActiveModel::Naming
- Includes:
- JasonObjectMetaProperties, ActiveModelMethods
- Defined in:
- lib/medea/jasonobject.rb
Constant Summary
Constants inherited from JasonBase
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.
-
#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
-
#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.
- #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
#==, #add_public, #build_url, #delete!, #get_public, #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, #permissions_header, #remove_public, resolve, #set_public
Methods included from ClassLevelInheritableAttributes
Constructor Details
#initialize(initialiser = nil, mode = :eager) ⇒ JasonObject
Returns a new instance of JasonObject.
113 114 115 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 |
# File 'lib/medea/jasonobject.rb', line 113 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)
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/medea/jasonobject.rb', line 87 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.
16 17 18 |
# File 'lib/medea/jasonobject.rb', line 16 def @attachments end |
Class Method Details
.all(opts = nil) ⇒ Object
create a JasonDeferredQuery for all records of this class & with some optional conditions
22 23 24 25 26 27 28 29 30 |
# File 'lib/medea/jasonobject.rb', line 22 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
56 57 58 |
# File 'lib/medea/jasonobject.rb', line 56 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/medea/jasonobject.rb', line 36 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
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/medea/jasonobject.rb', line 72 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/medea/jasonobject.rb', line 61 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 |
#persist_changes(method = :post) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/medea/jasonobject.rb', line 200 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 post_headers.merge! 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 |
#sanitize(hash) ⇒ Object
end “flexihash” access
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/medea/jasonobject.rb', line 100 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
163 164 165 166 167 168 169 170 171 |
# File 'lib/medea/jasonobject.rb', line 163 def save return false if @__jason_state == :stale or @__jason_state == :ghost begin save! return true rescue return false end end |
#save! ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/medea/jasonobject.rb', line 173 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
149 150 151 |
# File 'lib/medea/jasonobject.rb', line 149 def serialise JSON.generate(@__jason_data) end |
#to_public_url ⇒ Object
196 197 198 |
# File 'lib/medea/jasonobject.rb', line 196 def to_public_url to_url :public end |
#to_s ⇒ Object
144 145 146 |
# File 'lib/medea/jasonobject.rb', line 144 def to_s jason_key end |
#to_url(mode = :secure) ⇒ Object
192 193 194 |
# File 'lib/medea/jasonobject.rb', line 192 def to_url mode=:secure "#{JasonDB::db_auth_url mode}#{self.class.name}/#{self.jason_key}" end |
#update_attributes(attributes) ⇒ Object
object persistence methods
155 156 157 158 159 |
# File 'lib/medea/jasonobject.rb', line 155 def update_attributes attributes @__jason_data = sanitize attributes @__jason_state = :dirty unless @__jason_state == :new save end |