Skip to content

Commit

Permalink
changed all collection IVAR to avoid namespace collisions. Issue #99
Browse files Browse the repository at this point in the history
  • Loading branch information
sxross committed Jan 3, 2014
1 parent de715db commit 6849979
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
68 changes: 34 additions & 34 deletions motion/adapters/array_finder_query.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module MotionModel
class ArrayFinderQuery
attr_accessor :field_name

def initialize(*args)#nodoc
@field_name = args[0] if args.length > 1
@collection = args.last
@_collection = args.last
end

def belongs_to(obj, klass = nil) #nodoc
@related_object = obj
@klass = klass
self
end

# Conjunction to add conditions to query.
#
# Task.find(:name => 'bob').and(:gender).eq('M')
Expand All @@ -22,37 +22,37 @@ def and(field_name)
self
end
alias_method :where, :and

# Specifies how to sort. only ascending sort is supported in the short
# form. For descending, implement the block form.
#
# Task.where(:name).eq('bob').order(:pay_grade).all => array of bobs ascending by pay grade
# Task.where(:name).eq('bob').order(:pay_grade){|o1, o2| o2 <=> o1} => array of bobs descending by pay grade
def order(field = nil, &block)
if block_given?
@collection = @collection.sort{|o1, o2| yield(o1, o2)}
@_collection = @_collection.sort{|o1, o2| yield(o1, o2)}
else
raise ArgumentError.new('you must supply a field name to sort unless you supply a block.') if field.nil?
@collection = @collection.sort{|o1, o2| o1.send(field) <=> o2.send(field)}
@_collection = @_collection.sort{|o1, o2| o1.send(field) <=> o2.send(field)}
end
self
end

def translate_case(item, case_sensitive)#nodoc
item = item.underscore if case_sensitive === false && item.respond_to?(:underscore)
item
end

def do_comparison(query_string, options = {:case_sensitive => false})#nodoc
query_string = translate_case(query_string, options[:case_sensitive])
@collection = @collection.collect do |item|
@_collection = @_collection.collect do |item|
comparator = item.send(@field_name.to_sym)
comparator = translate_case(comparator, options[:case_sensitive])
item if yield query_string, comparator
end.compact
self
end

# performs a "like" query.
#
# Task.find(:work_group).contain('dev') => ['UI dev', 'Core dev', ...]
Expand All @@ -67,16 +67,16 @@ def contain(query_string, options = {:case_sensitive => false})
end
alias_method :contains, :contain
alias_method :like, :contain

# performs a set-inclusion test.
#
# Task.find(:id).in([3, 5, 9])
def in(set)
@collection = @collection.collect do |item|
@_collection = @_collection.collect do |item|
item if set.include?(item.send(@field_name.to_sym))
end.compact
end

# performs strict equality comparison.
#
# If arguments are strings, they are, by default,
Expand All @@ -91,7 +91,7 @@ def eq(query_string, options = {:case_sensitive => false})
end
alias_method :==, :eq
alias_method :equal, :eq

# performs greater-than comparison.
#
# see `eq` for notes on case sensitivity.
Expand All @@ -102,7 +102,7 @@ def gt(query_string, options = {:case_sensitive => false})
end
alias_method :>, :gt
alias_method :greater_than, :gt

# performs less-than comparison.
#
# see `eq` for notes on case sensitivity.
Expand All @@ -113,7 +113,7 @@ def lt(query_string, options = {:case_sensitive => false})
end
alias_method :<, :lt
alias_method :less_than, :lt

# performs greater-than-or-equal comparison.
#
# see `eq` for notes on case sensitivity.
Expand All @@ -124,7 +124,7 @@ def gte(query_string, options = {:case_sensitive => false})
end
alias_method :>=, :gte
alias_method :greater_than_or_equal, :gte

# performs less-than-or-equal comparison.
#
# see `eq` for notes on case sensitivity.
Expand All @@ -135,7 +135,7 @@ def lte(query_string, options = {:case_sensitive => false})
end
alias_method :<=, :lte
alias_method :less_than_or_equal, :lte

# performs inequality comparison.
#
# see `eq` for notes on case sensitivity.
Expand All @@ -146,7 +146,7 @@ def ne(query_string, options = {:case_sensitive => false})
end
alias_method :!=, :ne
alias_method :not_equal, :ne

########### accessor methods #########

# returns first element or count elements that matches.
Expand All @@ -158,15 +158,15 @@ def first(*args)
def last(*args)
to_a.send(:last, *args)
end

# returns all elements that match as an array.
def all
to_a
end

# returns all elements that match as an array.
def to_a
@collection || []
@_collection || []
end

# each is a shortcut method to turn a query into an iterator. It allows
Expand All @@ -175,17 +175,17 @@ def to_a
# Task.where(:assignee).eq('bob').each{ |assignee| do_something_with(assignee) }
def each(&block)
raise ArgumentError.new("each requires a block") unless block_given?
@collection.each{|item| yield item}
@_collection.each{|item| yield item}
end

