Class: Knj::Datarow_custom
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #delete ⇒ Object
- #destroy ⇒ Object
- #each(&args) ⇒ Object
- #id ⇒ Object
-
#initialize(d) ⇒ Datarow_custom
constructor
A new instance of Datarow_custom.
- #is_knj? ⇒ Boolean
- #name ⇒ Object (also: #title)
- #table ⇒ Object
- #update(data) ⇒ Object
Constructor Details
#initialize(d) ⇒ Datarow_custom
Returns a new instance of Datarow_custom.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/knj/datarow_custom.rb', line 68 def initialize(d) @ob = d.ob data = d.data if data.is_a?(Hash) @data = Knj::ArrayExt.hash_sym(data) else data = d.data raise "No 'data_from_id'-event connected to class." if !self.class.events.connected?(:data_from_id) data = self.class.events.call(:data_from_id, Knj::Hash_methods.new(:id => data)) raise "No data was received from the event: 'data_from_id'." if !data @data = Knj::ArrayExt.hash_sym(data) end end |
Class Method Details
.add(d) ⇒ Object
56 57 58 |
# File 'lib/knj/datarow_custom.rb', line 56 def self.add(d) return @events.call(:add, d) end |
.events ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/knj/datarow_custom.rb', line 44 def self.events if !@events @events = Knj::Event_handler.new @events.add_event(:name => :add, :connections_max => 1) @events.add_event(:name => :update, :connections_max => 1) @events.add_event(:name => :data_from_id, :connections_max => 1) @events.add_event(:name => :delete, :connections_max => 1) end return @events end |
.has_one(arr) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/knj/datarow_custom.rb', line 8 def self.has_one(arr) arr.each do |val| methodname = nil colname = nil classname = nil if val.is_a?(Symbol) classname = val methodname = val.to_s.downcase.to_sym colname = "#{val.to_s.downcase}_id".to_sym elsif val.is_a?(Array) classname, colname, methodname = *val elsif val.is_a?(Hash) classname, colname, methodname = val[:class], val[:col], val[:method] else raise "Unknown argument-type: '#{arr.class.name}'." end methodname = classname.to_s.downcase if !methodname colname = "#{classname.to_s.downcase}_id".to_sym if !colname define_method(methodname) do return @ob.get_try(self, colname, classname) end methodname_html = "#{methodname.to_s}_html".to_sym define_method(methodname_html) do |*args| obj = self.send(methodname) return @ob.events.call(:no_html, classname) if !obj raise "Class '#{classname}' does not have a 'html'-method." if !obj.respond_to?(:html) return obj.html(*args) end end end |
.table ⇒ Object
60 61 62 |
# File 'lib/knj/datarow_custom.rb', line 60 def self.table return self.name.split("::").last end |
Instance Method Details
#[](key) ⇒ Object
87 88 89 90 |
# File 'lib/knj/datarow_custom.rb', line 87 def [](key) raise "No such key: '#{key}'." if !@data or !@data.key?(key) return @data[key] end |
#delete ⇒ Object
113 114 115 |
# File 'lib/knj/datarow_custom.rb', line 113 def delete self.class.events.call(:delete, Knj::Hash_methods.new(:object => self)) end |
#destroy ⇒ Object
117 118 119 |
# File 'lib/knj/datarow_custom.rb', line 117 def destroy @data = nil end |
#each(&args) ⇒ Object
121 122 123 |
# File 'lib/knj/datarow_custom.rb', line 121 def each(&args) return @data.each(&args) end |
#id ⇒ Object
92 93 94 |
# File 'lib/knj/datarow_custom.rb', line 92 def id return self[:id] end |
#is_knj? ⇒ Boolean
4 5 6 |
# File 'lib/knj/datarow_custom.rb', line 4 def is_knj? return true end |
#name ⇒ Object Also known as: title
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/knj/datarow_custom.rb', line 96 def name if @data.key?(:title) return @data[:title] elsif @data.key?(:name) return @data[:name] end obj_methods = self.class.instance_methods(false) [:name, :title].each do |method_name| return self.method(method_name).call if obj_methods.index(method_name) end raise "Couldnt figure out the title/name of the object on class #{self.class.name}." end |
#table ⇒ Object
64 65 66 |
# File 'lib/knj/datarow_custom.rb', line 64 def table return self.class.name.split("::").last end |
#update(data) ⇒ Object
83 84 85 |
# File 'lib/knj/datarow_custom.rb', line 83 def update(data) return self.class.events.call(:update, Knj::Hash_methods.new(:object => self, :data => data)) end |