# File lib/chef_fixie_shahid/sql_objects.rb, line 384 def self.primary(arg) name = :"by_#{arg}" class_eval("def [](arg); #{name}(arg).all(1).first; end") listfun = <<EOLF def list(max_count=:default) elements = all(max_count) if elements == :too_many_results elements else elements.map {|e| e.#{arg} }.sort end end EOLF class_eval(listfun) end
class ChefFixie::Sql::SqlTable
Public Class Methods
element(name)
click to toggle source
filter_by(*args)
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 401 def self.filter_by(*args) args.each do |field| name = "by_#{field}" fundef = "def #{name}(exp); filter_core(:#{field},exp); end" class_eval(fundef) end end
max_count_default()
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 344 def self.max_count_default 50 end
new(tablespec = nil)
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 356 def initialize(tablespec = nil) ChefFixie::Sql.default_connection @inner = tablespec || Sequel::Model(get_table) end
primary(arg)
click to toggle source
TODO Improve these via define_method See blog.jayfields.com/2007/10/ruby-defining-class-methods.html
https://stackoverflow.com/questions/9658724/ruby-metaprogramming-class-eval/9658775#9658775
table(name)
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 409 def self.table(name) fundef = "def get_table; :#{name}; end" class_eval(fundef) end
Public Instance Methods
all(max_count = :default)
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 370 def all(max_count = :default) if max_count == :default max_count = ChefFixie::Sql::SqlTable.max_count_default end if max_count != :all return :too_many_results if inner.count > max_count end elements = inner.all.map { |org| mk_element(org) } end
filter_core(field, exp)
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 366 def filter_core(field, exp) self.class.new(inner.filter(field => exp)) end
get_table()
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 348 def get_table :unknown_table end
inner()
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 361 def inner # Make sure we have init @inner end
mk_element(x)
click to toggle source
# File lib/chef_fixie_shahid/sql_objects.rb, line 352 def mk_element(x) x end