Class: OMF::SFA::Resource::OProperty
- Inherits:
-
Base::LObject
- Object
- Base::LObject
- OMF::SFA::Resource::OProperty
- Includes:
- DataMapper::Resource
- Defined in:
- lib/omf-sfa/resource/oproperty.rb
Overview
Each resource will have a few properties.
Constant Summary collapse
- @@name2class =
{}
Class Method Summary collapse
-
._analyse_value(val) ⇒ Object
end.
- ._create_resource(query_result) ⇒ Object
- .prop_all(query, opts = {}, resource_class = nil) ⇒ Object
Instance Method Summary collapse
-
#_dirty_self? ⇒ Object
end.
- #dirty_self? ⇒ Boolean
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
- #valid?(context = :default) ⇒ Boolean
- #value ⇒ Object
- #value=(val) ⇒ Object
Class Method Details
._analyse_value(val) ⇒ Object
end
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 130 def self._analyse_value(val) if val.is_a? Numeric return {v: val, t: ((val.is_a? Integer) ? 'i' : 'f'), f: :n_value} elsif val.is_a? String return {v: val, t: 's', f: :s_value} elsif val.is_a? Symbol return {v: val.to_s, t: 's', f: :s_value} elsif val.is_a? OResource return {v: val.uuid.to_s, t: 'r', f: :s_value} elsif val.is_a? Time return {v: val.to_i, t: 't', f: :n_value} elsif val.class.included_modules.include?(DataMapper::Resource) #puts "SET>>>>> #{val}:#{val.class}" return {v: "#{val.class}@#{val.id}", t: 'd', f: :s_value} else #debug "SETTING VALUE> Class: #{val.class}" return {v: JSON.generate([val]), t: 'o', f: :s_value} end end |
._create_resource(query_result) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 87 def self._create_resource(query_result) qr = query_result unless klass = @@name2class[qr.type] begin klass = qr.type.split('::').inject(Object) do |mod, class_name| mod.const_get(class_name) end rescue Exception => ex warn "Can't find class '#{qr.type}' for resource - #{ex}" return nil end @@name2class[qr.type] = klass end klass.first(id: qr.id, uuid: qr.uuid, name: qr.name) # TODO: Does this create a DB call? end |
.prop_all(query, opts = {}, resource_class = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 50 def self.prop_all(query, opts = {}, resource_class = nil) i = 0 where = query.map do |pn, v| h = _analyse_value(v) tbl = "p#{i}" i += 1 if (val = h[:v]).is_a? String val = "'#{val}'" end "#{tbl}.#{h[:f]} #{h[:t] == 's' ? 'LIKE' : '='} #{val}" end i.times do |j| where << "r.id = p#{j}.o_resource_id" end where << "r.type = '#{resource_class}'" if resource_class table = storage_names[:default] from = i.times.map {|j| "#{table} AS p#{j}" } from << "omf_sfa_resource_o_resources AS r" # TODO: Shouldn't hard-code that q = "SELECT DISTINCT r.id, r.type, r.uuid, r.name FROM #{from.join(', ')} WHERE #{where.join(' AND ')};" if l = opts[:limit] q += " LIMIT #{l} OFFSET #{opts[:offset] || 0}" end debug "prop_all q: #{q}" res = repository(:default).adapter.select(q) ores = res.map do |qr| if resource_class resource_class.first(id: qr.id, uuid: qr.uuid, name: qr.name) # TODO: Does this create a DB call? else _create_resource(qr) end end #puts "RES>>> #{ores}" ores end |
Instance Method Details
#_dirty_self? ⇒ Object
end
199 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 199 alias_method :_dirty_self?, :dirty_self? |
#dirty_self? ⇒ Boolean
200 201 202 203 204 205 206 207 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 200 def dirty_self? #puts "#{object_id} DIRTY CHECK #{@value_dirty}" return true if @value_dirty || _dirty_self? if @old_value_ return @old_value_ != @value_ end false end |
#to_hash ⇒ Object
209 210 211 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 209 def to_hash {name: self.name, value: self.value} end |
#to_json(*args) ⇒ Object
213 214 215 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 213 def to_json(*args) to_hash.to_json(*args) end |
#to_s ⇒ Object
217 218 219 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 217 def to_s() "#<#{self.class} id: #{self.id} subj: #{self.o_resource} name: #{self.name} value: #{self.value}>" end |
#valid?(context = :default) ⇒ Boolean
188 189 190 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 188 def valid?(context = :default) self.name != nil #&& self.value != nil end |
#value ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 151 def value() case type = attribute_get(:type) when 'i' val = attribute_get(:n_value).to_i when 'f' val = attribute_get(:n_value) when 's' val = attribute_get(:s_value) when 'r' uuid_s = attribute_get(:s_value) uuid = UUIDTools::UUID.parse(uuid_s) #puts "RRRR >> #{uuid}" #puts "OOOO >> #{OResource.all.to_a}" val = OResource.first(uuid: uuid) when 't' val = Time.at(attribute_get(:n_value)) when 'd' v = attribute_get(:s_value) klass_s, id_s = v.split('@') klass = klass_s.split('::').inject(Kernel) {|k, s| k.const_get(s) } val = klass.first(id: id_s.to_i) #puts "GET>>>>> #{v} - #{val}" when 'o' js = attribute_get(:s_value) #debug "GET VALUE> <#{js}>" # http://www.ruby-lang.org/en/news/2013/02/22/json-dos-cve-2013-0269/ val = JSON.load(js)[0] #puts "VALUE: #{js.inspect}-#{val.inspect}-#{val.class}" if val.kind_of? Array #val.tap {|v| v.extend(OPropertyArray).instance_variable_set(:@oproperty, self) } end else throw "Unknown property type '#{type}'" end return val end |
#value=(val) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 103 def value=(val) h = self.class._analyse_value(val) attribute_set(h[:f], h[:v]) attribute_set(:type, h[:t]) save end |