Class: Medea::JasonBase

Inherits:
Object
  • Object
show all
Includes:
ClassLevelInheritableAttributes
Defined in:
lib/medea/jason_base.rb

Direct Known Subclasses

JasonObject

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassLevelInheritableAttributes

included

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/medea/jason_base.rb', line 10

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



33
34
35
36
# File 'lib/medea/jason_base.rb', line 33

def ==(other)
  return false if not other.is_a? JasonBase
  jason_key == other.jason_key
end

#build_urlObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/medea/jason_base.rb', line 104

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/medea/jason_base.rb', line 88

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

#jason_etagObject



48
49
50
# File 'lib/medea/jason_base.rb', line 48

def jason_etag
  @__jason_etag ||= ""
end

#jason_keyObject



38
39
40
41
42
# File 'lib/medea/jason_base.rb', line 38

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_parentObject



52
53
54
55
56
57
58
59
# File 'lib/medea/jason_base.rb', line 52

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



61
62
63
64
# File 'lib/medea/jason_base.rb', line 61

def jason_parent= parent
  @__jason_parent = parent
  @__jason_parent_key = parent.jason_key
end

#jason_parent_keyObject



66
67
68
# File 'lib/medea/jason_base.rb', line 66

def jason_parent_key
  @__jason_parent_key ||= nil
end

#jason_parent_key=(value) ⇒ Object



70
71
72
73
74
# File 'lib/medea/jason_base.rb', line 70

def jason_parent_key= value
  @__jason_parent_key = value
  #reset the parent here?
  @__jason_parent = nil
end

#jason_parent_listObject



76
77
78
# File 'lib/medea/jason_base.rb', line 76

def jason_parent_list
  @__jason_parent_list ||= nil
end

#jason_parent_list=(value) ⇒ Object



80
81
82
# File 'lib/medea/jason_base.rb', line 80

def jason_parent_list= value
  @__jason_parent_list = value
end

#jason_stateObject



44
45
46
# File 'lib/medea/jason_base.rb', line 44

def jason_state
  @__jason_state
end

#jason_timestampObject



84
85
86
# File 'lib/medea/jason_base.rb', line 84

def jason_timestamp
  @__jason_timestamp
end

#load_from_jasondbObject

fetches the data from the JasonDB



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/medea/jason_base.rb', line 118

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_public]
    @public = response.headers[:http_x_public].split(",")
  end
  @__jason_state = :stale
end