Class: DynamicModel::PropertiesData
- Inherits:
-
Object
- Object
- DynamicModel::PropertiesData
show all
- Defined in:
- lib/dynamic_model/properties_data.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(entity_record, options = {}) ⇒ PropertiesData
Returns a new instance of PropertiesData.
8
9
10
11
12
13
|
# File 'lib/dynamic_model/properties_data.rb', line 8
def initialize(entity_record, options={})
@entity_record = entity_record
@options = options
@properties_cache = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
15
16
17
|
# File 'lib/dynamic_model/properties_data.rb', line 15
def method_missing(method_name, *args, &block)
get(method_name.to_s)
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3
4
5
|
# File 'lib/dynamic_model/properties_data.rb', line 3
def options
@options
end
|
Instance Method Details
#entity_record ⇒ Object
25
26
27
|
# File 'lib/dynamic_model/properties_data.rb', line 25
def entity_record
@entity_record
end
|
#field_class ⇒ Object
29
30
31
|
# File 'lib/dynamic_model/properties_data.rb', line 29
def field_class
@field_class ||= @options[:field_class]
end
|
#field_value_class ⇒ Object
33
34
35
|
# File 'lib/dynamic_model/properties_data.rb', line 33
def field_value_class
@field_value_class ||= @options[:field_value_class]
end
|
#get(field_name, def_value = nil, opts = {}) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/dynamic_model/properties_data.rb', line 37
def get(field_name, def_value=nil, opts={})
version = opts[:version] || nil
v_cache, cache_exist = cache_get_value field_name, version
return v_cache if cache_exist
if field_name.is_a?(String)
field = get_field_by_name field_name
else
field = field_name
end
return nil if field.nil?
if field.multivalue
value_records = get_value_records field.id, version
v = value_records.map{|r| get_value(r)}
else
value_record = get_value_record field.id, version
return nil if value_record.nil?
v = get_value value_record
end
cache_set_value field_name, version, v
v
end
|
#get_value(value_record) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/dynamic_model/properties_data.rb', line 87
def get_value(value_record)
type_id = value_record.field.type_id
if [FieldType::INT, FieldType::REFERENCE].include? type_id
return value_record.value.to_i
elsif [FieldType::TEXT].include? type_id
return value_record.value_text
end
value_record.value
end
|
#get_value_record(field_id, version = nil) ⇒ Object
68
69
70
|
# File 'lib/dynamic_model/properties_data.rb', line 68
def get_value_record(field_id, version=nil)
field_value_class.where(entity_id: entity_record.id, field_id: field_id, version: version).first
end
|
#get_value_records(field_id, version = nil) ⇒ Object
72
73
74
|
# File 'lib/dynamic_model/properties_data.rb', line 72
def get_value_records(field_id, version=nil)
field_value_class.where(entity_id: entity_record.id, field_id: field_id, version: version).all.to_a
end
|
#properties_cache ⇒ Object
19
20
21
22
23
|
# File 'lib/dynamic_model/properties_data.rb', line 19
def properties_cache
return @properties_cache unless @properties_cache.nil?
@properties_cache = {}
end
|
#set(field_name, value, opts = {}) ⇒ Object
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/dynamic_model/properties_data.rb', line 76
def set(field_name, value, opts={})
field = get_field_by_name field_name
raise 'Field not found' if field.nil?
if field.multivalue
set_multivalue_field_value field, value, opts
else
set_field_value field, value, opts
end
end
|