Class: Representable::Definition

Inherits:
Hash
  • Object
show all
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

Methods included from Hash

#from_hash, included, #to_hash

Constructor Details

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

Returns a new instance of Definition.



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

def initialize(sym, options={})
  super()
  options = options.clone

  handle_deprecations!(options)

  @name   = sym.to_s
  # defaults:
  options[:as] ||= @name

  setup!(options)
end

Instance Attribute Details

#nameObject (readonly) Also known as: getter

Returns the value of attribute name.



7
8
9
# File 'lib/representable/definition.rb', line 7

def name
  @name
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  self[:collection]
end

#create_binding(*args) ⇒ Object



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

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

#default_for(value) ⇒ Object



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

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

#has_default?Boolean

Returns:

  • (Boolean)


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

def has_default?
  has_key?(:default)
end

#hash?Boolean

Returns:

  • (Boolean)


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

def hash?
  self[:hash]
end

#merge!(options) ⇒ Object

TODO: test merge!.



24
25
26
27
# File 'lib/representable/definition.rb', line 24

def merge!(options)
  setup!(options)
  self
end

#optionsObject

TODO: remove in 2.0.



31
32
33
34
# File 'lib/representable/definition.rb', line 31

def options # TODO: remove in 2.0.
  warn "Representable::Definition#option is deprecated, use #[] directly."
  self
end

#representable?Boolean

Returns:

  • (Boolean)


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

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

#representer_moduleObject



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

def representer_module
  self[:extend]
end

#setterObject



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

def setter
  :"#{name}="
end

#skipable_empty_value?(value) ⇒ Boolean Also known as: skipable_nil_value?

Returns:

  • (Boolean)


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

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)


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

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