Class: Yogo::Collection::Property

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/yogo/collection/property.rb

Direct Known Subclasses

Relationship

Constant Summary collapse

COMMON_PROPERTIES =
[:String, :Text, :Integer, :Float, :Boolean, :Date, :Time, :DateTime]

Instance Method Summary collapse

Instance Method Details

#add_to_model(model) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/yogo/collection/property.rb', line 52

def add_to_model(model)
  prop_name = self.to_s.intern
  prop_type = self.class
  prop_options   = self.options || {}
  
  model.send(model_method, prop_name, prop_type, prop_options)
end

#as_json(opts = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/yogo/collection/property.rb', line 29

def as_json(opts=nil)
  {
    :id => self.id.to_s,
    :type => self.type.to_s,
    :field_name => self.to_s,
    :name => self.name,
    :options => self.options,
    :data_collection => self.data_collection_id.to_s
  }
end

#field_nameObject

validates_uniqueness_of :name, :scope => :data_collection_id



21
22
23
# File 'lib/yogo/collection/property.rb', line 21

def field_name
  self.to_s
end

#model_methodObject



48
49
50
# File 'lib/yogo/collection/property.rb', line 48

def model_method
  :property
end

#to_sObject



25
26
27
# File 'lib/yogo/collection/property.rb', line 25

def to_s
  'field_' + self.id.to_s.gsub('-','_')
end

#update_attributes(hash) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/yogo/collection/property.rb', line 40

def update_attributes(hash)
  attrs = {}
  attrs[:name] = hash[:name] || hash['name'] || self.name
  attrs[:type] = hash[:type] || hash['type'] || self.type || self.class.name
  attrs[:options] = hash[:options] || hash['options'] || self.options
  self.attributes = attrs
end