Class: Spider::Model::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/model/element.rb

Direct Known Subclasses

IntegratedElement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, attributes = {}) ⇒ Element

Returns a new instance of Element.



11
12
13
14
15
# File 'lib/spiderfw/model/element.rb', line 11

def initialize(name, type, attributes={})
    @name = name
    @type = type
    @attributes = attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/spiderfw/model/element.rb', line 9

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/spiderfw/model/element.rb', line 8

def name
  @name
end

Instance Method Details

#associationObject

Named association.



142
143
144
# File 'lib/spiderfw/model/element.rb', line 142

def association
    self.attributes[:association]
end

#association_typeObject

The model used for the association. The BaseModel will automatically create a junction model for many to many relationships, unless one is supplied with the :through attribute. This will be set for n <-> n relationships, and will be nil otherwise.



37
38
39
# File 'lib/spiderfw/model/element.rb', line 37

def association_type
    @association_type ||= self.attributes[:association_type]
end

#autogenerated?Boolean

True if the element is generated on save by the model or the mapper.

Returns:

  • (Boolean)


137
138
139
# File 'lib/spiderfw/model/element.rb', line 137

def autogenerated?
    return (self.attributes[:auto] || self.attributes[:autoincrement])
end

#cloneObject



193
194
195
# File 'lib/spiderfw/model/element.rb', line 193

def clone
    self.class.new(@name, @type, self.attributes.clone)
end

#conditionObject

The element’s Condition, if any. If a condition is set with the :condition attribute, the association to the element’s model will be filtered by it.



169
170
171
172
173
# File 'lib/spiderfw/model/element.rb', line 169

def condition
    cond = attributes[:condition]
    cond = Condition.new(cond) if (cond && !cond.is_a?(Condition))
    return cond
end

#embedded?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/spiderfw/model/element.rb', line 132

def embedded?
    self.attributes[:embedded]
end

#extended?Boolean

True if the element type has been extended by passing a block to Model::BaseModel.element

Returns:

  • (Boolean)


118
119
120
# File 'lib/spiderfw/model/element.rb', line 118

def extended?
    self.attributes[:extended]
end

#has_single_reverse?Boolean

True if the element has a reverse, and that reverse is not multiple

Returns:

  • (Boolean)


108
109
110
# File 'lib/spiderfw/model/element.rb', line 108

def has_single_reverse?
    return true if self.attributes[:reverse] && !model.elements[self.attributes[:reverse]].multiple?
end

#hidden?Boolean

True if the :hidden attribute is set.

Returns:

  • (Boolean)


123
124
125
# File 'lib/spiderfw/model/element.rb', line 123

def hidden?
    self.attributes[:hidden]
end

#inline?Boolean

True if the element model is an InlineModel

Returns:

  • (Boolean)


113
114
115
# File 'lib/spiderfw/model/element.rb', line 113

def inline?
    self.attributes[:inline]
end

#integrated?Boolean

