Class: Dupe::Model::Schema::AttributeTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/attribute_template.rb

Overview

This class represents an attribute template. An attribute template consists of an attribute name (a symbol), a potential default value (nil if not specified), and a potential transformer proc.

Defined Under Namespace

Classes: NilValue

Instance Method Summary collapse

Instance Method Details

#generate(value = NilValue) ⇒ Object

Suppose we have the following attribute template:

console> a = Dupe::Model::Schema::AttributeTemplate.new(:title)

If we call generate with no parameter, we’ll get back the following:

console> a.generate
  ===> :title, nil

If we call generate with a parameter, we’ll get back the following:

console> a.generate 'my value'
  ===> :title, 'my value'

If we setup an attribute template with a transformer:

console> a = 
          Dupe::Model::Schema::AttributeTemplate.new(
            :title, 
            :default => 'default value', 
            :transformer => proc {|dont_care| 'test'}
          )

Then we’ll get back the following when we call generate:

console> a.generate
  ===> :title, 'default value'

console> a.generate 'my value'
  ===> :title, 'test'


42
43
44
45
46
47
48
49
50
# File 'lib/superdupe/attribute_template.rb', line 42

def generate(value=NilValue)
  if value == NilValue
    v = @default.respond_to?(:call) ? @default.call : (@default.dup rescue @default)
  else
    v = (@transformer ? @transformer.call(value) : value)
  end
  
  return @name, v
end