module Bodhi::Properties

This module helps turn a Ruby Object into a Form Object. It manages the objects properties as well as serialization to JSON @example

class MyNewClass
  include Bodhi::properties

  property :attribute_1, type: String
  property :attribute_2, type: DateTime
  ...
end

object = MyNewClass.new(attribute_1: "Foo", attribute_2: Time.now)
object.to_json #=> {"attribute_1":"Foo", "attribute_2": "2016-08-16T13:15:19-07:00"}

Constants

SYSTEM_PROPERTIES

Default properties for ALL records on the IoT Platform

Public Class Methods

included(base) click to toggle source
# File lib/bodhi-slam/properties.rb, line 159
def self.included(base)
  base.extend(ClassMethods)
  base.instance_variable_set(:@properties, Hash.new)
end
new(options={}) click to toggle source

Initializes a new instance of the class. Accepts a parameter Hash for mass assignment.

@param options [Hash, JSON String] @example

klass = Class.new do
  include Bodhi::Properties
  property :name, :email
end

object = klass.new(name: "Bob", email: "some@email.com")
object.name #=> "Bob"
object.email #=> "some@email.com"
# File lib/bodhi-slam/properties.rb, line 74
def initialize(options={})
  if options.is_a?(String)
    options = JSON.parse(options)
  end

  # Set properties defined by the +options+ parameter
  #options = Bodhi::Support.symbolize_keys(options)
  options.each do |property, value|
    property_options = self.class.properties[property.to_sym]
    if property_options.nil?
      send("#{property}=", value)
    else
      send("#{property}=", Bodhi::Support.coerce(value, property_options))
    end
  end

  # Set any default values
  self.class.properties.select{ |k,v| !v[:default].nil? }.each do |property, property_options|
    send("#{property}=", property_options[:default]) if send("#{property}").nil?
  end
end

Public Instance Methods

attributes() click to toggle source

Returns a Hash of the class properties and their values.

@return [Hash] @example

object = SomeResource.new(foo:"test", bar:12345)
object.attributes # => { foo: "test", bar: 12345 }
# File lib/bodhi-slam/properties.rb, line 102
def attributes
  attributes = Hash.new

  self.class.property_names.each do |property|
    value = send(property)
    if value.respond_to?(:attributes)
      attributes[property] = value.attributes.delete_if { |k, v| v.nil? }
    elsif value.is_a?(Array) && value.first.respond_to?(:attributes)
      attributes[property] = value.map(&:attributes).collect{ |item| item.delete_if { |k, v| v.nil? } }
    elsif value.is_a?(Time)
      attributes[property] = value.iso8601
    else
      attributes[property] = value
    end
  end

  attributes.delete_if { |k, v| v.nil? }
  attributes
end
id() click to toggle source

Wraps the sys_id property into a more Rails friendly attribute @return [String]

# File lib/bodhi-slam/properties.rb, line 52
def id; @sys_id; end
new_record?() click to toggle source

Returns true if the record has NOT been saved to the IoT Platform @return [Boolean]

# File lib/bodhi-slam/properties.rb, line 60
def new_record?; @sys_id.nil?; end
persisted?() click to toggle source

Returns true if the record has been saved to the IoT Platform @return [Boolean]

# File lib/bodhi-slam/properties.rb, line 56
def persisted?; !@sys_id.nil?; end
to_json(options=nil) click to toggle source

Returns all the classes properties as JSON. It converts any nested objects to JSON if they respond to to_json

@param options [Hash] @return [String] the JSON for all properties on the object @example

resource = SomeResource.new(foo:"test", bar:12345)
embedded_resources = AnotherResource.new( test: resource )

resource.to_json # => "{ 'foo':'test', 'bar':12345 }"
embedded_resources.to_json # => "{ 'test': { 'foo':'test', 'bar':12345 } }"
# File lib/bodhi-slam/properties.rb, line 155
def to_json(options=nil)
  attributes.to_json
end
update_attributes(properties) click to toggle source

Updates the resource with the given attributes Hash

@param properties [Hash] The properties to update @return [nil] @example

s = SomeResource.factory.build(foo:"test", bar:12345)
s.attributes # => { foo: "test", bar: 12345 }
s.update_attributes(bar: 10)
s.attributes # => { foo: "test", bar: 10 }
# File lib/bodhi-slam/properties.rb, line 131
def update_attributes(properties)
  properties.each do |property, value|
    property_options = self.class.properties[property.to_sym]
    if property_options.nil?
      send("#{property}=", value)
    else
      send("#{property}=", Bodhi::Support.coerce(value, property_options))
    end
  end

  return nil
end