Module: Houcho::Attribute
Instance Method Summary collapse
- #attr_id(attr_name) ⇒ Object
- #del_attr(target, attr_name = nil) ⇒ Object
- #get_attr(target, attr_name = nil, json = false) ⇒ Object
- #get_attr_json(target, name = nil) ⇒ Object
- #set_attr(target, attr, force = false) ⇒ Object
- #set_attr!(target, attr) ⇒ Object
Instance Method Details
#attr_id(attr_name) ⇒ Object
7 8 9 10 11 |
# File 'lib/houcho/attribute.rb', line 7 def attr_id(attr_name) id = @db.execute("SELECT id FROM attribute WHERE name = ?", attr_name) raise AttributeExceptiotn, "attribute does not exist - #{attr_name}" unless id id.flatten.first end |
#del_attr(target, attr_name = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/houcho/attribute.rb', line 81 def del_attr(target, attr_name = nil) before_delete = get_attr(target, attr_name) sql = "DELETE FROM attribute_value WHERE element_type = ? AND element_id = ?" @db.transaction do if attr_name sql += " AND attr_id = ?" @db.execute(sql, @type_id, id(target), attr_id(attr_name)) else @db.execute(sql, @type_id, id(target)) end end #end of transaction before_delete end |
#get_attr(target, attr_name = nil, json = false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/houcho/attribute.rb', line 60 def get_attr(target, attr_name = nil, json = false) target_id = id(target) return json ? "{}" : {} if target_id.nil? attr = {} id_value = @db.execute(" SELECT attr_id, value FROM attribute_value WHERE element_type = ? AND element_id = ? ", @type_id, target_id) id_value.each do |iv| name = @db.execute("SELECT name FROM attribute WHERE id = ?", iv[0]).first.first attr[name.to_sym] = iv[1] end attr = attr.select { |k,v| k == attr_name.to_sym } if attr_name attr = JSON.generate(attr) if json attr end |
#get_attr_json(target, name = nil) ⇒ Object
55 56 57 |
# File 'lib/houcho/attribute.rb', line 55 def get_attr_json(target, name = nil) get_attr(target, name, true) end |
#set_attr(target, attr, force = false) ⇒ Object
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 52 |
# File 'lib/houcho/attribute.rb', line 18 def set_attr(target, attr, force = false) target_id = id(target) raise_target_does_not_exist(target) if target_id.nil? @db.transaction do attr.each do |name, attr_value| attr_name = name.to_s begin @db.execute("INSERT INTO attribute(name) VALUES(?)", attr_name) rescue SQLite3::ConstraintException, "column name is not unique" end begin @db.execute( "INSERT INTO attribute_value(attr_id, element_type, element_id, value) VALUES(?,?,?,?)", attr_id(attr_name), @type_id, target_id, attr_value ) rescue SQLite3::ConstraintException, "columns attr_id, element_type, element_id are not unique" if force @db.execute(" UPDATE attribute_value SET value = ? WHERE attr_id = ? AND element_type = ? AND element_id = ? ", attr_value, attr_id(attr_name), @type_id, target_id) else raise AttributeExceptiotn, "attribute has already defined value in #{@type} - #{attr_name}" end end end end #end of transaction end |
#set_attr!(target, attr) ⇒ Object
14 15 16 |
# File 'lib/houcho/attribute.rb', line 14 def set_attr!(target, attr) set_attr(target, attr, true) end |