Class: Peanuts::Mappings::MemberMapping
- Inherits:
-
Peanuts::Mapping
- Object
- Peanuts::Mapping
- Peanuts::Mappings::MemberMapping
- Defined in:
- lib/peanuts/mappings.rb
Direct Known Subclasses
Attribute, Content, Element, ElementValue, ElementValues, Elements
Instance Attribute Summary collapse
-
#converter ⇒ Object
readonly
Returns the value of attribute converter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from Peanuts::Mapping
#local_name, #namespace_uri, #options, #prefix
Instance Method Summary collapse
- #clear(nut) ⇒ Object
- #define_accessors(type) ⇒ Object
-
#initialize(name, type, options) ⇒ MemberMapping
constructor
A new instance of MemberMapping.
- #read(nut, reader) ⇒ Object
- #write(nut, writer) ⇒ Object
Methods inherited from Peanuts::Mapping
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, ) @bare_name = name.to_s.sub(/\?\z/, '') super(.delete(:name) || @bare_name, ) @type = type @converter = case type when Symbol Converter.create!(type, ) when Class if type < Converter @type = nil Converter.create!(type, ) end when Array raise ArgumentError, "invalid value for type: #{type.inspect}" if type.length != 1 [:item_type] = type.first Converter.create!(:list, ) else raise ArgumentError, "invalid value for type: #{type.inspect}" end @name, @setter = name.to_sym, :"#{@bare_name}=" end |
Instance Attribute Details
#converter ⇒ Object (readonly)
Returns the value of attribute converter.
36 37 38 |
# File 'lib/peanuts/mappings.rb', line 36 def converter @converter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/peanuts/mappings.rb', line 36 def name @name end |
#type ⇒ Object (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
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 |