Class: DataMapper::Is::Schemaless::Index
- Inherits:
-
Object
- Object
- DataMapper::Is::Schemaless::Index
- Defined in:
- lib/dm-is-schemaless/is/index.rb
Defined Under Namespace
Classes: IndexingError
Instance Attribute Summary collapse
-
#assoc_name ⇒ Object
Returns the value of attribute assoc_name.
-
#model ⇒ Object
Returns the value of attribute model.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#storage_name ⇒ Object
Returns the value of attribute storage_name.
Instance Method Summary collapse
- #build_resource(name, field, parent_resource) ⇒ Object
-
#initialize(resource, field, opts) ⇒ Index
constructor
A new instance of Index.
- #update_field_callbacks(model, field) ⇒ Object
Constructor Details
#initialize(resource, field, opts) ⇒ Index
Returns a new instance of Index.
9 10 11 12 13 14 15 |
# File 'lib/dm-is-schemaless/is/index.rb', line 9 def initialize(resource,field, opts) name = "#{field.to_s.camel_case}Index" @storage_name = Extlib::Inflection.tableize(name) @parent = :"#{resource.to_s.snake_case}" @model = build_resource(name, field, resource) update_field_callbacks(resource, field) end |
Instance Attribute Details
#assoc_name ⇒ Object
Returns the value of attribute assoc_name.
5 6 7 |
# File 'lib/dm-is-schemaless/is/index.rb', line 5 def assoc_name @assoc_name end |
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/dm-is-schemaless/is/index.rb', line 5 def model @model end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/dm-is-schemaless/is/index.rb', line 5 def parent @parent end |
#storage_name ⇒ Object
Returns the value of attribute storage_name.
5 6 7 |
# File 'lib/dm-is-schemaless/is/index.rb', line 5 def storage_name @storage_name end |
Instance Method Details
#build_resource(name, field, parent_resource) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dm-is-schemaless/is/index.rb', line 34 def build_resource(name, field, parent_resource) klass = Object.const_set(name, Class.new) klass.send(:include, DataMapper::Resource) klass.property field.to_sym, String, :key => true parent_resource.key.each do |prop| klass.property :"#{@parent}_#{prop.name}", prop.type, :key => true end klass.belongs_to @parent, :parent_key => parent_resource.key.map{|k| k.name } klass end |
#update_field_callbacks(model, field) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dm-is-schemaless/is/index.rb', line 17 def update_field_callbacks(model, field) self.assoc_name = @model.to_s.snake_case model.has 1, assoc_name.to_sym model.class_eval " def update_\#{field}_index\n if body.key?('\#{field}')\n self.\#{assoc_name} ||= \#{@model}.new\n \#{assoc_name}.\#{field} = body['\#{field}']\n elsif \#{assoc_name} && \#{assoc_name}.destroy\n self.\#{assoc_name} = nil\n else\n end\n end\n RUBY\n model.before :save, :\"update_\#{field}_index\"\nend\n", __FILE__, __LINE__ + 1 |