Class: Ygg::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/ygg/base.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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ygg/base.rb', line 56

def initialize(*args)
  Ygg.data[Ygg.symbolize(self)] ||= []
  Ygg.data[Ygg.symbolize(self)] << 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



147
# File 'lib/ygg/base.rb', line 147

def self.all; Ygg.data[Ygg.symbolize(self)]; end

.key(*args) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ygg/base.rb', line 153

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



151
# File 'lib/ygg/base.rb', line 151

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

.validatorObject



148
# File 'lib/ygg/base.rb', line 148

def self.validator; @@validator; end

Instance Method Details

#&(res) ⇒ Object

Instance methods : Links



90
91
92
# File 'lib/ygg/base.rb', line 90

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

#*(arg) ⇒ Object



95
# File 'lib/ygg/base.rb', line 95

def *(arg);           links arg;                    end

#<(arg) ⇒ Object



94
# File 'lib/ygg/base.rb', line 94

def <(arg);           sources arg;                  end

#>(arg) ⇒ Object



93
# File 'lib/ygg/base.rb', line 93

def >(arg);           targets arg;                  end

#_finalize!Object

Instance methods : Defaults



79
# File 'lib/ygg/base.rb', line 79

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

#_idObject

Destroy all Links



85
86
87
# File 'lib/ygg/base.rb', line 85

def _id
  Ygg.data[Ygg.symbolize(self)].index self
end

#_loading!Object



80
# File 'lib/ygg/base.rb', line 80

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

#destroyObject



81
82
83
84
# File 'lib/ygg/base.rb', line 81

def destroy
  Ygg.data[Ygg.symbolize(self)].delete self
  # Destroy all Links
end

Test



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ygg/base.rb', line 132

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


96
# File 'lib/ygg/base.rb', line 96

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

#sources(arg = nil) ⇒ Object

Test



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

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ygg/base.rb', line 98

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