Class: Peanuts::Mappings::MemberMapping

Inherits:
Peanuts::Mapping show all
Defined in:
lib/peanuts/mappings.rb

Instance Attribute Summary collapse

Attributes inherited from Peanuts::Mapping

#local_name, #namespace_uri, #options, #prefix

Instance Method Summary collapse

Methods inherited from Peanuts::Mapping

#matches?, node_type

Constructor Details

#initialize(name, type, options) ⇒ MemberMapping

Returns a new instance of MemberMapping.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/peanuts/mappings.rb', line 38

def initialize(name, type, options)
  @bare_name = name.to_s.sub(/\?\z/, '')

  super(options.delete(:name) || @bare_name, options)

  @type = type
  @converter = case type
  when Symbol
    Converter.create!(type, options)
  when Class
    if type < Converter
      @type = nil
      Converter.create!(type, options)
    end
  when Array
    raise ArgumentError, "invalid value for type: #{type.inspect}" if type.length != 1
    options[:item_type] = type.first
    Converter.create!(:list, options)
  else
    raise ArgumentError, "invalid value for type: #{type.inspect}"
  end
  @name, @setter = name.to_sym, :"#{@bare_name}="
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



36
37
38
# File 'lib/peanuts/mappings.rb', line 36

def converter
  @converter
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/peanuts/mappings.rb', line 36

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



36
37
38
# File 'lib/peanuts/mappings.rb', line 36

def type
  @type
end

Instance Method Details

#clear(nut) ⇒ Object



85
86
87
# File 'lib/peanuts/mappings.rb', line 85

def clear(nut)
  set(nut, nil)
end

#define_accessors(type) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/peanuts/mappings.rb', line 62

def define_accessors(type)
  raise ArgumentError, "#{name}: method already defined or reserved" if type.method_defined?(name)
  raise ArgumentError, "#{@setter}: method already defined or reserved" if type.method_defined?(@setter)

  ivar = :"@#{@bare_name}"
  raise ArgumentError, "#{ivar}: instance variable already defined" if type.instance_variable_defined?(ivar)

  type.send(:define_method, name) do
    instance_variable_get(ivar)
  end
  type.send(:define_method, @setter) do |value|
    instance_variable_set(ivar, value)
  end
end

#read(nut, reader) ⇒ Object



77
78
79
# File 'lib/peanuts/mappings.rb', line 77

def read(nut, reader)
  set(nut, read_it(reader, get(nut)))
end

#write(nut, writer) ⇒ Object



81
82
83
# File 'lib/peanuts/mappings.rb', line 81

def write(nut, writer)
  write_it(writer, get(nut))
end