Class: Medea::JasonBase
- Inherits:
-
Object
- Object
- Medea::JasonBase
- Includes:
- ClassLevelInheritableAttributes
- Defined in:
- lib/medea/jason_base.rb
Direct Known Subclasses
Constant Summary collapse
- HTTP_VERBS =
these verbs are able to be made public
[:GET, :POST, :PUT, :DELETE]
Class Method Summary collapse
-
.resolve(key, mode = :lazy) ⇒ Object
the resolve method takes a key and returns the JasonObject that has that key This is useful when you have the key, but not the class.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add_public(*args) ⇒ Object
- #build_url ⇒ Object
- #delete!(cascade = false) ⇒ Object
- #get_public ⇒ Object
-
#initialize ⇒ JasonBase
constructor
A new instance of JasonBase.
- #jason_etag ⇒ Object
- #jason_key ⇒ Object
- #jason_parent ⇒ Object
- #jason_parent=(parent) ⇒ Object
- #jason_parent_key ⇒ Object
- #jason_parent_key=(value) ⇒ Object
- #jason_parent_list ⇒ Object
- #jason_parent_list=(value) ⇒ Object
- #jason_state ⇒ Object
- #jason_timestamp ⇒ Object
-
#load_from_jasondb ⇒ Object
fetches the data from the JasonDB.
- #permissions_header ⇒ Object
- #remove_public(*args) ⇒ Object
- #set_public(*args) ⇒ Object
Methods included from ClassLevelInheritableAttributes
Constructor Details
#initialize ⇒ JasonBase
Returns a new instance of JasonBase.
36 37 38 39 40 41 |
# File 'lib/medea/jason_base.rb', line 36 def initialize @public = [] if opts[:public] opts[:public].each {|i| @public << i} end end |
Class Method Details
.resolve(key, mode = :lazy) ⇒ Object
the resolve method takes a key and returns the JasonObject that has that key This is useful when you have the key, but not the class
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/medea/jason_base.rb', line 13 def self.resolve(key, mode=:lazy) q = JasonDeferredQuery.new :filters => {:VERSION0 => nil, :FILTER => {:HTTP_X_KEY => key, :HTTP_X_ACTION => :POST}} q.filters[:FILTER] ||= {} q.filters[:FILTER][:HTTP_X_KEY] = key resp = JSON.parse(RestClient.get(q.to_url)) if resp.has_key? "1" #this is the object, figure out its class resp["1"]["POST_TO"] =~ /([^\/]+)\/#{key}/ begin result = Kernel.const_get($1).get_by_key key, :lazy if result["1"].has_key? "CONTENT" result.instance_variable_set(:@__jason_data, result["1"]["CONTENT"]) result.instance_variable_set(:@__jason_state, :stale) end if mode == :eager result.send(:load) end rescue nil end end end |
Instance Method Details
#==(other) ⇒ Object
43 44 45 46 |
# File 'lib/medea/jason_base.rb', line 43 def ==(other) return false if not other.is_a? JasonBase jason_key == other.jason_key end |
#add_public(*args) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/medea/jason_base.rb', line 148 def add_public *args args.reject! do |i| not HTTP_VERBS.include? i end args.uniq! @public += args @public.uniq! @__jason_state = :dirty unless args.empty? end |
#build_url ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/medea/jason_base.rb', line 114 def build_url url = "#{JasonDB::db_auth_url}@0.content?" params = [ "VERSION0", "FILTER=HTTP_X_KEY:#{self.jason_key}", "FILTER=HTTP_X_CLASS:#{self.class.name}" ] url << params.join("&") url end |
#delete!(cascade = false) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/medea/jason_base.rb', line 98 def delete! cascade=false #TODO: Put this into some kind of async method or have JasonDB able to set flags on many records at once #This will be REALLY REALLY slowww! if cascade && (self.class.class_variable_defined? :@@lists) @@lists.keys.each do |list_name| #for each list that I have list = send(list_name) list.each do |item| #remove each item from the list, deleting it if possible list.remove! item, true end end end persist_changes :delete end |
#get_public ⇒ Object
176 177 178 |
# File 'lib/medea/jason_base.rb', line 176 def get_public @public end |
#jason_etag ⇒ Object
58 59 60 |
# File 'lib/medea/jason_base.rb', line 58 def jason_etag @__jason_etag ||= "" end |
#jason_key ⇒ Object
48 49 50 51 52 |
# File 'lib/medea/jason_base.rb', line 48 def jason_key #Generate a random UUID for this object. #since jason urls must start with a letter, we'll use the first letter of the class name @__id ||= "#{self.class.name[0].chr.downcase}#{UUIDTools::UUID::random_create.to_s}" end |
#jason_parent ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/medea/jason_base.rb', line 62 def jason_parent @__jason_parent ||= nil if @__jason_parent == nil && @__jason_parent_key #key is set but parent not? load the parent @__jason_parent = JasonObject.resolve @__jason_parent_key end @__jason_parent end |
#jason_parent=(parent) ⇒ Object
71 72 73 74 |
# File 'lib/medea/jason_base.rb', line 71 def jason_parent= parent @__jason_parent = parent @__jason_parent_key = parent.jason_key end |
#jason_parent_key ⇒ Object
76 77 78 |
# File 'lib/medea/jason_base.rb', line 76 def jason_parent_key @__jason_parent_key ||= nil end |
#jason_parent_key=(value) ⇒ Object
80 81 82 83 84 |
# File 'lib/medea/jason_base.rb', line 80 def jason_parent_key= value @__jason_parent_key = value #reset the parent here? @__jason_parent = nil end |
#jason_parent_list ⇒ Object
86 87 88 |
# File 'lib/medea/jason_base.rb', line 86 def jason_parent_list @__jason_parent_list ||= nil end |
#jason_parent_list=(value) ⇒ Object
90 91 92 |
# File 'lib/medea/jason_base.rb', line 90 def jason_parent_list= value @__jason_parent_list = value end |
#jason_state ⇒ Object
54 55 56 |
# File 'lib/medea/jason_base.rb', line 54 def jason_state @__jason_state end |
#jason_timestamp ⇒ Object
94 95 96 |
# File 'lib/medea/jason_base.rb', line 94 def @__jason_timestamp end |
#load_from_jasondb ⇒ Object
fetches the data from the JasonDB
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/medea/jason_base.rb', line 128 def load_from_jasondb #because this object might be owned by another, we need to search by key. #not passing a format to the query is a shortcut to getting just the object. url = to_url response = RestClient.get url @__jason_data = JSON.parse response @__jason_etag = response.headers[:etag] @__jason_timestamp = response.headers[:timestamp] if response.headers[:http_x_permissions] @public = [] response.headers[:http_x_permissions].match /PUBLIC:\[([A-Z]+)(,[A-Z]+)*\]/ do |m| m.captures.each do |c| @public << c.slice(/[A-Z]+/) end end end @__jason_state = :stale end |
#permissions_header ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/medea/jason_base.rb', line 180 def = {} if self.get_public.any? ["PUBLIC"] = self.get_public.join(",") end result = [] .each do |k, v| result << "#{k}:[#{v}]" end {"X-PERMISSIONS" => "{#{result.join ","}}"} end |
#remove_public(*args) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/medea/jason_base.rb', line 158 def remove_public *args args.reject! do |i| not HTTP_VERBS.include? i end @public -= args @public.uniq! @__jason_state = :dirty unless args.empty? end |
#set_public(*args) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/medea/jason_base.rb', line 167 def set_public *args args.reject! do |i| not HTTP_VERBS.include? i end args.uniq! @__jason_state = :dirty unless args.sort == @public.sort @public = args end |