Class: Towsta::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/towsta/core/base.rb,
lib/towsta/core/crud.rb,
lib/towsta/core/mail.rb,
lib/towsta/core/attributes.rb,
lib/towsta/core/references.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Core

Returns a new instance of Core.



5
6
7
8
# File 'lib/towsta/core/base.rb', line 5

def initialize args={}
  self.attributes.merge(args).each {|k,v| self.send("#{k}=".to_sym, v)}
  self.class.all << self
end

Class Attribute Details

.allObject

Returns the value of attribute all.



6
7
8
# File 'lib/towsta/core/references.rb', line 6

def all
  @all
end

.attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/towsta/core/attributes.rb', line 6

def attributes
  @attributes
end

.countObject

Returns the value of attribute count.



6
7
8
# File 'lib/towsta/core/references.rb', line 6

def count
  @count
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/towsta/core/crud.rb', line 5

def message
  @message
end

Class Method Details

.add_occurrence(occurrence) ⇒ Object



16
17
18
19
20
# File 'lib/towsta/core/base.rb', line 16

def self.add_occurrence occurrence
  self.send(:define_singleton_method, "occurrences_of_#{occurrence[:name].downcase}") do
    eval occurrence[:items].inspect
  end
end

.create(args, creator = Towsta.author) ⇒ Object



32
33
34
35
36
# File 'lib/towsta/core/crud.rb', line 32

def self.create args, creator=Towsta.author
  new = self.new(args.merge(:id => nil))
  new.save creator
  new
end

.define_attribute(attr, kind) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/towsta/core/attributes.rb', line 27

def self.define_attribute attr, kind
  self.class_eval("

    def #{attr}= value
      @#{attr} ||= Towsta::Kinds::#{kind[0].upcase + kind[1..-1]}Kind.new
      @#{attr}.set value
    end

    def #{attr}
      @#{attr}.get
    end

    def object_of_#{attr}
      @#{attr}
    end

    def self.find_by_#{attr} value
      self.all.each { |horizontal| return horizontal if horizontal.object_of_#{attr}.compare value }
      nil
    end

    def self.find_by_parameterized_#{attr} value
      self.all.each { |horizontal| return horizontal if horizontal.object_of_#{attr}.compare_parameterized value }
      nil
    end

    def self.find_all_by_#{attr} value
      self.all.select { |horizontal| horizontal.object_of_#{attr}.compare value }
    end

  ")
  self.attributes ||= []
  self.attributes << attr.to_sym
end

.find(id) ⇒ Object



19
20
21
# File 'lib/towsta/core/references.rb', line 19

def self.find id
  self.find_by_id id
end

.firstObject



11
12
13
# File 'lib/towsta/core/references.rb', line 11

def self.first
  self.all.first
end

.lastObject



15
16
17
# File 'lib/towsta/core/references.rb', line 15

def self.last
  self.all.last
end

.randomObject



23
24
25
26
# File 'lib/towsta/core/references.rb', line 23

def self.random
  position = (self.all.size) - 1
  self.all[rand(position)]
end

.to_hashObject



10
11
12
13
14
# File 'lib/towsta/core/base.rb', line 10

def self.to_hash
  hashes = []
  self.all.each {|hash| hashes << hash.attributes}
  hashes
end

Instance Method Details

#attributesObject



9
10
11
12
13
14
15
16
# File 'lib/towsta/core/attributes.rb', line 9

def attributes
  horizontal = {}
  self.class.attributes.each do |attr|
    slice = send :"object_of_#{attr.to_s}"
    horizontal[attr] = slice ? slice.export : nil
  end
  horizontal
end

#destroyObject



12
13
14
# File 'lib/towsta/core/crud.rb', line 12

def destroy
  #to-do
end

#meta_attributesObject



18
19
20
21
22
23
24
25
# File 'lib/towsta/core/attributes.rb', line 18

def meta_attributes
  a = attributes.clone
  a.delete :author
  a.delete :created_at
  a.delete :updated_at
  a.delete :id
  a
end

#save(creator = Towsta.author) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/towsta/core/crud.rb', line 16

def save creator=Towsta.author
  creator = author.email if author
  export = self.attributes
  export.delete :author
  export.delete :created_at
  export.delete :updated_at
  id_aux = export.delete(:id)
  id_aux ? id_aux : '0'
  export = {:creator => creator, :vertical => self.class.to_s, :attributes => export, :id => id_aux}
  response = Towsta::Synchronizer.save_request export
  @message = response[:message]
  self.id = response[:id] if response[:status]
  self.author = User.find_by_email creator
  response[:status]
end

#to_mailObject



5
6
7
8
9
# File 'lib/towsta/core/mail.rb', line 5

def to_mail
  string = ''
  meta_attributes.each { |attribute, value| string << "<b>#{attribute.to_s}:</b> #{value} <br/>" }
  string
end

#update(args, author = Towsta.author) ⇒ Object



7
8
9
10
# File 'lib/towsta/core/crud.rb', line 7

def update args, author=Towsta.author
  args.each {|k,v| self.send("#{k}=".to_sym, v)}
  self.save author
end