Class: Loveseat::Document::Support

Inherits:
Object
  • Object
show all
Defined in:
lib/loveseat/document/support.rb

Direct Known Subclasses

Loveseat::DesignDocument::Support

Constant Summary collapse

TYPE =
0
DEFAULT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Support

Returns a new instance of Support.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/loveseat/document/support.rb', line 9

def initialize(klass, options = {})
  @klass = klass
  @properties = {}
  @dsl = DSL.new(self)
  @abstract = !!options[:abstract]
  @singleton = !!options[:singleton]

  add_instance_adapter_accessor!

  unless @abstract
    add_property(:_id, Property::String)
    add_property(:_rev, Property::String)
  end
end

Instance Attribute Details

#dslObject

Returns the value of attribute dsl.



4
5
6
# File 'lib/loveseat/document/support.rb', line 4

def dsl
  @dsl
end

#propertiesObject

Returns the value of attribute properties.



4
5
6
# File 'lib/loveseat/document/support.rb', line 4

def properties
  @properties
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/loveseat/document/support.rb', line 69

def abstract?
  @abstract
end

#add_property(name, type, default = nil) ⇒ Object



24
25
26
27
28
# File 'lib/loveseat/document/support.rb', line 24

def add_property(name,type,default = nil)
  name = name.to_sym
  alter_property(name,type,default)
  add_instance_methods!(name)
end

#alter_property(name, type, default = nil) ⇒ Object



30
31
32
# File 'lib/loveseat/document/support.rb', line 30

def alter_property(name,type,default = nil)
  @properties[name] = [type, default]
end

#from_hash(doc) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/loveseat/document/support.rb', line 43

def from_hash(doc)
  object = nil
  if singleton?
    object = @klass
  else
    object = @klass.new
  end
  properties.each do |k,v|
    object.send(:"#{k}=", doc[k.to_s])
  end
  object
end

#generate_property_mapObject



56
57
58
59
60
61
62
63
# File 'lib/loveseat/document/support.rb', line 56

def generate_property_map
  map = {}
  properties.each do |name,value|
    type, default = value
    map[name] = type.new(default)
  end
  map
end

#singleton?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/loveseat/document/support.rb', line 65

def singleton?
  @singleton
end

#to_doc(instance) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/loveseat/document/support.rb', line 34

def to_doc(instance)
  doc = {}
  instance.__loveseat_instance_adapter.property_map.each do |k,v|
    value = v.get 
    doc[k] = value unless v.empty?
  end
  doc.to_json
end