Class: Prism::POSH

Inherits:
Object
  • Object
show all
Extended by:
Prism
Defined in:
lib/prism/posh.rb,
lib/prism/posh/base.rb,
lib/prism/posh/anchor.rb,
lib/prism/posh/definition_list.rb

Defined Under Namespace

Classes: Anchor, Base, DefinitionList

Constant Summary

Constants included from Prism

PRODID, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Prism

extract, extract_from, find, find_in, found_in?, get, get_document, get_url, map, normalize, parse, parse_first, search, search_for, valid?, validate

Constructor Details

#initialize(node, parent = nil) ⇒ POSH

Returns a new instance of POSH


160
161
162
163
164
165
166
# File 'lib/prism/posh.rb', line 160

def initialize(node, parent=nil)
  raise 'Uh OH' unless self.class.valid?(node)
  @node = node
  @source = node.document.url if node.document.url
  @parent = parent if parent
  @first_node = self.class.remove_nested(node.dup)
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node


158
159
160
# File 'lib/prism/posh.rb', line 158

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent


158
159
160
# File 'lib/prism/posh.rb', line 158

def parent
  @parent
end

#sourceObject (readonly)

Returns the value of attribute source


158
159
160
# File 'lib/prism/posh.rb', line 158

def source
  @source
end

Class Method Details

.[](key) ⇒ Object


78
79
80
# File 'lib/prism/posh.rb', line 78

def self.[](key)
  properties[key]
end

.add_property(property_name, &block) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prism/posh.rb', line 61

def self.add_property(property_name, &block)
  if property_name.respond_to?(:property_of?)
    property = property_name
  else
    property = Class.new(Prism::POSH::Base)
    property.name property_name
    property.parent self
    property.instance_eval(&block) if block_given?
  end
  properties[property.name] = property
  property
end

.extract(pattern = nil, &block) ⇒ Object

Instead of getting the contents of a node, this creates a POSH format from the node


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/prism/posh.rb', line 30

def self.extract(pattern = nil, &block)
  if !@extract
    if pattern
      @extract = Prism.map(pattern).extract
    elsif block_given?
      @extract = block
    elsif properties.empty?
      @extract = Prism::Pattern::ValueClass.extract
    end
  end
  @extract || lambda{|node| self.new(node) } 
end

.get_properties(node, props = properties) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/prism/posh.rb', line 140

def self.get_properties(node, props=properties)
  hash ||= {}
  props.each_pair do |key, property|
    hash[key] = if one && one.include?(property)
      property.parse_first(node)
    else
      property.parse(node)
    end
  end
  hash.reject do |key,value| 
    value.respond_to?(:empty?) ? value.empty? : value.nil?
  end
end

.has_many(*property_names, &block) ⇒ Object


96
97
98
99
100
101
102
103
104
# File 'lib/prism/posh.rb', line 96

def self.has_many(*property_names, &block)
  property_names.collect do |name|
    property = has_many!(name, &block)
    define_method property.name do 
      self[property.name]
    end
    property
  end
end

.has_many!(property_name, &block) ⇒ Object


113
114
115
116
117
118
# File 'lib/prism/posh.rb', line 113

def self.has_many!(property_name, &block)
  @has_many ||= []
  property = add_property(property_name, &block)
  @has_many << property
  property
end

.has_one(*property_names, &block) ⇒ Object


86
87
88
89
90
91
92
93
94
# File 'lib/prism/posh.rb', line 86

def self.has_one(*property_names, &block)
  property_names.collect do |name|
    property = has_one!(name, &block)
    define_method property.name do 
      self[property.name]
    end
    property
  end
end

.has_one!(property_name, &block) ⇒ Object


106
107
108
109
110
111
# File 'lib/prism/posh.rb', line 106

def self.has_one!(property_name, &block)
  @has_one ||= []
  property = add_property(property_name, &block)
  @has_one << property
  property
end

.inherited(subclass) ⇒ Object


20
21
22
23
24
25
26
# File 'lib/prism/posh.rb', line 20

def self.inherited(subclass)
  inheritable = [ :@properties, :@has_one, :@has_many, :@search, 
                  :@extract, :@validate, :@requirements ]
  inheritable.each do |var|
    subclass.instance_variable_set(var, instance_variable_get(var).dup) if instance_variable_get(var)
  end
