Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Picky ActiveModel code design for searching

  • Loading branch information...
commit 1fa72d80c33111cf64c05ecc36c0efbe8cde8f4b 1 parent ec72bf0
@floere authored
View
2  activemodel/lib/picky-activemodel.rb
@@ -0,0 +1,2 @@
+require File.expand_path '../picky-activemodel/model', __FILE__
+require File.expand_path '../picky-activemodel/picky', __FILE__
View
2  activemodel/lib/picky-activemodel/model.rb
@@ -0,0 +1,2 @@
+require File.expand_path '../model/indexing', __FILE__
+require File.expand_path '../model/searching', __FILE__
View
9 activemodel/lib/picky-activemodel/model/indexing.rb
@@ -0,0 +1,9 @@
+module Picky
+ module Model
+
+ module Indexing
+
+ end
+
+ end
+end
View
145 activemodel/lib/picky-activemodel/model/searching.rb
@@ -0,0 +1,145 @@
+module Picky
+ module Model
+
+ #
+ #
+ module Searching
+
+ #
+ #
+ def self.included into
+ into.extend ClassMethods
+ end
+
+ #
+ #
+ class Proxy
+
+ # Initialize with defaults.
+ #
+ def initialize model
+ @model = model
+ @text = nil
+ @indexes = model.__picky_default_indexes__
+ @ids = 20
+ @offset = 0
+ end
+
+ #
+ #
+ # Usage:
+ # * Person.search("Bond, James") # Convenience version.
+ # * Person.search.ids(30).with("Bond, James") # Version with options.
+ # * Person.search("Bond, James") do # With block.
+ # ids 30
+ # offset 10
+ # end
+ #
+ def search text = nil, options = {}, &block
+ instance_eval(&block) if block_given?
+
+ if text
+ #
+ #
+ with text
+ else
+ # The user wants the proxy.
+ # (Or made the mistake of passing nil)
+ #
+ self
+ end
+ end
+
+ #
+ #
+ def with text
+ @text = text
+ p "Searching in #{@indexes} with offset #{@offset} and for #{@ids} ids with '#{@text}'"
+ end
+
+ # Define which indexes to use.
+ #
+ def indexes index, *indexes
+ @indexes = [index, *indexes]
+ self
+ end
+
+ #
+ #
+ def ids value
+ @ids = value
+ self
+ end
+
+ # Set an offset for this search.
+ #
+ def offset value
+ @offset = value
+ self
+ end
+
+ end
+
+ module ClassMethods
+
+ #
+ #
+ def self.extended into
+ into.extend Convenience unless into.respond_to?(:search)
+ end
+
+ # This is the method that is called by the convenience methods.
+ #
+ # It returns a Proxy instance on which to search on.
+ #
+ # So if you defined
+ #
+ def picky
+ (@proxy ||= Proxy.new(self)).dup
+ end
+
+ #
+ #
+ module Convenience
+
+ #
+ #
+ def search text = nil, options = {}, &block
+ picky.search text, options, &block
+ end
+
+ end
+
+ end
+
+ end
+
+ end
+end
+
+class Bla
+
+ include Picky::Model::Searching
+
+ # Fake method since indexing has not yet been coded.
+ #
+ def self.__picky_default_indexes__
+ [:blas]
+ end
+
+end
+
+Bla.picky.search("hello")
+Bla.picky.search.with("hello")
+Bla.picky.search.offset(30).with("hello")
+Bla.picky.search.indexes(:blis).ids(30).offset(10).with("hello")
+
+Bla.search("hello")
+Bla.search.with("hello")
+Bla.search.offset(30).with("hello")
+Bla.search.indexes(:blis).ids(30).offset(10).with("hello")
+Bla.search "Gurrr" do
+ indexes :blooo
+ ids 77
+ offset 13
+end
View
16 activemodel/lib/picky-activemodel/picky.rb
@@ -0,0 +1,16 @@
+# The Picky module that can be included.
+#
+# Note: This module might be reopened here.
+#
+module Picky
+
+ # This must be the only place where this method is defined.
+ # If there are others, then included must call the old,
+ # aliased method too.
+ #
+ def self.included into
+ into.extend Model::Indexing
+ into.extend Model::Searching
+ end
+
+end
View
31 activemodel/test/activemodels/gem.rb
@@ -0,0 +1,31 @@
+# Example ActiveModel class
+
+require 'rubygems'
+require 'active_model'
+
+class Gem
+
+ extend ActiveModel::Naming
+ include ActiveModel::AttributeMethods
+ include ActiveModel::Serialization
+ include ActiveModel::Serializers::JSON
+
+ include Picky
+
+ attr_reader :attributes
+
+ def initialize(attributes = {})
+ @attributes = attributes
+ end
+
+ def id; attributes['id'] || attributes['_id']; end
+ def id=(value); attributes['id'] = value; end
+
+ def method_missing(name, *args, &block)
+ attributes[name.to_sym] || attributes[name.to_s] || super
+ end
+
+ def persisted?; true; end
+ def save; true; end
+
+end
View
17 activemodel/test/unit/lint_test.rb
@@ -0,0 +1,17 @@
+require 'test_helper'
+
+module Slingshot
+ module Model
+
+ class ActiveModelLintTest < Test::Unit::TestCase
+
+ include ActiveModel::Lint::Tests
+
+ def setup
+ @model = Gem.new :name => 'Test'
+ end
+
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.