Class: Baza::ModelCustom
- Inherits:
-
Object
- Object
- Baza::ModelCustom
- Defined in:
- lib/baza/model_custom.rb
Class Method Summary collapse
- .add(d) ⇒ Object
- .classname ⇒ Object
-
.datarow_init(d) ⇒ Object
Initializes variables on the class from objects.
- .events ⇒ Object
- .has_one(arr) ⇒ Object
- .table ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns a key from the hash that this object is holding or raises an error if it doesnt exist.
- #delete ⇒ Object
- #deleted? ⇒ Boolean
- #destroy ⇒ Object
- #each(&args) ⇒ Object
-
#id ⇒ Object
Returns the ID of the object.
-
#initialize(data, _args) ⇒ ModelCustom
constructor
A new instance of ModelCustom.
-
#is_knj? ⇒ Boolean
Used to determine if this is a knj-datarow-object.
-
#name ⇒ Object
(also: #title)
Returns the name of the object, which can be taken from various data or various defined methods.
- #reload ⇒ Object
- #table ⇒ Object
- #to_hash ⇒ Object
- #update(data) ⇒ Object
Constructor Details
#initialize(data, _args) ⇒ ModelCustom
Returns a new instance of ModelCustom.
86 87 88 89 90 91 92 93 94 |
# File 'lib/baza/model_custom.rb', line 86 def initialize(data, _args) if data.is_a?(Hash) @data = Knj::ArrayExt.hash_sym(data) @id = id else @id = data reload end end |
Class Method Details
.add(d) ⇒ Object
69 70 71 |
# File 'lib/baza/model_custom.rb', line 69 def self.add(d) @events.call(:add, d) end |
.classname ⇒ Object
65 66 67 |
# File 'lib/baza/model_custom.rb', line 65 def self.classname name.split("::").last end |
.datarow_init(d) ⇒ Object
Initializes variables on the class from objects.
10 11 12 13 |
# File 'lib/baza/model_custom.rb', line 10 def self.datarow_init(d) @@ob = d.ob @@db = d.db end |
.events ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/baza/model_custom.rb', line 53 def self.events unless @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 @events end |
.has_one(arr) ⇒ Object
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 43 44 45 46 47 48 49 50 51 |
# File 'lib/baza/model_custom.rb', line 15 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 = val[:class] colname = val[:col] methodname = val[:method] else raise "Unknown argument-type: '#{arr.class.name}'." end methodname = classname.to_s.downcase unless methodname colname = "#{classname.to_s.downcase}_id".to_sym unless colname define_method(methodname) do return @@ob.get_try(self, colname, classname) end methodname_html = "#{methodname}_html".to_sym define_method(methodname_html) do |*args| obj = send(methodname) return @@ob.events.call(:no_html, classname) unless obj raise "Class '#{classname}' does not have a 'html'-method." unless obj.respond_to?(:html) return obj.html(*args) end end end |
.table ⇒ Object
73 74 75 |
# File 'lib/baza/model_custom.rb', line 73 def self.table name.split("::").last end |
Instance Method Details
#[](key) ⇒ Object
Returns a key from the hash that this object is holding or raises an error if it doesnt exist.
111 112 113 114 115 |
# File 'lib/baza/model_custom.rb', line 111 def [](key) raise "No data spawned on object." unless @data raise "No such key: '#{key}'. Available keys are: '#{@data.keys.sort.join(", ")}'." unless @data.key?(key) @data[key] end |
#delete ⇒ Object
140 141 142 |
# File 'lib/baza/model_custom.rb', line 140 def delete self.class.events.call(:delete, Knj::Hash_methods.new(object: self)) end |
#deleted? ⇒ Boolean
77 78 79 80 |
# File 'lib/baza/model_custom.rb', line 77 def deleted? return true unless @data false end |
#destroy ⇒ Object
144 145 146 |
# File 'lib/baza/model_custom.rb', line 144 def destroy @data = nil end |
#each(&args) ⇒ Object
148 149 150 |
# File 'lib/baza/model_custom.rb', line 148 def each(&args) @data.each(&args) end |
#id ⇒ Object
Returns the ID of the object.
118 119 120 |
# File 'lib/baza/model_custom.rb', line 118 def id self[:id] end |
#is_knj? ⇒ Boolean
Used to determine if this is a knj-datarow-object.
5 6 7 |
# File 'lib/baza/model_custom.rb', line 5 def is_knj? true end |
#name ⇒ Object Also known as: title
Returns the name of the object, which can be taken from various data or various defined methods.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/baza/model_custom.rb', line 123 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 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 |
#reload ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/baza/model_custom.rb', line 96 def reload raise "No 'data_from_id'-event connected to class." unless self.class.events.connected?(:data_from_id) data = self.class.events.call(:data_from_id, Knj::Hash_methods.new(id: @id)) raise "No data was received from the event: 'data_from_id'." unless data raise "Data expected to be a hash but wasnt: '#{data.class.name}'." unless data.is_a?(Hash) @data = Knj::ArrayExt.hash_sym(data) end |
#table ⇒ Object
82 83 84 |
# File 'lib/baza/model_custom.rb', line 82 def table self.class.table end |
#to_hash ⇒ Object
152 153 154 |
# File 'lib/baza/model_custom.rb', line 152 def to_hash @data.clone end |
#update(data) ⇒ Object
104 105 106 107 108 |
# File 'lib/baza/model_custom.rb', line 104 def update(data) ret = self.class.events.call(:update, Knj::Hash_methods.new(object: self, data: data)) reload ret end |