Class: Bsm::Constrainable::Schema

Inherits:
Hash
  • Object
show all
Defined in:
lib/bsm/constrainable/schema.rb

Overview

Schema definition.

Constant Summary collapse

Field =
::Bsm::Constrainable::Field

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Schema

Returns a new instance of Schema.



5
6
7
8
# File 'lib/bsm/constrainable/schema.rb', line 5

def initialize(klass)
  @klass = klass
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (protected)



75
76
77
78
79
80
81
82
# File 'lib/bsm/constrainable/schema.rb', line 75

def method_missing(sym, *args)
  if Field.registered?(sym)
    opts = args.extract_options!.merge(:as => sym)
    match(*(args << opts))
  else
    super
  end
end

Instance Method Details

#fields(*names) ⇒ Object

Define multiple constrainable columns. Expects 1-n column names (must be real columns) and an options Hash. Examples:

fields :id
fields :id, :author_id
fields :id, :author_id, :with => [:in, :not_in]


17
18
19
20
21
22
23
24
25
# File 'lib/bsm/constrainable/schema.rb', line 17

def fields(*names)
  options = names.extract_options!
  names.map(&:to_s).each do |name|
    column = @klass.columns_hash[name]
    raise ArgumentError, "Invalid field #{name}" unless column
    raise ArgumentError, "Invalid field type #{column.type}" unless Field.registered?(column.type)
    match name, options.merge(:as => column.type)
  end
end

#filter(params = nil) ⇒ Object

Creates a FilterSet object for given params. Filter-sets can be used to constrain relations as well as e.g. in forms. Example:

filters = Post.constrainable.filter(params[:where])
Post.archived.constrain(filters).limit(100)


65
66
67
# File 'lib/bsm/constrainable/schema.rb', line 65

def filter(params = nil)
  Bsm::Constrainable::FilterSet.new self, params
end

#match(*names) ⇒ Object Also known as: field

Define constrainable names (don’t have to be real columns). Expects 1-n names and an options Hash. Options, must specify the type via :as. Examples:

# One match
match :id, :as => :number

# Multiple matches
match :id, :author_id, :as => :integer, :with => [:in, :not_in]

# Use a specific column
match :created, :using => :created_at, :as => :timestamp, :with => [:lt, :between]

# Complex example, using an attribute from another table, and ensure it's included (LEFT OUTER JOIN)
match :author, :using => proc { Author.scope.table[:name] }, :scope => { includes(:author) }, :as => :string, :with => [:eq, :matches]

There are also several short-cuts available. Examples:

timestamp :created, :using => :created_at, :with => [:lt, :between]
number    :id, :author_id
string    :title, :with => [:eq, :matches]


49
50
51
52
53
54
55
56
# File 'lib/bsm/constrainable/schema.rb', line 49

def match(*names)
  options = names.extract_options!
  kind    = options.delete(:as)
  names.map(&:to_s).each do |name|
    self[name] ||= []
    self[name] << Field.new(kind, name, options)
  end
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/bsm/constrainable/schema.rb', line 69

def respond_to?(sym)
  super || Field.registered?(sym)
end