Class: Ro::Model
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/ro/model.rb
Defined Under Namespace
Classes: List
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Model
Returns a new instance of Model.
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/ro/model.rb', line 128
def initialize(*args)
attributes = Map.options_for!(args)
node = args.detect { |arg| arg.is_a?(Node) }
model = args.detect { |arg| arg.is_a?(Model) }
node = model.node if node.nil? and !model.nil?
@node = node
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/ro/model.rb', line 147
def method_missing(method, *args, &block)
begin
node.send(method, *args, &block)
rescue
super
end
end
|
Instance Attribute Details
Returns the value of attribute node.
126
127
128
|
# File 'lib/ro/model.rb', line 126
def node
@node
end
|
Class Method Details
86
87
88
|
# File 'lib/ro/model.rb', line 86
def [](id)
find(id)
end
|
47
48
49
|
# File 'lib/ro/model.rb', line 47
def all
models_for(nodes)
end
|
.collection ⇒ Object
39
40
41
|
# File 'lib/ro/model.rb', line 39
def collection
root.collections[collection_name]
end
|
.collection_name(collection_name = nil) ⇒ Object
33
34
35
36
37
|
# File 'lib/ro/model.rb', line 33
def collection_name(collection_name = nil)
@collection_name = collection_name.to_s if collection_name
@collection_name || default_collection_name
end
|
.count(*args, &block) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/ro/model.rb', line 59
def count(*args, &block)
if args.empty? and block.nil?
all.size
else
where(*args, &block).size
end
end
|
.default_collection_name ⇒ Object
29
30
31
|
# File 'lib/ro/model.rb', line 29
def default_collection_name
name.to_s.split(/::/).last.underscore.pluralize
end
|
.detect(*args, &block) ⇒ Object
55
56
57
|
# File 'lib/ro/model.rb', line 55
def detect(*args, &block)
all.detect(*args, &block)
end
|
81
82
83
84
|
# File 'lib/ro/model.rb', line 81
def find(id)
slug = Slug.for(id)
all.detect { |model| Slug.for(model.id) == slug }
end
|
73
74
75
|
# File 'lib/ro/model.rb', line 73
def first
all.first
end
|
77
78
79
|
# File 'lib/ro/model.rb', line 77
def last
all.last
end
|
.models_for(result) ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/ro/model.rb', line 90
def models_for(result)
case result
when Array
List.for(Array(result).flatten.compact.map { |element| new(element) })
else
new(result)
end
end
|
43
44
45
|
# File 'lib/ro/model.rb', line 43
def nodes
collection.to_a
end
|
.paginate(*args, &block) ⇒ Object
99
100
101
|
# File 'lib/ro/model.rb', line 99
def paginate(*args, &block)
all.paginate(*args, &block)
end
|
21
22
23
|
# File 'lib/ro/model.rb', line 21
def root
@root ||= Ro.root
end
|
.root=(root) ⇒ Object
25
26
27
|
# File 'lib/ro/model.rb', line 25
def root=(root)
@root = Ro::Root.for(root)
end
|
.select(*args, &block) ⇒ Object
51
52
53
|
# File 'lib/ro/model.rb', line 51
def select(*args, &block)
all.select(*args, &block)
end
|
.where(*_args, &block) ⇒ Object
67
68
69
70
71
|
# File 'lib/ro/model.rb', line 67
def where(*_args, &block)
all.select do |model|
!!model.instance_eval(&block)
end
end
|
Instance Method Details
#attributes ⇒ Object
139
140
141
|
# File 'lib/ro/model.rb', line 139
def attributes
@node.attributes
end
|
#List(*args, &block) ⇒ Object
122
123
124
|
# File 'lib/ro/model.rb', line 122
def List(*args, &block)
List.new(*args, &block)
end
|
#persisted? ⇒ Boolean
143
144
145
|
# File 'lib/ro/model.rb', line 143
def persisted?
true
end
|
#respond_to?(method, *args, &block) ⇒ Boolean
155
156
157
|
# File 'lib/ro/model.rb', line 155
def respond_to?(method, *args, &block)
super || node.respond_to?(method)
end
|