Permalink
Browse files

Cleaned up. More docs

  • Loading branch information...
1 parent 38aa5f3 commit e554be6b7e4ed7eca6441a06d4525c372a89262f @dim dim committed Feb 18, 2011
View
@@ -1,2 +1,4 @@
+.yardoc
.project
spec/tmp
+doc
@@ -1,15 +1,14 @@
require "active_support/core_ext"
-module Bsm
- module Constrainable
+module Bsm # @private
+ module Constrainable # @private
autoload :Util, "bsm/constrainable/util"
autoload :Model, "bsm/constrainable/model"
autoload :Relation, "bsm/constrainable/relation"
autoload :Schema, "bsm/constrainable/schema"
autoload :Registry, "bsm/constrainable/registry"
autoload :Field, "bsm/constrainable/field"
autoload :Operation, "bsm/constrainable/operation"
- autoload :Filter, "bsm/constrainable/filter"
end
end
@@ -1,16 +1,31 @@
+# Abstract field type
class Bsm::Constrainable::Field::Base
DEFAULT_OPERATORS = [:eq, :not_eq, :gt, :gteq, :lt, :lteq, :between].freeze
class_inheritable_accessor :operators, :defaults, :instance_reader => false, :instance_writer => false
self.operators = DEFAULT_OPERATORS.dup
self.defaults = [:eq, :not_eq]
+ # Returns the field type/kind, e.g. <tt>:string</tt> or <tt>:integer</tt>
def self.kind
@kind ||= name.demodulize.underscore.to_sym
end
attr_reader :name, :operators, :attribute, :scope
+ # Accepts a name and options. Valid options are:
+ # * <tt>:using</tt> - a Symbol or a Proc pointing to a DB column, optional (uses name by default)
+ # * <tt>:with</tt> - a list of operators to use
+ # * <tt>:scope</tt> - a Proc containing additonal scopes
+ #
+ # Examples:
+ #
+ # Field::Integer.new :id
+ # Field::Integer.new :uid, :using => :id
+ # Field::Integer.new :uid, :using => lambda { Model.scoped.table[:col_name] }
+ # Field::String.new :name, :with => [:matches]
+ # Field::String.new :author, :with => [:matches], :using => lambda { Author.scoped.table[:name] }, :scope => labmda { includes(:author) }
+ #
def initialize(name, options = {})
@name = name.to_s
@attribute = options[:using] || name
@@ -19,6 +34,7 @@ def initialize(name, options = {})
@scope = options[:scope]
end
+ # Merge params into a relation
def merge(relation, params)
params.slice(*operators).each do |operator, value|
operation = Bsm::Constrainable::Operation.new(operator, value, relation, self)
@@ -1,8 +0,0 @@
-class Bsm::Constrainable::Filter < Hash
- include Bsm::Constrainable::Util
-
- def initialize(schema, params)
- end
-
-end
-
@@ -1,19 +1,23 @@
module Bsm::Constrainable::Registry
+ # Returns the current registry Hash
def registry
@registry ||= {}
end
+ # Register a class
def register(klass)
raise ArgumentError, "Already registered kind: #{klass.kind}" if registered?(klass.kind)
registry[klass.kind] = klass
end
+ # Create a new object of a certain kind.
def new(kind, *args)
raise ArgumentError, "Invalid kind #{kind}" unless registered?(kind)
registry[kind.to_sym].new(*args)
end
+ # Returns true if kind was already registered, else false
def registered?(kind)
registry.key?(kind.to_sym)
end
@@ -3,7 +3,7 @@ class Bsm::Constrainable::Schema < Hash
include ::Bsm::Constrainable::Util
Field = ::Bsm::Constrainable::Field
- def initialize(klass) # :nodoc:
+ def initialize(klass)
@klass = klass
super()
end
@@ -57,11 +57,11 @@ def match(*names)
end
alias_method :field, :match
- def respond_to?(sym) # :nodoc:
+ def respond_to?(sym)
super || Field.registered?(sym)
end
- def merge(relation, params) # :nodoc:
+ def merge(relation, params)
each_part(params) do |name, part|
self[name].each do |constrain|
relation = constrain.merge(relation, part)
@@ -1,4 +1,4 @@
-module Bsm::Constrainable::Util # :nodoc:
+module Bsm::Constrainable::Util
extend self
def normalized_hash(hash)

0 comments on commit e554be6

Please sign in to comment.