Class: HTMLSchema::Object

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Object.



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

def initialize(name, options = {}, &block)
  @_name      = name
  @attributes = {}
  @types      = {}
  @parent     = options[:parent]
  @as         = options[:as]
  @as         ||= @parent ? @parent.as : name
  @classes    = Array(as).map(&:to_s)
  
  # inherit parent attributes
  @parent.attributes.each do |key, attribute|
    self.attribute key, attribute.value
  end if @parent
  
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#_nameObject

Returns the value of attribute _name.



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

def _name
  @_name
end

#asObject

Returns the value of attribute as.



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

def as
  @as
end

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#classesObject

Returns the value of attribute classes.



4
5
6
# File 'lib/html-schema/object.rb', line 4

def classes
  @classes
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#typesObject

Returns the value of attribute types.



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

def types
  @types
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/html-schema/object.rb', line 35

def [](key)
  attributes[key]
end

#attribute(name, options = {}) ⇒ Object



23
24
25
# File 'lib/html-schema/object.rb', line 23

def attribute(name, options = {})
  create_attribute name, options
end

#inspectObject



31
32
33
# File 'lib/html-schema/object.rb', line 31

def inspect
  "#<#{self.class.name} name: #{_name.inspect}, attributes: #{attributes.inspect} parent: #{(parent.present? ? parent._name : nil).inspect}, types: #{types.keys.inspect}>"
end

#to_objectObject



39
40
41
42
43
# File 'lib/html-schema/object.rb', line 39

def to_object
  result = to_hash.merge(:attributes => attributes.keys.inject({}) { |hash, key| hash[key] = attributes[key].to_object ; hash })
  result[:parent] = parent._name if parent
  result
end

#type(name, options = {}, &block) ⇒ Object



27
28
29
# File 'lib/html-schema/object.rb', line 27

def type(name, options = {}, &block)
  create_type name, options, &block
end