Class: Zena::Use::DynAttributes::DynAttributeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/zena/use/dyn_attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, opts = {}) ⇒ DynAttributeProxy

Returns a new instance of DynAttributeProxy.



31
32
33
34
# File 'lib/zena/use/dyn_attributes.rb', line 31

def initialize(obj, opts={})
  @owner   = obj
  @options = opts
end

Class Method Details

.for(obj, opts = {}) ⇒ Object



27
28
29
# File 'lib/zena/use/dyn_attributes.rb', line 27

def self.for(obj, opts={})
  self.new(obj, opts)
end

Instance Method Details

#[](key) ⇒ Object



53
54
55
56
# File 'lib/zena/use/dyn_attributes.rb', line 53

def [](key)
  return nil unless valid_key?(key)
  hash[key.to_s]
end

#[]=(key, value) ⇒ Object

empty values are considered as nil



70
71
72
73
# File 'lib/zena/use/dyn_attributes.rb', line 70

def []=(key,value)
  return unless valid_key?(key)
  hash[key.to_s] = (value && value != '') ? value : nil
end

#attributes=(attrs) ⇒ Object



36
37
38
39
40
# File 'lib/zena/use/dyn_attributes.rb', line 36

def attributes=(attrs)
  attrs.each do |k,v|
    self[k] = v
  end
end

#changed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/zena/use/dyn_attributes.rb', line 49

def changed?
  @original != @hash
end

#clone_for(obj, opts = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/zena/use/dyn_attributes.rb', line 119

def clone_for(obj, opts={})
  # only keep the values, not the keys
  clone = DynAttributeProxy.for(obj, opts)
  # load clone's actual attributes so the keys are set
  clone_hash = clone.send(:hash)
  # replace with new one
  clone.instance_variable_set(:@hash, hash.dup)
  clone
end

#delete(key) ⇒ Object



83
84
85
# File 'lib/zena/use/dyn_attributes.rb', line 83

def delete(key)
  hash.delete(key.to_s)
end

#destroyObject



134
135
136
# File 'lib/zena/use/dyn_attributes.rb', line 134

def destroy
  connection.execute "DELETE FROM #{table_name} WHERE owner_id = '#{@owner[:id].to_i}'"
end

#eachObject



79
80
81
# File 'lib/zena/use/dyn_attributes.rb', line 79

def each
  hash.each {|e| yield(e) }
end

#inspectObject



138
139
140
141
142
143
144
# File 'lib/zena/use/dyn_attributes.rb', line 138

def inspect
  "#<#{self.class}:#{sprintf('%x',self.object_id)}\n" +
  "@hash =\n{ " +
   ((hash || {}).sort.map do |k,v|
     sprintf("%15s => %s", k, v.inspect)
   end.join("\n  ")) + "}, @owner = #<#{@owner.class}:#{sprintf('%x',@owner.object_id)}>, @options = #{@options.inspect} >"
end

#keysObject



75
76
77
# File 'lib/zena/use/dyn_attributes.rb', line 75

def keys
  hash.keys
end

#mapObject



58
59
60
61
62
# File 'lib/zena/use/dyn_attributes.rb', line 58

def map
  hash.map do |k,v|
    yield(k,v)
  end
end

#saveObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zena/use/dyn_attributes.rb', line 87

def save
  return unless @hash
  add = []
  upd = []
  del = []
  # detect removed elements
  Hash[*@keys.map{|k,v| [k,nil]}.flatten].merge(@hash).each do |key,value|
    if !value && id = @keys[key]
      del << id
    elsif value && id = @keys[key]
      upd << [value,id]
    elsif value
      add << [key, value, @owner[:id].to_i]
    end
  end

  unless add.empty?
    Zena::Db.insert_many(table_name, %W{key value owner_id}, add)
  end

  unless del.empty?
    connection.execute "DELETE FROM #{table_name} WHERE id IN ('#{del.join("','")}')"
  end

  upd.each do |value,id|
    connection.execute "UPDATE #{table_name} SET value = #{connection.quote(value)} WHERE id = '#{id}'"
  end

  # clear hash so it will be reloaded if needed
  @hash = @original = nil
end

#send(key) ⇒ Object



64
65
66
67
# File 'lib/zena/use/dyn_attributes.rb', line 64

def send(key)
  return nil unless valid_key?(key)
  hash[key.to_s]
end

#update_with(new_hash) ⇒ Object



129
130
131
132
# File 'lib/zena/use/dyn_attributes.rb', line 129

def update_with(new_hash)
  hash # make sure current elements are loaded
  @hash = Hash[*new_hash.dup.map{|k,v| [k.to_s,v]}.flatten]
end

#would_edit?(hash) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/zena/use/dyn_attributes.rb', line 42

def would_edit?(hash)
  hash.each do |k, v|
    return true if self[k.to_s] != v && !(self[k.to_s].blank? && v.blank?)
  end
  false
end