Class: ReDNS::Fragment

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/redns/fragment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support

#addr_to_arpa, #bind_all_addr, #default_nameservers, #default_resolver_address, #dns_port, #inet_aton, #inet_ntoa, #io_nonblock, #io_nonblock?, #io_set_nonblock, #is_ip?

Constructor Details

#initialize(contents = nil) ⇒ Fragment

Instance Methods =====================================================



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redns/fragment.rb', line 79

def initialize(contents = nil)
  @attributes = { }

  case (contents)
  when Hash
    assign(contents)
  when ReDNS::Buffer
    deserialize(contents)
  when String
    if (respond_to?(:primary_attribute=))
      self.primary_attribute = contents
    end
  else
    # FUTURE: Raise exception on invalid or unexpected type, but for now
    #         allow the subclass to try.
  end
  
  yield(self) if (block_given?)
end

Instance Attribute Details

#attributesObject (readonly)

Properties ===========================================================



8
9
10
# File 'lib/redns/fragment.rb', line 8

def attributes
  @attributes
end

Class Method Details

.attribute(name, options = nil) ⇒ Object

Declares an attribute of this fragment. Options:

  • :default = The default value to use if the attribute is not assigned

  • :convert = The conversion method to call on incoming values, which can

    be a Symbol, a Class, or a Proc.
    
  • :boolean = Converted to a boolean value, also introduces name? method

  • :primary = Indicates this is the primary attribute to be assigned when

    onstructed with only a String.
    


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/redns/fragment.rb', line 25

def self.attribute(name, options = nil)
  name = name.to_sym

  attribute_config = options || { }

  default = attribute_config[:default]

  if (attribute_config[:boolean])
    default ||= false
    
    attribute_config[:convert] ||= lambda { |v| !!v }
  end

  convert = attribute_config[:convert] || attribute_config[:class]
  
  define_method(name) do
    case (@attributes[name])
    when nil
      new_default = default.respond_to?(:call) ? default.call : default
      
      @attributes[name] = convert ? convert.call(new_default) : new_default
    else
      @attributes[name]
    end
  end

  if (attribute_config[:boolean])
    alias_method :"#{name}?", name
  end
  
  case (convert)
  when Symbol
    convert_sym = convert
    convert = lambda { |o| o.send(convert_sym) }
  when Class
    convert_class = convert
    convert = lambda { |o| o.is_a?(convert_class) ? o : convert_class.new(o) }
  end
  
  define_method(:"#{name}=") do |value|
    if (convert)
      value = convert.call(value)
    end
    
    @attributes[name] = value
  end

  if (attribute_config[:primary])
    alias_method :primary_attribute=, :"#{name}="
  end
end