Class: ActiveOrient::Base
- Inherits:
-
Object
- Object
- ActiveOrient::Base
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
- Defined in:
- lib/base.rb
Overview
Base class for tableless IB data Models, extends ActiveModel API
Direct Known Subclasses
Constant Summary collapse
- @@rid_store =
Every Rest::Base-Object is stored in the @@rid_store The Objects are just references to the @@rid_store. any Change of the Object is thus synchonized to any allocated variable
Hash.new
Class Method Summary collapse
- .attr_accessible(*args) ⇒ Object
-
.attr_protected(*args) ⇒ Object
Noop methods mocking ActiveRecord::Base macros.
-
.belongs_to(model, *args) ⇒ Object
ActiveRecord::Base association API mocks.
- .display_riid ⇒ Object
- .find(*args) ⇒ Object
- .get_riid(link) ⇒ Object
- .has_many(models, *args) ⇒ Object
- .has_one(model, *args) ⇒ Object
- .remove_riid(obj) ⇒ Object
-
.serialize(*properties) ⇒ Object
ActiveRecord::Base misc.
- .store_riid(obj) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
ActiveModel-style read/write_attribute accessors Here we define the autoload mechanism.
-
#[]=(key, val) ⇒ Object
Here we define how the attributes are initialized Key and val are set by the RestCliend.
-
#attributes ⇒ Object
ActiveModel API (for serialization).
- #attributes=(attrs) ⇒ Object
-
#initialize(attributes = {}, opts = {}) ⇒ Base
constructor
If a opts hash is given, keys are taken as attribute names, values as data.
- #to_model ⇒ Object
- #update_attribute(key, value) ⇒ Object
Constructor Details
#initialize(attributes = {}, opts = {}) ⇒ Base
If a opts hash is given, keys are taken as attribute names, values as data. The model instance fields are then set automatically from the opts Hash.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/base.rb', line 50 def initialize attributes={}, opts={} logger.progname= "ActiveOrient::Base#initialize" #possible_link_array_candidates = link_candidates = Hash.new = HashWithIndifferentAccess.new # @edges = HashWithIndifferentAccess.new run_callbacks :initialize do # puts "initialize::attributes: #{attributes.inspect}" attributes.keys.each do | att | unless att[0] == "@" # @ identifies Metadata-attributes att = att.to_sym if att.is_a?(String) unless self.class.instance_methods.detect{|x| x == att } self.class.define_property att, nil # logger.debug { "property #{att.to_s} assigned to #{self.class.to_s}" } else # logger.info{ "property #{att.to_s} NOT assigned " } end end end if attributes['@type'] == 'd' # document [ :type ] = attributes.delete '@type' [ :class ] = attributes.delete '@class' [ :version ] = attributes.delete '@version' [ :fieldTypes ] = attributes.delete '@fieldTypes' if attributes.has_key?( '@rid' ) rid = attributes.delete '@rid' cluster, record = rid[1,rid.size].split(':') [ :cluster ] = cluster.to_i [ :record ] = record.to_i end #### edges -- remove in_ and out_ and de-capitalize the remaining edge if [ :fieldTypes ].present? && ([ :fieldTypes ] =~ /=g/) edges = ['fieldTypes'].split(',').find_all{|x| x=~/=g/}.map{|x| x.split('=').first} edges.each do |edge| operator, *base_edge = edge.split('_') base_edge = base_edge.join('_') unless self.class.instance_methods.detect{|x| x == base_edge } ## define two methods: out_{Edge}/{in_Edge} -> edge. self.class.define_property base_edge, nil self.class.send :alias_method, base_edge.underscore, edge # # logger.debug { "#{link}:: edge #{edge} assigned to #{self.class.to_s} and remaped to #{base_edge.underscore}" } end end end end self.attributes = attributes # set_attribute_defaults is now after_init callback end ActiveOrient::Base.store_riid self end |
Class Method Details
.attr_accessible(*args) ⇒ Object
192 193 |
# File 'lib/base.rb', line 192 def self.attr_accessible *args end |
.attr_protected(*args) ⇒ Object
Noop methods mocking ActiveRecord::Base macros
189 190 |
# File 'lib/base.rb', line 189 def self.attr_protected *args end |
.belongs_to(model, *args) ⇒ Object
ActiveRecord::Base association API mocks
197 198 199 |
# File 'lib/base.rb', line 197 def self.belongs_to model, *args attr_accessor model end |
.display_riid ⇒ Object
20 21 22 |
# File 'lib/base.rb', line 20 def self.display_riid @@rid_store end |
.find(*args) ⇒ Object
214 215 216 |
# File 'lib/base.rb', line 214 def self.find *args [] end |
.get_riid(link) ⇒ Object
26 27 28 |
# File 'lib/base.rb', line 26 def self.get_riid link end |
.has_many(models, *args) ⇒ Object
205 206 207 208 209 210 211 212 |
# File 'lib/base.rb', line 205 def self.has_many models, *args attr_accessor models define_method(models) do self.instance_variable_get("@#{models}") || self.instance_variable_set("@#{models}", []) end end |
.has_one(model, *args) ⇒ Object
201 202 203 |
# File 'lib/base.rb', line 201 def self.has_one model, *args attr_accessor model end |
.remove_riid(obj) ⇒ Object
23 24 25 |
# File 'lib/base.rb', line 23 def self.remove_riid obj @@rid_store[obj.riid]=nil end |
.serialize(*properties) ⇒ Object
ActiveRecord::Base misc
224 225 |
# File 'lib/base.rb', line 224 def self.serialize *properties end |
.store_riid(obj) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/base.rb', line 29 def self.store_riid obj if obj.rid.present? && obj.riid.all?{|x| x.present? && x>=0} # only positive values are stored ## return the presence of a stored object as true by the block ## the block is only executed if the presence is confirmed ## Nothing is returned from the class-method if @@rid_store[obj.riid].present? yield if block_given? end @@rid_store[obj.riid] = obj @@rid_store[obj.riid] # return_value else obj # no rid-value: just return the obj end end |
Instance Method Details
#[](key) ⇒ Object
ActiveModel-style read/write_attribute accessors Here we define the autoload mechanism
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/base.rb', line 119 def [] key iv= attributes[key.to_sym] # iv.from_orient unless iv.nil? if iv.is_a?(String) && iv.rid? #&& @metadata[:fieldTypes].present? && @metadata[:fieldTypes].include?( key.to_s+"=x" ) # puts "autoload: #{iv}" ActiveOrient::Model.autoload_object iv elsif iv.is_a?(Array) # && @metadata[:fieldTypes].present? && @metadata[:fieldTypes].match( key.to_s+"=[znmgx]" ) # puts "autoload: #{iv.inspect}" OrientSupport::Array.new self, *iv.map{|y| (y.is_a?(String) && y.rid?) ? ActiveOrient::Model.autoload_object( y ) : y } else if [:fieldTypes].present? && [:fieldTypes].include?( key.to_s+"=t" ) # time # switch between date and datetime representation of the date-object iv =~ /00:00:00/ ? Date.parse( iv ) : DateTime.parse( iv ) else iv end end end |
#[]=(key, val) ⇒ Object
Here we define how the attributes are initialized Key and val are set by the RestCliend
149 150 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 |
# File 'lib/base.rb', line 149 def []= key, val val = val.rid if val.is_a? ActiveOrient::Model # if val.is_a?(Array) # && @metadata[:fieldTypes].present? && @metadata[:fieldTypes].include?( key.to_s+"=n" ) # if @metadata[ :fieldTypes ] =~ /out=x,in=x/ # puts "VAL is a ARRAY" # else # puts "METADATA: #{ @metadata[ :fieldTypes ]} " # end # val# = val.map{|x| if val.is_a? ActiveOrient::Model then val.rid else val end } # end attributes[key.to_sym] = case val when Array if val.first.is_a?(Hash) v=val.map do |x| if x.is_a?( Hash ) HashWithIndifferentAccess.new(x) else x end end OrientSupport::Array.new( self, *v ) else OrientSupport::Array.new( self, *val ) end when Hash HashWithIndifferentAccess.new(val) else val end end |
#attributes ⇒ Object
ActiveModel API (for serialization)
109 110 111 |
# File 'lib/base.rb', line 109 def attributes @attributes ||= HashWithIndifferentAccess.new end |
#attributes=(attrs) ⇒ Object
113 114 115 |
# File 'lib/base.rb', line 113 def attributes= attrs attrs.keys.each { |key| self.send("#{key}=", attrs[key]) } end |
#to_model ⇒ Object
181 182 183 |
# File 'lib/base.rb', line 181 def to_model self end |
#update_attribute(key, value) ⇒ Object
142 143 144 |
# File 'lib/base.rb', line 142 def update_attribute key, value @attributes[key] = value end |