True if the element is integrated from another one. (See also BaseModel#integrate). Example:

class Address < BaseModel
  element :street, String
  element :area_code, String
end
class Person < BaseModel
  element :name, String
  element :address, Address
  integrate :address
end
Person.elements[:street].integrated? => true
Person.elements[:street].integrated_from => :address
Person.elements[:street].integrated_from_element => :street

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/spiderfw/model/element.rb', line 76

def integrated?
    return @is_integrated if @is_integrated != nil
    @is_integrated = self.attributes[:integrated_from]
end

#integrated_fromObject

If the element is integrated, the element from which it is taken. See also #integrated?.



82
83
84
# File 'lib/spiderfw/model/element.rb', line 82

def integrated_from
    self.attributes[:integrated_from]
end

#integrated_from_elementObject

If the element is integrated, the element corresponding to this in the model corresponding to the #integrated_from element. See also #integrated?.



88
89
90
# File 'lib/spiderfw/model/element.rb', line 88

def integrated_from_element
    self.attributes[:integrated_from_element]
end

#junction?Boolean

True if the element model is a junction (for a many to many relationship).

Returns:

  • (Boolean)


30
31
32
# File 'lib/spiderfw/model/element.rb', line 30

def junction?
    self.attributes[:junction]
end

#labelObject

Label. Will use the :label attribute, or return the name split by ‘_’ with each word capitalized.



147
148
149
# File 'lib/spiderfw/model/element.rb', line 147

def label
    return self.attributes[:label] ? _(self.attributes[:label]) : Inflector.underscore_to_upcasefirst(@name.to_s)
end

#lazyObject

Lazy attribute. (See #lazy_groups).



176
177
178
# File 'lib/spiderfw/model/element.rb', line 176

def lazy
    attributes[:lazy]
end

#lazy?Boolean

True if lazy attribute is set. (See #lazy_groups).

Returns:

  • (Boolean)


181
182
183
# File 'lib/spiderfw/model/element.rb', line 181

def lazy?
    attributes[:lazy]
end

#lazy_groupsObject

Returns the lazy groups this elements is in, as set by BaseModel#element with the :lazy attributes. Lazy groups are used by the mapper to determine which elements to autoload: when an element in a lazy group is accessed, all the elements in the same group(s) will be loaded.



188
189
190
191
# File 'lib/spiderfw/model/element.rb', line 188

def lazy_groups
    return nil unless attributes[:lazy] && attributes[:lazy] != true
    return attributes[:lazy].is_a?(Array) ? attributes[:lazy] : [attributes[:lazy]]
end

#mapperObject

Mapper for the element’s #model.



162
163
164
165
# File 'lib/spiderfw/model/element.rb', line 162

def mapper
    return nil unless model?
    return self.model.mapper
end

#modelObject

The actual model used to represent the association. Will return the #association_type or the #type.



24
25
26
27
# File 'lib/spiderfw/model/element.rb', line 24

def model
    return nil unless model?
    return association_type || type
end

#model?Boolean

True if the element defines an association to another model.

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/spiderfw/model/element.rb', line 57

def model?
    return @is_model if @is_model != nil
    @is_model = (type < Spider::Model::BaseModel || association_type)
end

#multiple?Boolean

True if the element defines a 1|n -> n association.

Returns:

  • (Boolean)


42
43
44
# File 'lib/spiderfw/model/element.rb', line 42

def multiple?
    self.attributes[:multiple]
end

#owned?Boolean

True if only the defining BaseModel holds references to the associated

Returns:

  • (Boolean)


128
129
130
# File 'lib/spiderfw/model/element.rb', line 128

def owned?
    self.attributes[:owned]
end

#primary_key?Boolean

True if the element is a primary key.

Returns:

  • (Boolean)


93
94
95
# File 'lib/spiderfw/model/element.rb', line 93

def primary_key?
    @primary_key ||= self.attributes[:primary_key]
end

#read_only?Boolean

True if the element is read only.

Returns:

  • (Boolean)


98
99
100
# File 'lib/spiderfw/model/element.rb', line 98

def read_only?
    self.attributes[:read_only]
end

#required?Boolean

True if the element must have a value.

Returns:

  • (Boolean)


47
48
49
# File 'lib/spiderfw/model/element.rb', line 47

def required?
    self.attributes[:required]
end

#reverseObject

The reverse element in the relationship to another model.



103
104
105
# File 'lib/spiderfw/model/element.rb', line 103

def reverse
    self.attributes[:reverse]
end

#storageObject

Storage for the element’s #model.



156
157
158
159
# File 'lib/spiderfw/model/element.rb', line 156

def storage
    return nil unless model?
    return self.mapper.storage
end

#to_sObject



151
152
153
# File 'lib/spiderfw/model/element.rb', line 151

def to_s
    return "Element '#{@name.to_s}'"
end

#typeObject

The element type, as per the second argument passed to the Model::BaseModel.element method. This may be different from #model.



19
20
21
# File 'lib/spiderfw/model/element.rb', line 19

def type
    @type
end

#unique?Boolean

True if no two model instances can have the same value for the element.

Returns:

  • (Boolean)


52
53
54
# File 'lib/spiderfw/model/element.rb', line 52

def unique?
    self.attributes[:unique]
end