Class: Titanium::Resource

Inherits:
Base
  • Object
show all
Defined in:
lib/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Resource

Constructs a new Resource. Additional options can be provided as shown below.

r = Resource.new("book")
r = Resource.new("book", {:metadata => {:title => "Animal Farm"}})
r = Resource.new("book", {:key => "128dfrit12wywbc", :metadata => {:title => "Animal Farm"}})


31
32
33
34
35
# File 'lib/resource.rb', line 31

def initialize(name, options={})
  @name = name
  @key = options[:key]
  @metadata = options[:metadata]
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



24
25
26
# File 'lib/resource.rb', line 24

def key
  @key
end

#metadataObject

Returns the value of attribute metadata.



25
26
27
# File 'lib/resource.rb', line 25

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/resource.rb', line 24

def name
  @name
end

Class Method Details

.count_by_name(name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/resource.rb', line 89

def self.count_by_name(name)
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    count = db_instance.count_by_name(name)
    return count
  ensure
    db_instance.disconnect
  end
end

.delete_all_by_name(name) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/resource.rb', line 100

def self.delete_all_by_name(name)
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    return db_instance.delete_all_by_name(name)
  ensure
    db_instance.disconnect
  end
end

.find_by_name(name, sort_field = nil, sort_order = nil, limit = nil, offset = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/resource.rb', line 73

def self.find_by_name(name, sort_field=nil, sort_order=nil, limit=nil, offset=nil)
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    resources = []
    result = db_instance.find_by_name_and_conditions(name, nil, nil, sort_field, sort_order, limit, offset)
    result.each do |row|
      key = row.delete('key')
      resources << Resource.new(name, {:key => key, :metadata => row})
    end
    return resources
  ensure
    db_instance.disconnect
  end
end

.find_by_name_and_key(name, key) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/resource.rb', line 110

def self.find_by_name_and_key(name, key)
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    resources = []
    result = db_instance.find_by_name_and_conditions(name, key)
    result.each do |row|
      key = row.delete('key')
      resources << Resource.new(name, {:key => key, :metadata => row})
    end
    return resources
  ensure
    db_instance.disconnect
  end
end

.find_by_name_and_params(name, params, sort_field = nil, sort_order = nil, limit = nil, offset = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/resource.rb', line 126

def self.find_by_name_and_params(name, params, sort_field=nil, sort_order=nil, limit=nil, offset=nil)
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    resources = []
    result = db_instance.find_by_name_and_conditions(name, nil, params, sort_field, sort_order, limit, offset)
    result.each do |row|
      key = row.delete('key')
      resources << Resource.new(name, {:key => key, :metadata => row})
    end
    return resources 
  ensure
    db_instance.disconnect
  end
end

Instance Method Details

#deleteObject



63
64
65
66
67
68
69
70
71
# File 'lib/resource.rb', line 63

def delete
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    return db_instance.delete(@key)
  ensure
    db_instance.disconnect
  end
end

#insertObject



43
44
45
46
47
48
49
50
51
# File 'lib/resource.rb', line 43

def insert
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    return db_instance.insert(@name, @metadata)
  ensure
    db_instance.disconnect
  end
end

#to_jsonObject



37
38
39
40
41
# File 'lib/resource.rb', line 37

def to_json
  json_hash = @metadata.dup
  json_hash[:key] = @key
  json_hash.to_json
end

#updateObject



53
54
55
56
57
58
59
60
61
# File 'lib/resource.rb', line 53

def update
  db_instance = Configuration.db_adapter.new
  begin
    db_instance.connect
    return db_instance.update(@key, @metadata)
  ensure
    db_instance.disconnect
  end
end