Class: Microformats2::Format
- Inherits:
-
Object
- Object
- Microformats2::Format
- Defined in:
- lib/microformats2/format.rb
Constant Summary collapse
- CLASS_REG_EXP =
/^(h-)/
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
Instance Method Summary collapse
- #add_property(property_class, value) ⇒ Object
- #children ⇒ Object
- #format_types ⇒ Object
-
#initialize(element, base) ⇒ Format
constructor
A new instance of Format.
- #parse ⇒ Object
- #parse_child_formats ⇒ Object
- #parse_implied_properties ⇒ Object
- #parse_properties ⇒ Object
- #properties ⇒ Object
- #property_present?(property) ⇒ Boolean
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(element, base) ⇒ Format
Returns a new instance of Format.
7 8 9 10 11 12 13 14 |
# File 'lib/microformats2/format.rb', line 7 def initialize(element, base) @element = element @base = base @method_name = to_method_name(type.first) @property_names = [] @child_formats = [] @child_formats_parsed = false end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
5 6 7 |
# File 'lib/microformats2/format.rb', line 5 def method_name @method_name end |
Instance Method Details
#add_property(property_class, value) ⇒ Object
57 58 59 60 |
# File 'lib/microformats2/format.rb', line 57 def add_property(property_class, value) property = Property.new(nil, property_class, value, @base) assign_property(property) end |
#children ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/microformats2/format.rb', line 22 def children unless @child_formats_parsed parse_child_formats @child_formats_parsed = true end @child_formats end |
#format_types ⇒ Object
30 31 32 33 |
# File 'lib/microformats2/format.rb', line 30 def format_types warn "[DEPRECATION] `format_types` is deprecated and will be removed in the next release. Please use `type` instead." type end |
#parse ⇒ Object
16 17 18 19 20 |
# File 'lib/microformats2/format.rb', line 16 def parse type properties self end |
#parse_child_formats ⇒ Object
51 52 53 54 55 |
# File 'lib/microformats2/format.rb', line 51 def parse_child_formats FormatParser.parse(@element.children, @base, true).each do |child_format| @child_formats.push(child_format) end end |
#parse_implied_properties ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/microformats2/format.rb', line 62 def parse_implied_properties ip = [] ip << ImpliedProperty::Name.new(@element, @base).parse unless property_present?(:name) ip << ImpliedProperty::Url.new(@element, @base).parse unless property_present?(:url) ip << ImpliedProperty::Photo.new(@element, @base).parse unless property_present?(:photo) ip.compact.each do |property| save_property_name(property.method_name) define_method(property.method_name) set_value(property.method_name, property) end end |
#parse_properties ⇒ Object
45 46 47 48 49 |
# File 'lib/microformats2/format.rb', line 45 def parse_properties PropertyParser.parse(@element.children, @base).each do |property| assign_property(property) end end |
#properties ⇒ Object
41 42 43 |
# File 'lib/microformats2/format.rb', line 41 def properties @properties ||= parse_properties.concat parse_implied_properties end |
#property_present?(property) ⇒ Boolean
74 75 76 |
# File 'lib/microformats2/format.rb', line 74 def property_present?(property) !! respond_to?(property) && send(property) end |
#to_hash ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/microformats2/format.rb', line 78 def to_hash hash = { type: type, properties: {} } @property_names.each do |method_name| hash[:properties][method_name.to_sym] = send(method_name, :all).map(&:to_hash) end unless children.empty? hash[:children] = [] children.each do |child_format| hash[:children].push(child_format.to_hash) end end hash end |
#to_json ⇒ Object
92 93 94 |
# File 'lib/microformats2/format.rb', line 92 def to_json to_hash.to_json end |
#type ⇒ Object
35 36 37 38 39 |
# File 'lib/microformats2/format.rb', line 35 def type @type ||= @element.attribute("class").to_s.split.select do |html_class| html_class =~ Format::CLASS_REG_EXP end end |