Module: Yogo::Data::DefaultMethods::InstanceMethods

Defined in:
lib/yogo/data/default_methods.rb

Instance Method Summary collapse

Instance Method Details

#as_json(*a) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/yogo/data/default_methods.rb', line 77

def as_json(*a)
  data = labeled_properties.reduce({}){ |result, property| result.merge(property.name => __send__(property.name)) }
  default_data = unlabeled_properties.reduce({}){ |result, property| result.merge(property.name => __send__(property.name)) }
  default_data.merge({
    :url => self.to_url,
    :data => data
  })
end

#attributes_by_labelObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yogo/data/default_methods.rb', line 44

def attributes_by_label
  attributes = {}
  properties.each do |property|
    label = property.options['label']
    next if label.nil?
    name = property.name
    next unless model.public_method_defined?(name)
    attributes[label] = __send__(name)
  end
  return attributes
end

#labeled_propertiesObject



56
57
58
# File 'lib/yogo/data/default_methods.rb', line 56

def labeled_properties
  self.class.labeled_properties
end

#to_json(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/yogo/data/default_methods.rb', line 64

def to_json(*args)
  options = args.first || {}
  options = options.to_h if options.respond_to?(:to_h)

  result = as_json(*args)

  if options.fetch(:to_json, true)
    result.to_json
  else
    result
  end
end

#to_urlObject

Raises:

  • (Exception)


39
40
41
42
# File 'lib/yogo/data/default_methods.rb', line 39

def to_url
  raise Exception, "Need to save before item has a url" if new?
  "/data/" << self.model.name << "/" << yogo_id.to_s
end

#unlabeled_propertiesObject



60
61
62
# File 'lib/yogo/data/default_methods.rb', line 60

def unlabeled_properties
  self.class.unlabeled_properties
end