Class: ContentItem
- Inherits:
-
Object
show all
- Defined in:
- lib/content_item.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ ContentItem
55
56
57
58
|
# File 'lib/content_item.rb', line 55
def initialize(attrs = {})
self.attributes = {}
(attrs || {}).each { |key, value| self.attributes[key.to_s] = value.to_s }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/content_item.rb', line 74
def method_missing(name, *arguments)
if arguments.length > 0
attributes[name.to_s.chomp('=')] = arguments.first
elsif attributes.has_key? name.to_s
attributes[name.to_s]
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
2
3
4
|
# File 'lib/content_item.rb', line 2
def attributes
@attributes
end
|
Class Method Details
.connection ⇒ Object
8
9
10
|
# File 'lib/content_item.rb', line 8
def self.connection()
@@connection ||= self.establish_connection('contents')
end
|
.establish_connection(name) ⇒ Object
4
5
6
|
# File 'lib/content_item.rb', line 4
def self.establish_connection(name)
@@connection = Rufus::Tokyo::Table.new("db/#{name}.tdb", "create")
end
|
.find(options) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/content_item.rb', line 31
def self.find(options)
options ||= {}
connection.query { |q|
(options[:conditions] || {}).each { |cond| q.add_condition cond.key, :eq, cond.value }
(options[:order] || []).each {|order| q.order_by order }
}
end
|
.find_all_by_url(url) ⇒ Object
27
28
29
|
# File 'lib/content_item.rb', line 27
def self.find_all_by_url(url)
[self.find_first_by_url(url)]
end
|
.find_first_by_url(url) ⇒ Object
23
24
25
|
# File 'lib/content_item.rb', line 23
def self.find_first_by_url(url)
wrap_result connection[url]
end
|
.method_missing(name, *arguments) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/content_item.rb', line 45
def self.method_missing(name, *arguments)
if name.to_s =~ /^find_all(_by)?_(.+)$/
polymorphic_finder(:all, $2, arguments)
elsif name.to_s =~ /^find_(first_by|by)_(.+)$/
polymorphic_finder(:first, $2, arguments)
else
super
end
end
|
.mock_attr(*syms) ⇒ Object
12
13
14
15
16
17
|
# File 'lib/content_item.rb', line 12
def self.mock_attr(*syms)
syms.each do |sym|
define_method(sym) { "#{attributes[sym.to_s]}" }
define_method("#{sym.to_s}=".to_sym) { |val| attributes[sym.to_s] = val }
end
end
|
.polymorphic_finder(which, name, *arguments) ⇒ Object
39
40
41
42
43
|
# File 'lib/content_item.rb', line 39
def self.polymorphic_finder(which, name, *arguments)
hash = (connection.query {|q| q.add_condition name.to_s, :eq, arguments.first.to_s })
hash = hash.first if which == :first
wrap_result hash
end
|
.wrap_result(attrs) ⇒ Object
19
20
21
|
# File 'lib/content_item.rb', line 19
def self.wrap_result(attrs)
attrs.nil? ? nil : ContentItem.new(attrs)
end
|
Instance Method Details
#save ⇒ Object
60
61
62
63
64
|
# File 'lib/content_item.rb', line 60
def save()
hash = self.attributes.dup
url = hash.delete "url"
ContentItem.connection[url] = hash
end
|
#to_json ⇒ Object
70
71
72
|
# File 'lib/content_item.rb', line 70
def to_json
attributes.to_json
end
|
#to_yaml ⇒ Object
66
67
68
|
# File 'lib/content_item.rb', line 66
def to_yaml
attributes.to_yaml
end
|