Class: OxMlk::Attr

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

Constant Summary collapse

PROCS =

Named Procs for use in :as option.

(Hash.new {|h,k| k.to_proc rescue nil}).merge(
Integer => :to_i.to_proc,
Float => :to_f.to_proc,
String => :to_s.to_proc,
Symbol => :to_sym.to_proc,
:bool => proc {|a| fetch_bool(a)})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, o = {}) {|String| ... } ⇒ Attr

Creates a new instance of Attr to be used as a template for converting XML to objects and from objects to XML

Parameters:

  • name (Symbol, String)

    Sets the accessor methods for this attr. It is also used to guess defaults for :from and :as. For example if it ends with ‘?’ and :as isn’t set it will treat it as a boolean.

  • o (Hash) (defaults to: {})

    the options for the new attr definition

Options Hash (o):

  • :from (Symbol, String) — default: tag_proc.call(name)

    Tells OxMlk what the name of the XML attribute is. It defaults to name processed by the tag_proc.

  • :as (Symbol, String, Proc, Array<Symbol,String,Proc>)

    Tells OxMlk how to translate the XML. The argument is coerced into a Proc and applied to the string found in the XML attribute. If an Array is passed each Proc is applied in order with the results of the first being passed to the second and so on. If it isn’t set and name ends in ‘?’ it processes it as if :bool was passed otherwise it treats it as a string with no processing. Includes the following named Procs: Integer, Float, String, Symbol and :bool.

  • :tag_proc (Proc) — default: proc {|x| x}

    Proc used to guess :from. The Proc is applied to name and the results used to find the XML attribute if :from isn’t set.

Yields:

  • (String)

    Adds anothe Proc that is applied to the value.



36
37
38
39
40
41
42
43
44
45
# File 'lib/oxmlk/attr.rb', line 36

def initialize(name,o={},&block)
  name = name.to_s
  @accessor = name.chomp('?').intern
  @setter = "#{@accessor}=".intern
  @from = o[:from]
  @tag_proc = o[:tag_proc].to_proc rescue proc {|x| x}
  @tag = (from || (@tag_proc.call(accessor.to_s) rescue accessor)).to_s
  @as = o[:as] || (:bool if name.ends_with?('?'))
  @procs = ([*as].map {|k| PROCS[k]} + [block]).compact
end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def accessor
  @accessor
end

#asObject (readonly)

Returns the value of attribute as.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def as
  @as
end

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def from
  @from
end

#procsObject (readonly)

Returns the value of attribute procs.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def procs
  @procs
end

#setterObject (readonly)

Returns the value of attribute setter.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def setter
  @setter
end

#tagObject (readonly)

Returns the value of attribute tag.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def tag
  @tag
end

#tag_procObject (readonly)

Returns the value of attribute tag_proc.



4
5
6
# File 'lib/oxmlk/attr.rb', line 4

def tag_proc
  @tag_proc
end

Instance Method Details

#from_xml(data) ⇒ Object

Finds @tag in data and applies procs.



48
49
50
# File 'lib/oxmlk/attr.rb', line 48

def from_xml(data)
  procs.inject(XML::Node.from(data)[tag]) {|d,p| p.call(d) rescue d}
end