Class: Cartograph::Sculptor

Inherits:
Object
  • Object
show all
Defined in:
lib/cartograph/sculptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, map) ⇒ Sculptor

Returns a new instance of Sculptor.



5
6
7
8
# File 'lib/cartograph/sculptor.rb', line 5

def initialize(object, map)
  @object = object
  @map = map
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



3
4
5
# File 'lib/cartograph/sculptor.rb', line 3

def map
  @map
end

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/cartograph/sculptor.rb', line 3

def object
  @object
end

Instance Method Details

#propertiesObject



10
11
12
# File 'lib/cartograph/sculptor.rb', line 10

def properties
  map.properties
end

#sculpt(scope = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cartograph/sculptor.rb', line 22

def sculpt(scope = nil)
  return unless @object

  scoped_properties = scope ? properties.filter_by_scope(scope) : properties

  attributes = scoped_properties.each_with_object({}) do |property, h|
    h[property.name] = property.value_from(object, scope)
  end

  return map.mapping.new(attributes) unless @sculpted_object

  attributes.each do |name, val|
    @sculpted_object[name] = val
  end

  @sculpted_object
end

#sculpted_object=(object) ⇒ Object

Set this to pass in an object to extract into. Must

Parameters:

  • object

    must be of the same class as the map#mapping

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/cartograph/sculptor.rb', line 16

def sculpted_object=(object)
  raise ArgumentError unless object.is_a?(map.mapping)

  @sculpted_object = object
end