module Seatbelt::Property::InstanceMethods

Public Instance Methods

properties(role = :accessibles) click to toggle source

Public: Gets a list of key-value pairs that includes the properties with its values.

role - The scope of properties that will be selected

Defaults to :accessibles which will only select properties that 
are defined as accessible.

Returns an Hash containing the property names and its values.

# File lib/seatbelt/core/property.rb, line 108
def properties(role = :accessibles)
  injector = lambda do |result, item|
    result[item] = self.send(item)
    result
  end
  list = case role
    when :accessibles then
      self.class.accessible_properties.inject({}, &injector)
    when :all then
      all = self.class.send(:property_list).inject({}, &injector)
      all.merge!(attributes) if self.respond_to?(:attributes)
      all
    else
      {}
  end
  list.with_indifferent_access
end
properties=(property_hsh) click to toggle source

Public: Sets a bunch of properties that are marked as accessible.

property_hsh - The Hash containing the property name and the property

value.

Examples

class Car
  include Seatbelt::Ghost

  interface :instance do 
    define_property :wheels
    define_property :color

    property_accessible :color
  end
end

car = Car.new
car.properties = { :color => "yellow", :wheels => 3 }

car.color   # => 'yellow' because it's accessible
car.wheels  # => nil because it's not accessible
# File lib/seatbelt/core/property.rb, line 150
def properties=(property_hsh)
  property_hsh.each do |property_key, property_value|
    if self.class.accessible_properties.include?(property_key) || \
      self.class.accessible_properties.map(&:to_s).include?(property_key)
      self.send("#{property_key}=", property_value)
    end
  end
end