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
11
12
# File 'lib/representable/definition.rb', line 7

def initialize(sym, options={})
  @name     = sym.to_s
  @options  = options
  
  options[:default] ||= [] if array?  # FIXME: move to CollectionBinding!
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)


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

def array?
  options[:collection]
end

#attributeObject



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

def attribute
  options[:attribute]
end

#cloneObject



14
15
16
# File 'lib/representable/definition.rb', line 14

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

#defaultObject



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

def default
  options[:default]
end

#default_for(value) ⇒ Object



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

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

#fromObject



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

def from
  (options[:from] || name).to_s
end

#has_default?Boolean

Returns:

  • (Boolean)


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

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

#hash?Boolean

Returns:

  • (Boolean)


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

def hash?
  options[:hash]
end

#representer_moduleObject



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

def representer_module
  options[:extend]
end

#setterObject



18
19
20
# File 'lib/representable/definition.rb', line 18

def setter
  :"#{name}="
end

#skipable_nil_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#sought_typeObject



34
35
36
# File 'lib/representable/definition.rb', line 34

def sought_type
  options[:class]
end

#typed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/representable/definition.rb', line 22

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