Class: Ro::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/ro/model.rb

Direct Known Subclasses

Person

Defined Under Namespace

Classes: List

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Model

Returns a new instance of Model.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ro/model.rb', line 113

def initialize(*args, &block)
  attributes = Map.options_for!(args)

  node = args.detect{|arg| arg.is_a?(Node)}
  model = args.detect{|arg| arg.is_a?(Model)}

  if node.nil? and not model.nil?
    node = model.node
  end

  if node
    @node = node
  else
    path = File.join(prefix, ':new')
    node = Node.new(path)
    @node = node
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



146
147
148
# File 'lib/ro/model.rb', line 146

def method_missing(method, *args, &block)
  node.send(method, *args, &block)
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



111
112
113
# File 'lib/ro/model.rb', line 111

def node
  @node
end

Class Method Details

.[](id) ⇒ Object



66
67
68
# File 'lib/ro/model.rb', line 66

def Model.[](id)
  find(id)
end

.all(*args, &block) ⇒ Object



27
28
29
# File 'lib/ro/model.rb', line 27

def Model.all(*args, &block)
  models_for(nodes)
end

.count(*args, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/ro/model.rb', line 39

def Model.count(*args, &block)
  if args.empty? and block.nil?
    all.size
  else
    where(*args, &block).size
  end
end

.detect(*args, &block) ⇒ Object



35
36
37
# File 'lib/ro/model.rb', line 35

def Model.detect(*args, &block)
  all.detect(*args, &block)
end

.find(id) ⇒ Object



61
62
63
64
# File 'lib/ro/model.rb', line 61

def Model.find(id)
  re = %r/#{ id.to_s.gsub(/[-_]/, '[-_]') }/i
  all.detect{|model| model.id.to_s == id.to_s}
end

.firstObject



53
54
55
# File 'lib/ro/model.rb', line 53

def Model.first
  all.first
end

.lastObject



57
58
59
# File 'lib/ro/model.rb', line 57

def Model.last
  all.last
end

.method_missing(method, *args, &block) ⇒ Object



70
71
72
73
74
75
# File 'lib/ro/model.rb', line 70

def Model.method_missing(method, *args, &block)
  id = method
  model = find(id)
  return model if model
  super
end

.models_for(result) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/ro/model.rb', line 77

def Model.models_for(result)
  case result
    when Array
      List.for(Array(result).flatten.compact.map{|element| new(element)})
    else
      new(result)
  end
end

.nodes(*args, &block) ⇒ Object



23
24
25
# File 'lib/ro/model.rb', line 23

def Model.nodes(*args, &block)
  root.nodes.send(collection)
end

.paginate(*args, &block) ⇒ Object



86
87
88
# File 'lib/ro/model.rb', line 86

def Model.paginate(*args, &block)
  all.paginate(*args, &block)
end

.select(*args, &block) ⇒ Object



31
32
33
# File 'lib/ro/model.rb', line 31

def Model.select(*args, &block)
  all.select(*args, &block)
end

.where(*args, &block) ⇒ Object



47
48
49
50
51
# File 'lib/ro/model.rb', line 47

def Model.where(*args, &block)
  all.select do |model|
    !!model.instance_eval(&block)
  end
end

Instance Method Details

#attributesObject



132
133
134
# File 'lib/ro/model.rb', line 132

def attributes
  @node.attributes
end

#directoryObject



141
142
143
# File 'lib/ro/model.rb', line 141

def directory
  File.join(prefix, id)
end

#List(*args, &block) ⇒ Object



106
107
108
# File 'lib/ro/model.rb', line 106

def List(*args, &block)
  List.new(*args, &block)
end

#prefixObject



137
138
139
# File 'lib/ro/model.rb', line 137

def prefix
  self.class.prefix
end