Class: Towsta::Core
- Inherits:
-
Object
- Object
- Towsta::Core
- 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
-
.all ⇒ Object
Returns the value of attribute all.
-
.attributes ⇒ Object
Returns the value of attribute attributes.
-
.count ⇒ Object
Returns the value of attribute count.
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
Class Method Summary collapse
- .add_occurrence(occurrence) ⇒ Object
- .create(args, creator = Towsta.author) ⇒ Object
- .define_attribute(attr, kind) ⇒ Object
- .find(id) ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .random ⇒ Object
- .to_hash ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #destroy ⇒ Object
-
#initialize(args = {}) ⇒ Core
constructor
A new instance of Core.
- #meta_attributes ⇒ Object
- #save(creator = Towsta.author) ⇒ Object
- #to_mail ⇒ Object
- #update(args, author = Towsta.author) ⇒ Object
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
.all ⇒ Object
Returns the value of attribute all.
6 7 8 |
# File 'lib/towsta/core/references.rb', line 6 def all @all end |
.attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/towsta/core/attributes.rb', line 6 def attributes @attributes end |
.count ⇒ Object
Returns the value of attribute count.
6 7 8 |
# File 'lib/towsta/core/references.rb', line 6 def count @count end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
5 6 7 |
# File 'lib/towsta/core/crud.rb', line 5 def @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. 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 |
.first ⇒ Object
11 12 13 |
# File 'lib/towsta/core/references.rb', line 11 def self.first self.all.first end |
.last ⇒ Object
15 16 17 |
# File 'lib/towsta/core/references.rb', line 15 def self.last self.all.last end |
.random ⇒ Object
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_hash ⇒ Object
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
#attributes ⇒ Object
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 |
#destroy ⇒ Object
12 13 14 |
# File 'lib/towsta/core/crud.rb', line 12 def destroy #to-do end |
#meta_attributes ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/towsta/core/attributes.rb', line 18 def 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. creator = .email if 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. = User.find_by_email creator response[:status] end |
#to_mail ⇒ Object
5 6 7 8 9 |
# File 'lib/towsta/core/mail.rb', line 5 def to_mail string = '' .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, =Towsta. args.each {|k,v| self.send("#{k}=".to_sym, v)} self.save end |