Class: Houcho::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/houcho/element.rb

Direct Known Subclasses

Host, OuterRole, Spec

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Element

Returns a new instance of Element.



6
7
8
9
10
# File 'lib/houcho/element.rb', line 6

def initialize(type)
  @db = Houcho::Database.new.handle
  @role = Houcho::Role.new
  @type = type
end

Instance Method Details

#attach(elements, roles) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/houcho/element.rb', line 46

def attach(elements, roles)
  elements = [elements] unless elements.is_a?(Array)
  roles = [roles] unless roles.is_a?(Array)

  roles.each do |role|
    role_id = @role.id(role)
    raise RoleExistenceException, "role does not exist - #{role}" unless role_id

    @db.transaction do

    elements.each do |element|
      @db.execute("INSERT INTO #{@type}(name) VALUES(?)", element) unless id(element)

      begin
        @db.execute("INSERT INTO role_#{@type} VALUES(?,?)", role_id, id(element))
      rescue SQLite3::ConstraintException
        next
      end
    end

    end #end of transaction
  end
end

#detach(elements, roles) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/houcho/element.rb', line 81

def detach(elements, roles)
  elements = [elements] unless elements.is_a?(Array)
  roles = [roles] unless roles.is_a?(Array)

  roles.each do |role|
    role_id = @role.id(role)
    raise RoleExistenceException, "role does not exist - #{role}" if role_id.nil?

    @db.transaction do

    elements.each do |element|
      @db.execute(
        "DELETE FROM role_#{@type} WHERE role_id = ? AND #{@type}_id = ?",
        role_id,
        id(element)
      )

      begin
        @db.execute("DELETE FROM #{@type} WHERE name = ?", element)
      rescue SQLite3::ConstraintException, "foreign key constraint failed"
        next
      end
    end

    end #end of transaction
  end
end

#detach_from_all(elements) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/houcho/element.rb', line 71

def detach_from_all(elements)
  roles = []
  details(elements).each do |e, r|
    roles = roles.concat(r["role"]||[]).uniq
  end

  detach(elements, roles)
end

#details(elements) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/houcho/element.rb', line 26

def details(elements)
  elements = elements.is_a?(Array) ? elements : [elements]
  result = {}

  elements.each do |element|
    roles = @db.execute("
      SELECT role.name
      FROM role, #{@type}, role_#{@type}
      WHERE role_#{@type}.#{@type}_id = #{@type}.id
      AND role_#{@type}.role_id = role.id
      AND #{@type}.name = ?
    ", element).flatten.sort.uniq

    result[element] = { "role" => roles }
  end

  result
end

#id(name) ⇒ Object



13
14
15
# File 'lib/houcho/element.rb', line 13

def id(name)
  @db.execute("SELECT id FROM #{@type} WHERE name = '#{name}'").flatten.first
end

#list(role_id = nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/houcho/element.rb', line 18

def list(role_id = nil)
  sql = "SELECT T1.name FROM #{@type} T1"
  sql += " JOIN role_#{@type} T2 ON T1.id = T2.#{@type}_id WHERE T2.role_id = #{role_id}" if role_id

  @db.execute(sql).flatten
end