Class: Ygg::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/ygg/bck/model.rb

Direct Known Subclasses

Link

Constant Summary collapse

@@validator =
[/(.*)/, [:index]]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Model

Returns a new instance of Model.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ygg/bck/model.rb', line 6

def initialize(*args)
  Ygg.data[self.class] ||= []
  Ygg.data[self.class] << self
  @created_at ||= Time.now    if respond_to? :created_at
  @updated_at ||= Time.now    if respond_to? :updated_at
  
  if data = args.shift
    if data.kind_of? Hash
      data.each {|k,v| self.send(:"#{k}=", v) }
    elsif data.kind_of? String
      validator = self.class.validator
      if matches = data.match(validator[0])
        validator[1].each_index do |i|
          self.send :"_#{validator[1][i]}=", matches[i+1]
        end
      else
        raise Ygg::InvalidInitializor, "#{data} does not match the validation regexp for #{self.class}"
      end
    end
  end
end

Class Method Details

.allObject

Class methods : Shorthands



97
# File 'lib/ygg/bck/model.rb', line 97

def self.all; Ygg.data[self.class]; end

.key(*args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ygg/bck/model.rb', line 103

def self.key(*args)
  attr_reader *args
  args.each do |arg|
    class_eval <<-_EOE
      def _#{arg}=(v)
        @#{arg} = v
      end
      
      def _#{arg}_hook=(v)
        @updated_at = Time.now if respond_to? :updated_at
      end
      
      def #{arg}=(v)
        self._#{arg}_hook = v
        self._#{arg} = v
      end
    _EOE
  end
end

.timestamps!Object

Class methods : Metamethods



101
# File 'lib/ygg/bck/model.rb', line 101

def self.timestamps!; attr_accessor :created_at, :updated_at; end

.validatorObject



98
# File 'lib/ygg/bck/model.rb', line 98

def self.validator; @@validator; end

Instance Method Details

#&(res) ⇒ Object

Instance methods : Links



40
41
42
# File 'lib/ygg/bck/model.rb', line 40

def &(res)
  Link.new :source => self, :target => res
end

#*(arg) ⇒ Object



45
# File 'lib/ygg/bck/model.rb', line 45

def *(arg);           links arg;                    end

#<(arg) ⇒ Object



44
# File 'lib/ygg/bck/model.rb', line 44

def <(arg);           sources arg;                  end

#>(arg) ⇒ Object



43
# File 'lib/ygg/bck/model.rb', line 43

def >(arg);           targets arg;                  end

#_finalize!Object

Instance methods : Defaults



29
# File 'lib/ygg/bck/model.rb', line 29

def _finalize!; finalize! if respond_to? :finalize!; end

#_idObject

Destroy all Links



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

def _id
  Ygg.data[self.class].index self
end

#_loading!Object



30
# File 'lib/ygg/bck/model.rb', line 30

def _loading!; loading! if respond_to? :loading!; end

#destroyObject



31
32
33
34
# File 'lib/ygg/bck/model.rb', line 31

def destroy
  Ygg.data[self.class].delete self
  # Destroy all Links
end

Test



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ygg/bck/model.rb', line 82

def link_with(resource) # Test
  links = Link.all.by_source self
  return nil if links.kind_of? Array and links.empty?
  links = [links] unless links.kind_of? Array
  result = []
  links.each do |link|
    result << link if link.target == resource
  end
  
  return false      if result.length == 0
  return result[0]  if result.length == 1
  return result
end


46
# File 'lib/ygg/bck/model.rb', line 46

def links(arg = nil); targets(arg) + sources(arg);  end

#sources(arg = nil) ⇒ Object

Test



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ygg/bck/model.rb', line 65

def sources(arg = nil)   # Test
  links = Link.all.by_target self
  return [] if links.kind_of? Array and links.empty?
  links = [links] unless links.kind_of? Array
  result = [] 
  links.each do |link|
    if arg
      if arg.kind_of? Class
        result << link.source if link.source.class == arg
      else
        result << link.source if link.info == arg
      end
    else; result << link.source; end
  end
  result
end

#targets(arg = nil) ⇒ Object

Test



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ygg/bck/model.rb', line 48

def targets(arg = nil)   # Test
  links = Link.all.by_source self
  return [] if links.kind_of? Array and links.empty?
  links = [links] unless links.kind_of? Array
  result = []
  links.each do |link|
    if arg  
      if arg.kind_of? Class
        result << link.target if link.target.class == arg
      else
        result << link.target if link.info == arg
      end
    else; result << link.target; end
  end
  result
end