Module: Yogo::Data::DefaultMethods::InstanceMethods
- Defined in:
- lib/yogo/data/default_methods.rb
Instance Method Summary collapse
- #as_json(*a) ⇒ Object
- #attributes_by_label ⇒ Object
- #labeled_properties ⇒ Object
- #to_json(*args) ⇒ Object
- #to_url ⇒ Object
- #unlabeled_properties ⇒ Object
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_label ⇒ Object
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.['label'] next if label.nil? name = property.name next unless model.public_method_defined?(name) attributes[label] = __send__(name) end return attributes end |
#labeled_properties ⇒ Object
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) = args.first || {} = .to_h if .respond_to?(:to_h) result = as_json(*args) if .fetch(:to_json, true) result.to_json else result end end |
#to_url ⇒ Object
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_properties ⇒ Object
60 61 62 |
# File 'lib/yogo/data/default_methods.rb', line 60 def unlabeled_properties self.class.unlabeled_properties end |