Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.

Replace class_eval with method calls in support module #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/dm-types/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DataMapper
class Property
class Enum < Integer

include Flags
include Types::Support::Flags

def initialize(model, name, options = {})
super
Expand All @@ -19,10 +19,8 @@ def initialize(model, name, options = {})

if defined?(::DataMapper::Validations)
unless model.skip_auto_validation_for?(self)
if self.class.ancestors.include?(Property::Enum)
allowed = flag_map.values_at(*flag_map.keys.sort)
model.validates_within name, model.options_with_message({ :set => allowed }, self, :within)
end
allowed = flag_map.values_at(*flag_map.keys.sort)
model.validates_within name, model.options_with_message({ :set => allowed }, self, :within)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-types/flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DataMapper
class Property
class Flag < Integer

include Flags
include Types::Support::Flags

def initialize(model, name, options = {})
super
Expand Down
13 changes: 5 additions & 8 deletions lib/dm-types/paranoid_boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ class ParanoidBoolean < Boolean

# @api private
def bind
property_name = name.inspect
unless model < DataMapper::Types::Paranoid::Base
model.__send__ :include, DataMapper::Types::Paranoid::Base
end

model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
include DataMapper::Types::Paranoid::Base

set_paranoid_property(#{property_name}) { true }

default_scope(#{repository_name.inspect}).update(#{property_name} => false)
RUBY
model.set_paranoid_property(name) { true }
model.default_scope(repository_name).update(name => false)
end
end # class ParanoidBoolean
end # module Property
Expand Down
13 changes: 5 additions & 8 deletions lib/dm-types/paranoid_datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ class ParanoidDateTime < DateTime

# @api private
def bind
property_name = name.inspect
unless model < DataMapper::Types::Paranoid::Base
model.__send__ :include, DataMapper::Types::Paranoid::Base
end

model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
include DataMapper::Types::Paranoid::Base

set_paranoid_property(#{property_name}) { ::DateTime.now }

default_scope(#{repository_name.inspect}).update(#{property_name} => nil)
RUBY
model.set_paranoid_property(name) { ::DateTime.now }
model.default_scope(repository_name).update(name => nil)
end
end # class ParanoidDateTime
end # module Property
Expand Down
63 changes: 31 additions & 32 deletions lib/dm-types/support/flags.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
module DataMapper
class Property
module Flags
def self.included(base)
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
extend DataMapper::Property::Flags::ClassMethods
module Types
module Support
module Flags
def self.included(model)
model.extend ClassMethods
model.accept_options :flags
model.__send__ :attr_reader, :flag_map
model.instance_variable_set(:@generated_classes, {})

accept_options :flags
attr_reader :flag_map

class << self
class << model
attr_accessor :generated_classes
end
end

self.generated_classes = {}
RUBY
end

def custom?
true
end
def custom?
true
end

module ClassMethods
# TODO: document
# @api public
def [](*values)
if klass = generated_classes[values.flatten]
klass
else
klass = ::Class.new(self)
klass.flags(values)
module ClassMethods
# TODO: document
# @api public
def [](*values)
if klass = generated_classes[values.flatten]
klass
else
klass = ::Class.new(self)
klass.flags(values)

generated_classes[values.flatten] = klass
generated_classes[values.flatten] = klass

klass
klass
end
end
end
end
end
end
end
end # module ClassMethods

end # module Flags
end # module Support
end # module Types
end # module DataMapper