Class: Representable::Definition

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

Constructor Details

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

Returns a new instance of Definition.



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

def initialize(sym, options={})
  @name     = sym.to_s
  @options  = options
end

Instance Attribute Details

#nameObject (readonly) Also known as: getter

Returns the value of attribute name.



4
5
6
# File 'lib/representable/definition.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/representable/definition.rb', line 4

def options
  @options
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  options[:collection]
end

#attributeObject



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

def attribute
  options[:attribute]
end

#bindingObject



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

def binding
  options[:binding]
end

#cloneObject



12
13
14
# File 'lib/representable/definition.rb', line 12

def clone
  self.class.new(name, options.clone) # DISCUSS: make generic Definition.cloned_attribute that passes list to constructor.
end

#contentObject



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

def content
  options[:content]
end

#create_binding(*args) ⇒ Object



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

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

#defaultObject



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

def default
  options[:default]
end

#default_for(value) ⇒ Object



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

def default_for(value)
  return default if skipable_nil_value?(value)
  value
end

#deserialize_classObject



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

def deserialize_class
  options[:class]
end

#fromObject



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

def from
  # TODO: deprecate :from.
  (options[:from] || options[:as] || name).to_s
end

#has_default?Boolean

Returns:

  • (Boolean)


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

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

#hash?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/representable/definition.rb', line 28

def hash?
  options[:hash]
end

#representer_moduleObject



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

def representer_module
  options[:extend] or options[:decorator]
end

#setterObject



16
17
18
# File 'lib/representable/definition.rb', line 16

def setter
  :"#{name}="
end

#skipable_nil_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def skipable_nil_value?(value)
  value.nil? and not options[:render_nil]
end

#sync?Boolean

Returns:

  • (Boolean)


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

def sync?
  options[:parse_strategy] == :sync
end

#typed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/representable/definition.rb', line 20

def typed?
  deserialize_class.is_a?(Class) or representer_module or options[:instance]  # also true if only :extend is set, for people who want solely rendering.
end