Class: HTMLSchema::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/html-schema/attribute.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#_name, #as, #attributes, #classes, #parent, #source, #types

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#[], #attribute

Constructor Details

#initialize(name, options = {}, &block) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/html-schema/attribute.rb', line 5

def initialize(name, options = {}, &block)
  @_name      = name
  @value      = options
  @parent     = options[:parent]
  @as         = options[:as] || name
  @classes    = Array(as).map(&:to_s)
  @required   = options[:required] == true
  @type       = options[:type] || :string
  
  @options    = options.except(:as, :parent, :required, :type)
  @options.each do |key, value|
    @options[key] = value.to_s
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/html-schema/attribute.rb', line 3

def options
  @options
end

#requiredObject

Returns the value of attribute required.



3
4
5
# File 'lib/html-schema/attribute.rb', line 3

def required
  @required
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/html-schema/attribute.rb', line 3

def type
  @type
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/html-schema/attribute.rb', line 3

def value
  @value
end

Class Method Details

.attribute?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/html-schema/attribute.rb', line 47

def attribute?
  true
end

Instance Method Details

#format(value) ⇒ Object

ISO 8601 date format



25
26
27
28
29
30
31
32
# File 'lib/html-schema/attribute.rb', line 25

def format(value)
  case type
  when :date
    ::Date.parse(value)
  else
    value.to_s
  end
end

#inspectObject



20
21
22
# File 'lib/html-schema/attribute.rb', line 20

def inspect
  "#<#{self.class.name} name: #{_name.inspect}, value: #{value.inspect}>"
end

#to_hashObject



38
39
40
# File 'lib/html-schema/attribute.rb', line 38

def to_hash
  {}
end

#to_objectObject



42
43
44
# File 'lib/html-schema/attribute.rb', line 42

def to_object
  to_hash.merge(:type => type)
end