Class: Shape::PropertyShaper

Inherits:
Object
  • Object
show all
Includes:
Base::ClassMethods
Defined in:
lib/shape/property_shaper.rb

Overview

Property Shaper

Keeps track of property info and context when shaping views.

We’ll use the PropertyShaper objects later to recursively build the data.

Allows anything inside a property block to call methods in the context of the Shape dsl allowing for nested properties.

Example:

property :address do
  property :street_address do
    property :addr_line1
    property :addr_line2
  end
  property :city
  # ...
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassMethods

#_properties_from, #associations, #delegate, #properties, #properties_from, #property, #shape, #shape_collection

Constructor Details

#initialize(shaper_context, name, options = {}, &block) ⇒ PropertyShaper

Returns a new instance of PropertyShaper.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shape/property_shaper.rb', line 30

def initialize(shaper_context, name, options={}, &block)
  self.shaper_context = shaper_context
  self.name = name
  self.options = options

  if block
    instance_eval(&block)
  else
    from = options[:from] || name
    define_accessor(name, from)
    delegate_property(from)
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/shape/property_shaper.rb', line 26

def name
  @name
end

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/shape/property_shaper.rb', line 28

def options
  @options
end

#shaper_contextObject

Returns the value of attribute shaper_context.



27
28
29
# File 'lib/shape/property_shaper.rb', line 27

def shaper_context
  @shaper_context
end

Instance Method Details

#from(&block) ⇒ Object



44
45
46
47
48
# File 'lib/shape/property_shaper.rb', line 44

def from(&block)
  unless shaper_context.method_defined?(name.to_sym)
    shaper_context.send(:define_method, name, &block)
  end
end

#with(&block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/shape/property_shaper.rb', line 50

def with(&block)
  options[:with] = Class.new do
    include Shape
    instance_eval(&block)
  end
  define_accessor(name, options[:from] || name)
end