Class: OxMlk::Elem

Inherits:
Object
  • Object
show all
Defined in:
lib/oxmlk/elem.rb

Constant Summary collapse

PROCS =
(Hash.new {|h,k| default_proc(k)}).merge(
Integer => :to_i.to_proc,
Float => :to_f.to_proc,
String => :to_s.to_proc,
Symbol => :to_sym.to_proc,
Time => proc {|e| Time.parse(e)},
:raw => proc {|e| e},
:name => proc {|e| e.name},
:value => proc {|e| e.value},
:bool => proc {|e| fetch_bool(e)})

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, o = {}, &blk) ⇒ Elem

Returns a new instance of Elem.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oxmlk/elem.rb', line 16

def initialize(name,o={},&blk)
  @tag_proc = o[:tag_proc].to_proc rescue proc {|x| x}
  @base_tag = name.to_s.chomp('?')
  @tag = @tag_proc.call(@base_tag) rescue @base_tag
  @accessor = @base_tag.intern
  @setter = "#{@accessor}=".intern
  @collection = o[:as].is_a?(Array)
  
  @from = o[:from]
  @as = [*o[:as]].compact
  @as = [:bool] if as.empty? && name.to_s.end_with?('?')
  @in = o[:in].to_s
  
  @procs = as.map {|k| PROCS[k]}
  @procs = [PROCS[:value]] + procs unless [:raw,:name,:value].include?(as.first) || ox?
  @block = blk || (collection ? proc {|x| x} : proc {|x| x.first})
end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def accessor
  @accessor
end

#asObject (readonly)

Returns the value of attribute as.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def as
  @as
end

#blockObject (readonly)

Returns the value of attribute block.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def block
  @block
end

#collectionObject (readonly)

Returns the value of attribute collection.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def collection
  @collection
end

#fromObject (readonly)

Returns the value of attribute from.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def from
  @from
end

#inObject (readonly)

Returns the value of attribute in.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def in
  @in
end

#procsObject (readonly)

Returns the value of attribute procs.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def procs
  @procs
end

#setterObject (readonly)

Returns the value of attribute setter.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def setter
  @setter
end

#tagObject (readonly)

Returns the value of attribute tag.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def tag
  @tag
end

#tag_procObject (readonly)

Returns the value of attribute tag_proc.



14
15
16
# File 'lib/oxmlk/elem.rb', line 14

def tag_proc
  @tag_proc
end

Class Method Details

.default_proc(value) ⇒ Object



87
88
89
90
91
92
# File 'lib/oxmlk/elem.rb', line 87

def self.default_proc(value)
  return value if value.is_a?(Proc)
  return proc {|x| value.from_xml(x) rescue x} if (value.ox? rescue false)
  return value.to_proc rescue proc {|x| x} if value.respond_to?(:to_proc)
  proc {|x| x.value}
end

.fetch_bool(value) ⇒ Object



80
81
82
83
84
85
# File 'lib/oxmlk/elem.rb', line 80

def self.fetch_bool(value)
  value = value.to_s.downcase
  return true if %w{true yes 1 t}.include? value
  return false if %w{false no 0 f}.include? value
  value
end

Instance Method Details

#content?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/oxmlk/elem.rb', line 39

def content?
  @from == :content || @from == '.'
end

#from_xml(data) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/oxmlk/elem.rb', line 43

def from_xml(data)
  xml = XML::Node.from(data)
  block.call(xml.search(xpath).map do |node|
    procs.inject(node) do |n,p|
      p.call(n)
    end
  end)
end

#ox?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/oxmlk/elem.rb', line 34

def ox?
  return false if as.empty?
  as.all? {|x| x.ox? rescue false}
end

#to_xml(data) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/oxmlk/elem.rb', line 52

def to_xml(data)
  value = data.send(accessor)
  
  return [] if value.nil?
  
  nodes = [*value].map do |node|
    if node.respond_to?(:to_xml)
      node.to_xml
    elsif content?
      XML::Node.new_text(node.to_s)
    else
      XML::Node.new(tag, node.to_s)
    end
  end
  nodes.empty? ? [] : nodes
end

#xpathObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/oxmlk/elem.rb', line 69

def xpath
  wrap case @from
  when nil
    ox? ? as.map(&:ox_tag).join('|') : tag
  when String
    from
  when :content
    '.'
  end
end