Class: Representable::Definition

Inherits:
Object
  • Object
show all
Includes:
Cloneable
Defined in:
lib/representable/definition.rb

Overview

Created at class compile time. Keeps configuration options for one property.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, options = {}, &block) ⇒ Definition

Returns a new instance of Definition.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/representable/definition.rb', line 12

def initialize(sym, options={}, &block)
  @options = Cloneable::Hash.new # allows deep cloning. we then had to set Pipeline cloneable.
  @name    = sym.to_s
  options  = options.clone

  # defaults:
  options[:parse_filter]  = Pipeline[*options[:parse_filter]]
  options[:render_filter] = Pipeline[*options[:render_filter]]
  options[:as]          ||= @name

  setup!(options, &block)
end

Instance Attribute Details

#nameObject (readonly) Also known as: getter

Returns the value of attribute name.



9
10
11
# File 'lib/representable/definition.rb', line 9

def name
  @name
end

Instance Method Details

#[](name) ⇒ Object



41
42
43
# File 'lib/representable/definition.rb', line 41

def [](name)
  @runtime_options[name]
end

#array?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/representable/definition.rb', line 62

def array?
  self[:collection]
end

#cloneObject



45
46
47
# File 'lib/representable/definition.rb', line 45

def clone
  self.class.new(name, @options.clone)
end

#create_binding(*args) ⇒ Object



78
79
80
# File 'lib/representable/definition.rb', line 78

def create_binding(*args)
  self[:binding].call(self, *args)
end

#delete!(name) ⇒ Object



35
36
37
38
39
# File 'lib/representable/definition.rb', line 35

def delete!(name)
  @runtime_options.delete(name)
  @options.delete(name)
  self
end

#has_default?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/representable/definition.rb', line 70

def has_default?
  @options.has_key?(:default)
end

#hash?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/representable/definition.rb', line 66

def hash?
  self[:hash]
end

#inspectObject



82
83
84
85
# File 'lib/representable/definition.rb', line 82

def inspect
  state = (instance_variables-[:@runtime_options, :@name]).collect { |ivar| "#{ivar}=#{instance_variable_get(ivar)}" }
  "#<Representable::Definition ==>#{name} #{state.join(" ")}>"
end

#merge!(options, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/representable/definition.rb', line 25

def merge!(options, &block)
  options = options.clone

  options[:parse_filter]  = @options[:parse_filter].push(*options[:parse_filter])
  options[:render_filter] = @options[:render_filter].push(*options[:render_filter])

  setup!(options, &block) # FIXME: this doesn't yield :as etc.
  self
end

#representable?Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/representable/definition.rb', line 57

def representable?
  return if self[:representable] == false
  self[:representable] or typed?
end

#representer_moduleObject



74
75
76
# File 'lib/representable/definition.rb', line 74

def representer_module
  @options[:extend]
end

#setterObject



49
50
51
# File 'lib/representable/definition.rb', line 49

def setter
  :"#{name}="
end

#typed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/representable/definition.rb', line 53

def typed?
  self[:class] or self[:extend] or self[:instance]
end