Class: Knj::Datarow_custom

Inherits:
Object show all
Defined in:
lib/knj/datarow_custom.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, args) ⇒ Datarow_custom

Returns a new instance of Datarow_custom.



84
85
86
87
88
89
90
91
92
# File 'lib/knj/datarow_custom.rb', line 84

def initialize(data, args)
  if data.is_a?(Hash)
    @data = Knj::ArrayExt.hash_sym(data)
    @id = self.id
  else
    @id = data
    self.reload
  end
end

Class Method Details

.add(d) ⇒ Object



67
68
69
# File 'lib/knj/datarow_custom.rb', line 67

def self.add(d)
  return @events.call(:add, d)
end

.classnameObject



63
64
65
# File 'lib/knj/datarow_custom.rb', line 63

def self.classname
  self.name.split("::").last
end

.datarow_init(d) ⇒ Object

Initializes variables on the class from objects.



10
11
12
13
# File 'lib/knj/datarow_custom.rb', line 10

def self.datarow_init(d)
  @@ob = d.ob
  @@db = d.db
end

.eventsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/knj/datarow_custom.rb', line 51

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



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
# File 'lib/knj/datarow_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, 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

.tableObject



71
72
73
# File 'lib/knj/datarow_custom.rb', line 71

def self.table
  return self.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.



109
110
111
112
113
# File 'lib/knj/datarow_custom.rb', line 109

def [](key)
  raise "No data spawned on object." if !@data
  raise "No such key: '#{key}'. Available keys are: '#{@data.keys.sort.join(", ")}'." if !@data.key?(key)
  return @data[key]
end

#deleteObject



138
139
140
# File 'lib/knj/datarow_custom.rb', line 138

def delete
  self.class.events.call(:delete, Knj::Hash_methods.new(:object => self))
end

#deleted?Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/knj/datarow_custom.rb', line 75

def deleted?
  return true if !@data
  return false
end

#destroyObject



142
143
144
# File 'lib/knj/datarow_custom.rb', line 142

def destroy
  @data = nil
end

#each(&args) ⇒ Object



146
147
148
# File 'lib/knj/datarow_custom.rb', line 146

def each(&args)
  return @data.each(&args)
end

#idObject

Returns the ID of the object.



116
117
118
# File 'lib/knj/datarow_custom.rb', line 116

def id
  return self[:id]
end

#is_knj?Boolean

Used to determine if this is a knj-datarow-object.

Returns:

  • (Boolean)


5
6
7
# File 'lib/knj/datarow_custom.rb', line 5

def is_knj?
  return true
end

#nameObject Also known as: title

Returns the name of the object, which can be taken from various data or various defined methods.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/knj/datarow_custom.rb', line 121

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

#reloadObject



94
95
96
97
98
99
100
# File 'lib/knj/datarow_custom.rb', line 94

def reload
  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 => @id))
  raise "No data was received from the event: 'data_from_id'." if !data
  raise "Data expected to be a hash but wasnt: '#{data.class.name}'." if !data.is_a?(Hash)
  @data = Knj::ArrayExt.hash_sym(data)
end

#tableObject



80
81
82
# File 'lib/knj/datarow_custom.rb', line 80

def table
  return self.class.table
end

#to_hashObject



150
151
152
# File 'lib/knj/datarow_custom.rb', line 150

def to_hash
  return @data.clone
end

#update(data) ⇒ Object



102
103
104
105
106
# File 'lib/knj/datarow_custom.rb', line 102

def update(data)
  ret = self.class.events.call(:update, Knj::Hash_methods.new(:object => self, :data => data))
  self.reload
  return ret
end