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.



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

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

#definer_modelObject

Returns the value of attribute definer_model.



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

def definer_model
  @definer_model
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.



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

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.



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

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)


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

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

#cloneObject



203
204
205
# File 'lib/spiderfw/model/element.rb', line 203

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.



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

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

#embedded?Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

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)


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

def hidden?
    self.attributes[:hidden]
end

#inline?Boolean

True if the element model is an InlineModel

Returns:

  • (Boolean)


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

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)


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

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?.



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

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?.



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

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)


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

def junction?
    self.attributes[:junction]
end

#labelObject

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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/spiderfw/model/element.rb', line 148

def label
    prev_text_domain = nil
    if @definer_model && @definer_model != Spider::Model::Managed
        prev_text_domain = FastGettext.text_domain
        FastGettext.text_domain = @definer_model.app.short_name if FastGettext.translation_repositories.key?(@definer_model.app.short_name)
    end
    l = self.attributes[:label] ? _(self.attributes[:label]) : Inflector.underscore_to_upcasefirst(@name.to_s)
    if prev_text_domain
        FastGettext.text_domain = prev_text_domain
    end
    l
end

#lazyObject

Lazy attribute. (See #lazy_groups).



186
187
188
# File 'lib/spiderfw/model/element.rb', line 186

def lazy
    attributes[:lazy]
end

#lazy?Boolean

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

Returns:

  • (Boolean)


191
192
193
# File 'lib/spiderfw/model/element.rb', line 191

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.



198
199
200
201
# File 'lib/spiderfw/model/element.rb', line 198

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.



172
173
174
175
# File 'lib/spiderfw/model/element.rb', line 172

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.



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

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)


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

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)


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

def multiple?
    self.attributes[:multiple]
end

#owned?Boolean

True if only the defining BaseModel holds references to the associated

Returns:

  • (Boolean)


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

def owned?
    self.attributes[:owned]
end

#primary_key?Boolean

True if the element is a primary key.

Returns:

  • (Boolean)


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

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

#read_only?Boolean

True if the element is read only.

Returns:

  • (Boolean)


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

def read_only?
    self.attributes[:read_only]
end

#required?Boolean

True if the element must have a value.

Returns:

  • (Boolean)


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

def required?
    self.attributes[:required]
end

#reverseObject

The reverse element in the relationship to another model.



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

def reverse
    self.attributes[:reverse]
end

#storageObject

Storage for the element’s #model.



166
167
168
169
# File 'lib/spiderfw/model/element.rb', line 166

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

#to_sObject



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

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.



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

def type
    @type
end

#unique?Boolean

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

Returns:

  • (Boolean)


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

def unique?
    self.attributes[:unique]
end