Class: Representable::Definition

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

Overview

:nodoc:

Direct Known Subclasses

XML::Definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Definition.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/representable/definition.rb', line 6

def initialize(sym, options={})
  @name         = sym.to_s
  @array        = options[:collection]
  @from         = (options[:from] || name).to_s
  @sought_type  = options[:as] || :text
  
  # FIXME: move me to xml.
  if options[:from].to_s =~ /^@/
    @sought_type = :attr
    options[:from].sub!('@', '')
  end
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#nameObject (readonly) Also known as: getter

Returns the value of attribute name.



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

def name
  @name
end

#sought_typeObject (readonly)

Returns the value of attribute sought_type.



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

def sought_type
  @sought_type
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



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

def wrapper
  @wrapper
end

Instance Method Details

#apply(value) ⇒ Object

Applies the block to value which might also be a collection.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/representable/definition.rb', line 49

def apply(value)
  return value unless value # DISCUSS: is that ok here?
  
  if array?
    value = value.collect do |item|
      yield item
    end
  else
    value = yield value
  end
  
  value
end

#array?Boolean

Returns:

  • (Boolean)


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

def array?
  @array
end

#cdata?Boolean

FIXME: move to XML!

Returns:

  • (Boolean)


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

def cdata?  # FIXME: move to XML!
  @cdata
end

#content?Boolean

Returns:

  • (Boolean)


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

def content?
  @name == '.'
end

#instance_variable_nameObject



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

def instance_variable_name
  :"@#{name}"
end

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  @name == '*'
end

#setterObject



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

def setter
  :"#{name}="
end

#typed?Boolean

Returns:

  • (Boolean)


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

def typed?
  sought_type.is_a?(Class)
end