lib/markable.rb in markable-0.0.4 vs lib/markable.rb in markable-0.0.5
- old
+ new
@@ -9,23 +9,23 @@
@@marker_objects = []
@@markable_objects = []
protected
- def self.set_models models
- @@models = models unless @@models.count > 0
+ def self.set_models
+ @@models = @@models.presence || ActiveRecord::Base.connection.tables.collect{ |t| t.classify rescue nil }.compact
end
def self.add_markable markable
- @@markable_objects.push markable
- @@markables.push markable.name.to_sym
+ @@markable_objects.push markable unless @@markable_objects.include? markable
+ @@markables.push markable.name.to_sym unless @@markables.include? markable.name.to_sym
create_methods @@marker_objects, [ markable ]
end
def self.add_marker marker
- @@marker_objects.push marker
- @@markers.push marker.name.to_sym
+ @@marker_objects.push marker unless @@marker_objects.include? marker
+ @@markers.push marker.name.to_sym unless @@markers.include? marker.name.to_sym
create_methods [ marker ], @@markable_objects
end
def self.create_methods markers, markables
markables.try :each do |markable|
@@ -65,28 +65,28 @@
}
end
end
end
- def self.can_mark_or_raise? marker_object, markables, mark
- unless self.can_mark? marker_object, markables, mark
- raise Markable::WrongMarkableType.new
- end
- true
+ def self.can_mark_or_raise? markers, markables, mark
+ Array.wrap(markers).all? { |marker_object|
+ Array.wrap(markables).all? { |markable|
+ self.can_mark_object?(marker_object, markable, mark)
+ }
+ }
end
- def self.can_mark? markers, markables, mark
- markables = [ markables ] unless markables.kind_of? Array
- markers = [ markers ] unless markers.kind_of? Array
- markers.all? { |marker_object| markables.all? { |markable| self.can_mark_object?(marker_object, markable, mark) } }
- end
-
def self.can_mark_object? marker_object, markable_object, mark
marker_name = marker_object.class.name.to_sym
markable_name = markable_object.class.name.to_sym
+ raise Markable::WrongMarkerType.new(marker_name) unless @@markers.include?(marker_name)
+ raise Markable::WrongMarkableType.new(markable_name) unless @@markables.include?(markable_name)
+ raise Markable::WrongMark.new(marker_object, markable_object, mark) unless markable_object.markable_marks.include?(mark)
- @@markers.include?(marker_name) && @@markables.include?(markable_name) && markable_object.markable_marks.include?(mark) &&
- (markable_object.markable_marks[mark][:allowed_markers] == :all || markable_object.markable_marks[mark][:allowed_markers].include?(marker_name.to_s.downcase.to_sym))
+ raise Markable::NotAllowedMarker.new(marker_object, markable_object, mark) unless (markable_object.markable_marks[mark][:allowed_markers] == :all ||
+ markable_object.markable_marks[mark][:allowed_markers].include?(marker_name.to_s.downcase.to_sym))
+
+ true
end
end
require 'markable/exceptions'
require 'markable/acts_as_marker'