end

.manyObject


124
125
126
# File 'lib/prism/posh.rb', line 124

def self.many
  @has_many
end

.name(root = nil) ⇒ Object


5
6
7
8
# File 'lib/prism/posh.rb', line 5

def self.name(root=nil)
  @name = Prism.normalize(root) if root
  @name
end

.oneObject


120
121
122
# File 'lib/prism/posh.rb', line 120

def self.one
  @has_one
end

.parent(owner = nil) ⇒ Object


15
16
17
18
# File 'lib/prism/posh.rb', line 15

def self.parent(owner=nil)
  @parent = owner.respond_to?(:extract) ? owner : Prism.map(owner) if owner
  @parent
end

.propertiesObject


74
75
76
# File 'lib/prism/posh.rb', line 74

def self.properties
  @properties ||= {}
end

.property_of?(format) ⇒ Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/prism/posh.rb', line 82

def self.property_of?(format)
  Prism.map(format) == parent
end

.remove_nested(node) ⇒ Object

Remove a format from a node if it is nested.


133
134
135
136
137
138
# File 'lib/prism/posh.rb', line 133

def self.remove_nested(node)
  if (find_in(node) != node)
    find_in(node).unlink if found_in?(node)
  end
  node
end

.required!Object


154
155
156
# File 'lib/prism/posh.rb', line 154

def self.required!
  parent.requirements << self if parent
end

.requirementsObject


128
129
130
# File 'lib/prism/posh.rb', line 128

def self.requirements
  @requirements ||= []
end

.search(&block) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prism/posh.rb', line 43

def self.search(&block)
  @search = block if block_given?
  @search || lambda do |node|
    if selector
      node.css(selector)
    elsif name
      node.css(".#{name}".gsub('_','-'))
    else
      node
    end
  end
end

.selector(css = nil) ⇒ Object


10
11
12
13
# File 'lib/prism/posh.rb', line 10

def self.selector(css=nil)
  @selector = css if css
  @selector
end

.validate(&block) ⇒ Object


56
57
58
59
# File 'lib/prism/posh.rb', line 56

def self.validate(&block)
  @validate = block if block_given?
  @validate || lambda {|node| selector ? node.matches?(selector) : found_in?(node.parent) }
end

Instance Method Details

#[](property_key) ⇒ Object


168
169
170
171
# File 'lib/prism/posh.rb', line 168

def [](property_key)
  @get_properties ||= self.class.get_properties(@first_node, properties)
  @get_properties[property_key]
end

#each_pair(&block) ⇒ Object


219
220
221
# File 'lib/prism/posh.rb', line 219

def each_pair(&block)
  to_h.each_pair(&block)
end

#empty?Boolean

Returns:

  • (Boolean)

215
216
217
# File 'lib/prism/posh.rb', line 215

def empty?
  to_h.empty?
end

#has_property?(key) ⇒ Boolean

Returns:

  • (Boolean)

203
204
205
# File 'lib/prism/posh.rb', line 203

def has_property?(key)
  to_h.has_key?(key)
end

#inspectObject


199
200
201
# File 'lib/prism/posh.rb', line 199

def inspect
  to_h.inspect
end

#propertiesObject


173
174
175
176
177
# File 'lib/prism/posh.rb', line 173

def properties
  @properties ||= self.class.properties.reject { |key, property|
    !property.found_in?(@first_node)
  }
end

#to_hObject


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/prism/posh.rb', line 179

def to_h
  return @to_h if @to_h
  @get_properties ||= self.class.get_properties(@first_node, properties)
  @to_h = {}
  @get_properties.each_pair do |key,value|
    @to_h[key] = if value.respond_to?(:to_h)
      value.to_h
    elsif value.respond_to?(:flatten)
      value.map {|v| v.respond_to?(:to_h) ? v.to_h : v }
    else
      value
    end
  end
  @to_h
end

#to_htmlObject


211
212
213
# File 'lib/prism/posh.rb', line 211

def to_html
  node.to_html
end

#to_sObject


207
208
209
# File 'lib/prism/posh.rb', line 207

def to_s
  node.to_s
end

#to_yamlObject


195
196
197
# File 'lib/prism/posh.rb', line 195

def to_yaml
  to_h.to_yaml
end