Class: Representable::Definition

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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
24
# File 'lib/representable/definition.rb', line 12

def initialize(sym, options={}, &block)
  @options = {}
  # @options = Inheritable::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

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  self[:collection]
end

#cloneObject



39
40
41
# File 'lib/representable/definition.rb', line 39

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

#create_binding(*args) ⇒ Object



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

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

#default_for(value) ⇒ Object



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

def default_for(value)
  return self[:default] if skipable_empty_value?(value)
  value
end

#has_default?Boolean

Returns:

  • (Boolean)


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

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

#hash?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/representable/definition.rb', line 60

def hash?
  self[:hash]
end

#merge!(options, &block) ⇒ Object



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

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)


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

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

#representer_moduleObject



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

def representer_module
  @options[:extend]
end

#setterObject



43
44
45
# File 'lib/representable/definition.rb', line 43

def setter
  :"#{name}="
end

#skipable_empty_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def skipable_empty_value?(value)
  return true if array? and self[:render_empty] == false and value and value.size == 0  # TODO: change in 2.0, don't render emtpy.
  value.nil? and not self[:render_nil]
end

#typed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/representable/definition.rb', line 47

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