Class: Element
Overview
The Element class is a wrapper for an element attribute hash which exposes the hash and also allows access to the values using the keys as if they were methods.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Public: Gets element attribute.
-
#[]=(key, value) ⇒ Object
Public: Sets element attribute.
-
#contains?(element) ⇒ Boolean
Public: Checks if the element contains the specified element or area.
-
#initialize(hash) ⇒ Element
constructor
Public: Initializes an element.
-
#method_missing(m, *args, &_) ⇒ Object
Public: Gets/sets element attribute via accessor.
Constructor Details
#initialize(hash) ⇒ Element
Public: Initializes an element. NOT FOR USE IN TESTS.
Returns nothing.
9 10 11 |
# File 'lib/element/element.rb', line 9 def initialize(hash) @hash = rubify(hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &_) ⇒ Object
Public: Gets/sets element attribute via accessor. NOT FOR USE IN TESTS.
m - Symbol or String name of attribute. args - Splat Array of Object arguments. When used like a setter, assigns the first item as the value. _ - Block unused.
Returns nothing.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/element/element.rb', line 43 def method_missing(m, *args, &_) m = m.to_s m_key = m.sub(/=$/, '') raise %Q(No attribute "#{m_key}"!) unless @hash.keys.include?(m_key) if m_key.eql?(m) @hash[m_key] else @hash[m_key] = args.first end end |
Instance Method Details
#[](key) ⇒ Object
Public: Gets element attribute.
key - Symbol attribute to retrieve.
Returns Object value of specified attribute.
18 19 20 21 22 |
# File 'lib/element/element.rb', line 18 def [](key) key = key.to_s raise %Q(No attribute "#{key}"!) unless @hash.keys.include?(key) @hash[key] end |
#[]=(key, value) ⇒ Object
Public: Sets element attribute.
key - Symbol attribute to set. value - Object value to set.
Returns nothing.
30 31 32 33 34 |
# File 'lib/element/element.rb', line 30 def []=(key, value) key = key.to_s raise %Q(No attribute "#{key}"!) unless @hash.keys.include?(key) @hash[key] = value end |
#contains?(element) ⇒ Boolean
Public: Checks if the element contains the specified element or area.
element - Element or area Hash with keys :x, :y, :width, :height.
Returns true if the element contains the specified element or area.
59 60 61 62 |
# File 'lib/element/element.rb', line 59 def contains?(element) element[:x] >= self[:x] && (element[:x] + element[:width]) <= (self[:x] + self[:width]) && element[:y] >= self[:y] && (element[:y] + element[:height]) <= (self[:y] + self[:height]) end |