Class: LibEagle::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/libeagle/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(attribute_name, params = {}) ⇒ Object



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/libeagle/base.rb', line 47

def self.attribute(attribute_name, params = {})
  attribute_name = attribute_name.to_s
  variable_name = "@attribute_#{attribute_name}"

  # Initialize place inside LibEagle objects
  lib_eagle_objects[:attributes][attribute_name] ||= {}
  lib_eagle_objects[:attributes][attribute_name][:required] = true if params[:required]
  lib_eagle_objects[:attributes][attribute_name][:type] = params[:type] if params[:type]
  lib_eagle_objects[:attributes][attribute_name][:valid_values] = params[:valid_values] if params[:valid_values]

  # If default value is set return default value
  if params[:default]
    define_method "attribute_#{attribute_name}".to_sym do
      instance_variable_set(variable_name, params[:default]) unless instance_variable_defined?(variable_name)
      instance_variable_get(variable_name)
    end
  else
    define_method "attribute_#{attribute_name}".to_sym do
      instance_variable_get(variable_name)
    end
  end

  # Attribute setter
  define_method "attribute_#{attribute_name}=".to_sym do |value|
    if is_valid?(attribute_name,value)
      instance_variable_set(variable_name,value)
    end
  end
end

.change_element_name(element_name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/libeagle/base.rb', line 38

def self.change_element_name(element_name)
  lib_eagle_objects[:element_name] = true
  define_method "element_name".to_sym do
      instance_variable_set("@element_name", element_name) unless instance_variable_defined?("@element_name")
      instance_variable_get("@element_name")
    end
end

.empty_elementObject



23
24
25
# File 'lib/libeagle/base.rb', line 23

def self.empty_element
  lib_eagle_objects[:empty_element] = true
end

.iname(name) ⇒ Object



6
7
8
# File 'lib/libeagle/base.rb', line 6

def self.iname(name)
  name.gsub(/^class$/, "clazz")
end

.lib_eagle_objectsObject

Getter of LibEagle objects



12
13
14
15
16
17
18
19
20
21
# File 'lib/libeagle/base.rb', line 12

def self.lib_eagle_objects
  @lib_eagle_objects ||= Hash.new
  @lib_eagle_objects[:attributes] ||= {}
  @lib_eagle_objects[:objects] ||= {}
  @lib_eagle_objects[:empty_element] ||= false
  @lib_eagle_objects[:root_element] ||= false
  @lib_eagle_objects[:text_content] ||= false
  @lib_eagle_objects[:element_name] ||= false
  @lib_eagle_objects
end

.new_with_xml(xml) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/libeagle/base.rb', line 107

def self.new_with_xml(xml)
  @base_class = self.new

  if @lib_eagle_objects[:text_content]
    @base_class.instance_variable_set("@content", xml.content)
  end

  if @lib_eagle_objects[:attributes].size > 0
    @lib_eagle_objects[:attributes].each_pair do |attribute_name, params|
      @base_class.instance_variable_set("@attribute_#{attribute_name}",xml[attribute_name]) if xml[attribute_name]
    end
  end

  if @lib_eagle_objects[:objects].size > 0
    @lib_eagle_objects[:objects].each_pair do |object_name, params|
      if xml.xpath(object_name).size > 1
        objects = []
        xml.xpath(object_name).each do |xml_node|
          objects << LibEagle::const_get(params[:class]).new_with_xml(xml_node)
        end
        @base_class.instance_variable_set("@object_#{object_name}",objects)
      elsif xml.xpath(object_name).size == 1
        xml_node = xml.xpath(object_name).first
        object = LibEagle::const_get(params[:class]).new_with_xml(xml_node)
        @base_class.instance_variable_set("@object_#{object_name}",object)
      end
    end
  end

  return @base_class
end

.newFile(file) ⇒ Object



97
98
99
100
# File 'lib/libeagle/base.rb', line 97

def self.newFile(file)
  xml = LibEagle::XML::Loader.new.loadFile(file)
  self.new_with_xml(xml)
end

.newXML(block_xml) ⇒ Object



102
103
104
105
# File 'lib/libeagle/base.rb', line 102

def self.newXML(block_xml)
  xml = LibEagle::XML::Loader.new.loadBlock(block_xml)
  self.new_with_xml(xml)
end

.object(object_name, params = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/libeagle/base.rb', line 77

def self.object(object_name, params = {})
  object_name = object_name.to_s
  variable_name = "@object_#{object_name}"

  # Initialize place inside LibEagle objects
  lib_eagle_objects[:objects][object_name] ||= {}
  lib_eagle_objects[:objects][object_name][:class] = params[:class] if params[:class]
  lib_eagle_objects[:objects][object_name][:class] = object_name.capitalize unless params[:class]

  # Object getter
  define_method "object_#{object_name}".to_sym do
    instance_variable_get(variable_name)
  end

  # Object setter
  define_method "object_#{object_name}=".to_sym do |value|
    instance_variable_set(variable_name,value)
  end
end

.root_elementObject



27
28
29
# File 'lib/libeagle/base.rb', line 27

def self.root_element
  lib_eagle_objects[:root_element] = true
end

.text_contentObject



31
32
33
34
35
36
# File 'lib/libeagle/base.rb', line 31

def self.text_content
  lib_eagle_objects[:text_content] = true

  # Define Setter and getter
  attr_accessor :content
end

Instance Method Details

#is_valid?(attribute, value) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/libeagle/base.rb', line 139

def is_valid?(attribute,value)
  params = self.class.lib_eagle_objects[:attributes][attribute]
    if params[:required]
      unless value
        raise AttributeRequired.new("#{self.class.name}: #{attribute} is required")
        return false
      end
    end

    if params[:type] && value
      unless params[:type] =~ value
        raise AttributeValueInvalid.new("`#{attribute}` value: \"#{value}\" isn't in valid range (#{params[:type]})")
        return false
      end
    end
    return true
end

#saveXMLObject



177
178
179
180
181
# File 'lib/libeagle/base.rb', line 177

def saveXML
  if valid?
    LibEagle::XML::Saver.new(self.class.lib_eagle_objects,self).parse
  end
end

#valid?Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/libeagle/base.rb', line 157

def valid?
  self.class.lib_eagle_objects[:attributes].each_pair do |attribute, params|
    value = self.instance_variable_get("@attribute_#{attribute}")
    if params[:required]
      unless value
        raise AttributeRequired.new("#{self.class.name}: #{attribute} is required")
        return false
      end
    end

    if params[:type] && value
      unless params[:type] =~ value
        raise AttributeValueInvalid.new("`#{attribute}` value: \"#{value}\" isn't in valid range (#{params[:type]})")
        return false
      end
    end
    return true
  end
end