Class: GEXF::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/gexf/attribute.rb

Defined Under Namespace

Modules: Assignable, Definable

Constant Summary collapse

BOOLEAN =
:boolean
STRING =
:string
INTEGER =
:integer
FLOAT =
:float
ANY_URI =
:anyURI
LIST_STRING =
:liststring
TYPES =
[ BOOLEAN, STRING, INTEGER, FLOAT, ANY_URI, LIST_STRING]
DYNAMIC =
:dynamic
STATIC =
:static
MODES =
[DYNAMIC, STATIC]
NODE =
:node
EDGE =
:edge
CLASSES =
[NODE, EDGE]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, title, opts = {}) ⇒ Attribute

Returns a new instance of Attribute.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gexf/attribute.rb', line 21

def initialize(id, title, opts={})

  attr_class = opts[:class] || NODE
  mode       = opts[:mode]  || STATIC
  type       = opts[:type]  || STRING
  default    = opts[:default]
  options    = opts[:options]
  id         = id.to_s

  @options   = if type == LIST_STRING && options.respond_to?(:split)
                 options.split('|').uniq
               else
                 Array(options).uniq
               end


  raise ArgumentError.new "Invalid or missing type: #{type}" if !TYPES.include?(type)
  raise ArgumentError.new "invalid or missing class: #{attr_class}" if !CLASSES.include?(attr_class)
  raise ArgumentError.new "Invalid mode: #{mode}" if !MODES.include?(mode)

  @attr_class = attr_class
  @type       = type
  @id         = id
  @type       = type
  @mode       = mode
  @title      = title.to_s
  self.default=default
end

Instance Attribute Details

#attr_classObject (readonly)

Returns the value of attribute attr_class.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def attr_class
  @attr_class
end

#defaultObject

Returns the value of attribute default.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def default
  @default
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def id
  @id
end

#modeObject (readonly)

Returns the value of attribute mode.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def mode
  @mode
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/gexf/attribute.rb', line 19

def type
  @type
end

Instance Method Details

#coherce(value) ⇒ Object

Note: is this a violation of the “Tell don’t ask principle”?



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gexf/attribute.rb', line 56

def coherce(value)
  case @type
  when BOOLEAN
    case value
    when *['1', 'true', 1, true]
      true
    when *['0', 'false', 0, false]
      false
    end
  when STRING, ANY_URI
    value.to_s
  when FLOAT
    value.to_f if value.respond_to?(:to_f)
  when INTEGER
    value.to_i if value.respond_to?(:to_i)

  when LIST_STRING
    Array(value).flatten.map(&:to_s).uniq
  end
end

#is_valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/gexf/attribute.rb', line 77

def is_valid?(value)
  if @options.empty?
    true
  else
    value = value.first if value.respond_to?(:first)
    @options.map(&:to_s).include?(value.to_s)
  end
end

#to_hashObject



86
87
88
89
90
91
# File 'lib/gexf/attribute.rb', line 86

def to_hash
  optional = {}
  optional[:options] = options.join('|') if options && options.any?

  {:id => id, :title => title, :type => type}.merge(optional)
end