Class: Rod::Property::Base

Inherits:
Object
  • Object
show all
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

Field, PluralAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

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,options)
  check_class(klass)
  @klass = klass
  check_name(name)
  @name = name
  check_options(options)
  @options = options.dup.freeze
end

Instance Attribute Details

#nameObject (readonly)

The name of the property.



15
16
17
# File 'lib/rod/property/base.rb', line 15

def name
  @name
end

#optionsObject (readonly)

The options of the property.



18
19
20
# File 'lib/rod/property/base.rb', line 18

def options
  @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_findersObject

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_options = {}
  self.options.each{|k,v| self_options[k] = v.to_s.sub(LEGACY_RE,"")}
  other_options = {}
  other.options.each{|k,v| other_options[k] = v.to_s.sub(LEGACY_RE,"")}
  differences = {}
  self_options.each do |option,value|
    if other_options[option] != value
      differences[option] = [value,other_options[option]]
    end
  end
  other_options.each do |option,value|
    if self_options[option] != value && !differences.has_key?(option)
      differences[option] = [self_options[option],value]
    end
  end
  differences
end

#has_index?Boolean

Returns true if the property has an index.

Returns:

  • (Boolean)


59
60
61
# File 'lib/rod/property/base.rb', line 59

def has_index?
  !@options[:index].nil?
end

#indexObject

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_indexObject

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