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
-
._build_query(query, resource_class = nil) ⇒ Object
Query: resource: Resource to find properties for name: Name of property value: Value of property.
- ._create_resource(query_result) ⇒ Object
- .count(query) ⇒ Object
- .create(attr = {}) ⇒ 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 159 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 |
._build_query(query, resource_class = nil) ⇒ Object
Query:
resource: Resource to find properties for
name: Name of property
value: Value of property
Return the FROM and WHERE elements for ‘query’
TODO: THIS IS REALLY BROKEN! Need to define what a query is in this context
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 95 def self._build_query(query, resource_class = nil) #puts ">>>QUERY>>> #{query}" i = 0 resource = nil where = query.map do |pn, v| tbl = "p#{i}" case pn.to_sym when :resource unless v.is_a? OResource raise "Expected type OResource for :resource, but got '#{v}::#{v.class}'" end resource = v "r.id = #{resource.id}" #"#{tbl}.o_resource_id = #{v.id}" # resource property 'name' is not stored as a property but as a column in the resource # when :name # "r.name = '#{v}'" else h = _analyse_value(v) i += 1 if (val = h[:v]).is_a? String val = "'#{val}'" end op = (h[:t] == 's' ? 'LIKE' : '=') # TODO: Would also need to add a where clause "#{tbl}.name = #{pn} " "#{tbl}.#{h[:f]} #{op} #{val}" end 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 (why not?) [from, where] end |
._create_resource(query_result) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 135 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 |
.count(query) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 78 def self.count(query) from, where = self._build_query(query) q = "SELECT COUNT(r.id) FROM #{from.join(', ')} WHERE #{where.join(' AND ')};" res = repository(:default).adapter.select(q) #debug "count res: #{res} q: #{query} sql: #{q}" res[0] end |
.create(attr = {}) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 50 def self.create(attr = {}) if (value = attr.delete(:value)) h = self._analyse_value(value) attr[h[:f]] = h[:v] attr[:type] = h[:t] end super end |
.prop_all(query, opts = {}, resource_class = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 59 def self.prop_all(query, opts = {}, resource_class = nil) from, where = self._build_query(query, resource_class) 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
228 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 228 alias_method :_dirty_self?, :dirty_self? |
#dirty_self? ⇒ Boolean
229 230 231 232 233 234 235 236 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 229 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
238 239 240 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 238 def to_hash {name: self.name, value: self.value} end |
#to_json(*args) ⇒ Object
242 243 244 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 242 def to_json(*args) to_hash.to_json(*args) end |
#to_s ⇒ Object
246 247 248 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 246 def to_s() "#<#{self.class} id: #{self.id} subj: #{self.o_resource} name: #{self.name} value: #{self.value}>" end |
#valid?(context = :default) ⇒ Boolean
217 218 219 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 217 def valid?(context = :default) self.name != nil #&& self.value != nil end |
#value ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 180 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
151 152 153 154 155 156 157 |
# File 'lib/omf-sfa/resource/oproperty.rb', line 151 def value=(val) h = self.class._analyse_value(val) #puts "VALUE>>> #{h}" attribute_set(h[:f], h[:v]) attribute_set(:type, h[:t]) save end |