# returns length of the result set.
def length
@collection.length
@_collection.length
end
alias_method :count, :length

################ relation support ##############

# task.assignees.create(:name => 'bob')
# creates a new Assignee object on the Task object task
def create(options)
Expand All @@ -194,24 +194,24 @@ def create(options)
obj.save
obj
end

# task.assignees.new(:name => 'BoB')
# creates a new unsaved Assignee object on the Task object task
def new(options = {})
raise ArgumentError.new("Creating on a relation requires the parent be saved first.") if @related_object.nil?

id_field = (@related_object.class.to_s.underscore + '_id').to_sym
new_obj = @klass.new(options.merge(id_field => @related_object.id))

new_obj
end

# Returns number of objects (rows) in collection
def length
@collection.length
@_collection.length
end
alias_method :count, :length

# Pushes an object onto an association. For e.g.:
#
# Task.find(3).assignees.push(assignee)
Expand Down
34 changes: 17 additions & 17 deletions motion/adapters/array_model_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ def self.included(base)
end

module PublicClassMethods
def collection
@collection ||= []
def _collection
@_collection ||= []
end

def insert(object)
collection << object
_collection << object
end
alias :<< :insert

def length
collection.length
_collection.length
end
alias_method :count, :length

Expand All @@ -34,7 +34,7 @@ def delete_all
# Do each delete so any on_delete and
# cascades are called, then empty the
# collection and compact the array.
bulk_update { collection.pop.delete until collection.empty? }
bulk_update { _collection.pop.delete until _collection.empty? }
_reset_next_id
end

Expand All @@ -47,18 +47,18 @@ def delete_all
# @posts = Post.find(:author).eq('bob').all
def find(*args, &block)
if block_given?
matches = collection.collect do |item|
matches = _collection.collect do |item|
item if yield(item)
end.compact
return ArrayFinderQuery.new(matches)
end

unless args[0].is_a?(Symbol) || args[0].is_a?(String)
target_id = args[0].to_i
return collection.select{|element| element.id == target_id}.first
return _collection.select{|element| element.id == target_id}.first
end

ArrayFinderQuery.new(args[0].to_sym, collection)
ArrayFinderQuery.new(args[0].to_sym, _collection)
end
alias_method :where, :find

Expand All @@ -68,11 +68,11 @@ def find_by_id(id)

# Returns query result as an array
def all
collection
_collection
end

def order(field_name = nil, &block)
ArrayFinderQuery.new(collection).order(field_name, &block)
ArrayFinderQuery.new(_collection).order(field_name, &block)
end

end
Expand Down Expand Up @@ -110,12 +110,12 @@ def increment_next_id(other_id)
# work. It only undeletes the object you still own.

def undelete
collection << self
_collection << self
issue_notification(:action => 'add')
end

def collection #nodoc
self.class.collection
def _collection #nodoc
self.class._collection
end

# This adds to the ArrayStore without the magic date
Expand All @@ -127,7 +127,7 @@ def add_to_store(*)

# Count of objects in the current collection
def length
collection.length
_collection.length
end
alias_method :count, :length

Expand Down Expand Up @@ -160,15 +160,15 @@ def _has_many_has_one_relation(col) # nodoc
end

def do_insert(options = {})
collection << self
_collection << self
end

def do_update(options = {})
end

def do_delete
target_index = collection.index{|item| item.id == self.id}
collection.delete_at(target_index) unless target_index.nil?
target_index = _collection.index{|item| item.id == self.id}
_collection.delete_at(target_index) unless target_index.nil?
issue_notification(:action => 'delete')
end

Expand Down
2 changes: 1 addition & 1 deletion motion/adapters/array_model_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def serialize_to_file(file_name = nil)
@file_name = file_name if file_name
error_ptr = Pointer.new(:object)

data = NSKeyedArchiver.archivedDataWithRootObject collection
data = NSKeyedArchiver.archivedDataWithRootObject _collection
unless data.writeToFile(documents_file(@file_name), options: NSDataWritingAtomic, error: error_ptr)
# De-reference the pointer.
error = error_ptr[0]
Expand Down
12 changes: 6 additions & 6 deletions motion/model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def define_belongs_to_methods(name) #nodoc
def define_has_many_methods(name) #nodoc
col = column(name)
define_method(name) { get_has_many_attr(col) }
define_method("#{name}=") { |collection| set_has_many_attr(col, *collection) }
define_method("#{name}=") { |collection| set_has_many_attr(col, *_collection) }
end

def define_has_one_methods(name) #nodoc
Expand Down Expand Up @@ -700,13 +700,13 @@ def belongs_to_synced?(col, owner)

def push_has_many_attr(col, *instances)
_col = column(col)
collection = get_has_many_attr(_col)
_collection = []
_collection = get_has_many_attr(_col)
__collection = []
instances.each do |instance|
next if collection.include?(instance)
_collection << instance
next if _collection.include?(instance)
__collection << instance
end
push_relation(_col, *_collection)
push_relation(_col, *__collection)
instances
end

Expand Down

0 comments on commit 6849979

Please sign in to comment.