Module: Seatbelt::Property::InstanceMethods

Defined in:
lib/seatbelt/core/property.rb

Instance Method Summary collapse

Instance Method Details

#properties(role = :accessibles) ⇒ Object

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Object

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


150
151
152
153
154
155
156
157
# 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