Class: GEXF::Attribute
- Inherits:
-
Object
- Object
- GEXF::Attribute
- 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
-
#attr_class ⇒ Object
readonly
Returns the value of attribute attr_class.
-
#default ⇒ Object
Returns the value of attribute default.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#coherce(value) ⇒ Object
Note: is this a violation of the “Tell don’t ask principle”?.
-
#initialize(id, title, opts = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #is_valid?(value) ⇒ Boolean
- #to_hash ⇒ Object
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] = opts[:options] id = id.to_s = if type == LIST_STRING && .respond_to?(:split) .split('|').uniq else Array().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_class ⇒ Object (readonly)
Returns the value of attribute attr_class.
19 20 21 |
# File 'lib/gexf/attribute.rb', line 19 def attr_class @attr_class end |
#default ⇒ Object
Returns the value of attribute default.
19 20 21 |
# File 'lib/gexf/attribute.rb', line 19 def default @default end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
19 20 21 |
# File 'lib/gexf/attribute.rb', line 19 def id @id end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
19 20 21 |
# File 'lib/gexf/attribute.rb', line 19 def mode @mode end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/gexf/attribute.rb', line 19 def end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
19 20 21 |
# File 'lib/gexf/attribute.rb', line 19 def title @title end |
#type ⇒ Object (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
77 78 79 80 81 82 83 84 |
# File 'lib/gexf/attribute.rb', line 77 def is_valid?(value) if .empty? true else value = value.first if value.respond_to?(:first) .map(&:to_s).include?(value.to_s) end end |
#to_hash ⇒ Object
86 87 88 89 90 91 |
# File 'lib/gexf/attribute.rb', line 86 def to_hash optional = {} optional[:options] = .join('|') if && .any? {:id => id, :title => title, :type => type}.merge(optional) end |