Class: Rod::Property::Base
- Inherits:
-
Object
- Object
- Rod::Property::Base
- Defined in:
- lib/rod/property/base.rb
Overview
This class defines the properties which are used in Rod::Model. These might be:
-
fields
-
has_one associations
-
has_many associations
It provides basic data concerning these properties, such as name, options, etc.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of the property.
-
#options ⇒ Object
readonly
The options of the property.
Instance Method Summary collapse
-
#copy(klass) ⇒ Object
Creates a copy of the property with a new
klass
. -
#define_finders ⇒ Object
Defines finders (
find_by
andfind_all_by
) for indexed property. -
#difference(other) ⇒ Object
Checks the difference in options between this and the
other
property. -
#has_index? ⇒ Boolean
Returns true if the property has an index.
-
#index ⇒ Object
Returns the index associated with the property.
-
#initialize(klass, name, options) ⇒ Base
constructor
Initializes the property associated with
klass
with itsname
andoptions
. -
#reset_index ⇒ Object
Get rid of the index that is associated with this property.
Constructor Details
#initialize(klass, name, options) ⇒ Base
Initializes the property associated with klass
with its name
and options
.
22 23 24 25 26 27 28 29 |
# File 'lib/rod/property/base.rb', line 22 def initialize(klass,name,) check_class(klass) @klass = klass check_name(name) @name = name () @options = .dup.freeze end |
Instance Attribute Details
#name ⇒ Object (readonly)
The name of the property.
15 16 17 |
# File 'lib/rod/property/base.rb', line 15 def name @name end |
#options ⇒ Object (readonly)
The options of the property.
18 19 20 |
# File 'lib/rod/property/base.rb', line 18 def @options end |
Instance Method Details
#copy(klass) ⇒ Object
Creates a copy of the property with a new klass
.
69 70 71 |
# File 'lib/rod/property/base.rb', line 69 def copy(klass) self.class.new(klass,@name,@options) end |
#define_finders ⇒ Object
Defines finders (find_by
and find_all_by
) for indexed property.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rod/property/base.rb', line 74 def define_finders return unless has_index? # optimization name = @name.to_s property = self (class << @klass; self; end).class_eval do # Find all objects with given +value+ of the +property+. define_method("find_all_by_#{name}") do |value| property.index[value] end # Find first object with given +value+ of the +property+. define_method("find_by_#{name}") do |value| property.index[value][0] end end end |
#difference(other) ⇒ Object
Checks the difference in options between this and the other
property. The prefix of legacy module is removed from the values of the options.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rod/property/base.rb', line 34 def difference(other) = {} self..each{|k,v| [k] = v.to_s.sub(LEGACY_RE,"")} = {} other..each{|k,v| [k] = v.to_s.sub(LEGACY_RE,"")} differences = {} .each do |option,value| if [option] != value differences[option] = [value,[option]] end end .each do |option,value| if [option] != value && !differences.has_key?(option) differences[option] = [[option],value] end end differences end |
#has_index? ⇒ Boolean
Returns true if the property has an index.
59 60 61 |
# File 'lib/rod/property/base.rb', line 59 def has_index? !@options[:index].nil? end |
#index ⇒ Object
Returns the index associated with the property.
54 55 56 |
# File 'lib/rod/property/base.rb', line 54 def index @index ||= Index::Base.create(path(@klass.database.path),@klass,@options) end |
#reset_index ⇒ Object
Get rid of the index that is associated with this property.
64 65 66 |
# File 'lib/rod/property/base.rb', line 64 def reset_index @index = nil end |