Module: Shape::Base::ClassMethods

Included in:
Shape::Base, PropertyShaper, ViewDecorator
Defined in:
lib/shape/base.rb

Instance Method Summary collapse

Instance Method Details

#_properties_fromObject



94
95
96
# File 'lib/shape/base.rb', line 94

def _properties_from
  @properties_from ||= []
end

#associationsObject



86
87
88
# File 'lib/shape/base.rb', line 86

def associations
  @associations ||= {}
end

#delegate(*methods, options = {}) ⇒ void

This method returns an undefined value.

Overrides Module.delegate to make ‘:_source` the default delegation target.



103
104
105
106
# File 'lib/shape/base.rb', line 103

def delegate(*methods)
  options = methods.extract_options!
  super *methods, options.reverse_merge(to: :_source)
end

#propertiesObject



90
91
92
# File 'lib/shape/base.rb', line 90

def properties
  @properties ||= {}
end

#properties_from(name, options = {}) ⇒ Object



81
82
83
84
# File 'lib/shape/base.rb', line 81

def properties_from(name, options={})
  except = Array(options[:except])
  _properties_from << [name, except]
end

#property(property_name, options = {}, &block) ⇒ Object Also known as: association

Expose a property as “…” To expose a property using a different attribute on the resource:

property :display, from: :display_name

To expose a property with inline definition:

property :display do
  from do
    #{last_name}, #{first_name}
  end
end

To expose a decorated collection:

property :practices, with: PracticeDecorator

To expose a decorated collection with view context

property :practices, with: PracticeDecorator, context: {view: :summary}


74
75
76
77
78
# File 'lib/shape/base.rb', line 74

def property(property_name, options={}, &block)
  properties[property_name] = Shape::PropertyShaper.new(
    shaper_context, property_name, options, &block
  )
end

#shape(source, options = {}) ⇒ Object



44
45
46
# File 'lib/shape/base.rb', line 44

def shape(source, options={})
  self.new(source, options)
end

#shape_collection(collection, options = {}) ⇒ Object



108
109
110
111
112
# File 'lib/shape/base.rb', line 108

def shape_collection(collection, options = {})
  Array(collection).map do |item|
    self.shape(item, options.clone)
  end
end

#shaper_contextObject



48
49
50
# File 'lib/shape/base.rb', line 48

def shaper_context
  @shaper_context || self
end