Class: Kartograph::Sculptor

Inherits:
Object
  • Object
show all
Defined in:
lib/kartograph/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/kartograph/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/kartograph/sculptor.rb', line 3

def map
  @map
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Instance Method Details

#propertiesObject



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

def properties
  map.properties
end

#sculpt(scope = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kartograph/sculptor.rb', line 27

def sculpt(scope = nil)
  return nil if @object.nil?

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

  scoped_properties.each_with_object(sculpted_object) do |property, mutable|
    setter_method = "#{property.name}="
    value = property.value_from(object, scope)
    mutable.send(setter_method, value)
  end
end

#sculpted_objectObject



14
15
16
17
# File 'lib/kartograph/sculptor.rb', line 14

def sculpted_object
  # Fallback initialization of the object we're extracting into
  @sculpted_object ||= map.mapping.new
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)


21
22
23
24
25
# File 'lib/kartograph/sculptor.rb', line 21

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

  @sculpted_object = object
end