Class: Microformat::Attribute
- Inherits:
-
Object
- Object
- Microformat::Attribute
- Defined in:
- lib/microformat/attribute.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #attribute(name, options = {}, &block) ⇒ Object
- #cast(value, cast_to = nil) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #read_from(document) ⇒ Object
- #read_value_from(document) ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ Attribute
Returns a new instance of Attribute.
7 8 9 10 11 12 13 14 15 |
# File 'lib/microformat/attribute.rb', line 7 def initialize(name, = {}, &block) @name = name = @attributes ||= {} if [:format] @attributes.merge!([:format].attribute_definition.attributes) end block.yield(self) if block_given? end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/microformat/attribute.rb', line 5 def attributes @attributes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/microformat/attribute.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/microformat/attribute.rb', line 5 def end |
Instance Method Details
#attribute(name, options = {}, &block) ⇒ Object
17 18 19 20 21 |
# File 'lib/microformat/attribute.rb', line 17 def attribute(name, = {}, &block) Attribute.new(name, , &block).tap do |attribute| @attributes.merge!(name.to_sym => attribute) end end |
#cast(value, cast_to = nil) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/microformat/attribute.rb', line 41 def cast(value, cast_to = nil) value && case cast_to when :integer then value.to_s.strip.to_i when :decimal then value.to_s.strip.to_f when :string then value.to_s.strip else value end end |
#read_from(document) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/microformat/attribute.rb', line 23 def read_from(document) selector = [:selector] || ".#{@name}" sub_document = document.css(selector).first value = read_value_from(sub_document) AttributeMap.new(self, sub_document, { value: value }) end |
#read_value_from(document) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/microformat/attribute.rb', line 30 def read_value_from(document) Array([:attribute] || "text").each do |attr| if attr == "text" return cast((document && document.text) || [:default], [:cast]) elsif document[attr] return cast(document && document[attr], [:cast]) end end return [:default] end |