Class: Epuber::DSL::Object
- Inherits:
-
Object
- Object
- Epuber::DSL::Object
- Extended by:
- AttributeSupport
- Defined in:
- lib/epuber/dsl/object.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ValidationError
Class Attribute Summary collapse
-
.attributes ⇒ Hash<Symbol, Attribute>
The attributes of the class.
Instance Attribute Summary collapse
- #file_path ⇒ String? readonly
Class Method Summary collapse
-
.from_file(file_path) ⇒ Self
Creates new instance by parsing ruby code from file.
-
.from_string(string, file_path = nil) ⇒ Self
Creates new instance by parsing ruby code from string.
Instance Method Summary collapse
-
#freeze ⇒ Object
Nil.
-
#from_file? ⇒ Bool
Is created from file.
-
#initialize ⇒ Object
constructor
A new instance of Object.
- #to_s ⇒ String
-
#validate ⇒ Object
Validates all values of attributes, if there is some error, StandardError will be raised.
Methods included from AttributeSupport
attribute, define_method_attr, dsl_attributes, find_root
Constructor Details
#initialize ⇒ Object
Returns a new instance of Object.
20 21 22 23 24 |
# File 'lib/epuber/dsl/object.rb', line 20 def initialize super @attributes_values = {} @file_path = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (protected)
Raise exception when there is used some unknown method or attribute
This is just for creating better message in raised exception
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/epuber/dsl/object.rb', line 129 def method_missing(name, *args) if /([^=]+)=?/ =~ name attr_name = ::Regexp.last_match(1) location = caller_locations.first = " Unknown attribute or method `\#{attr_name}` for class `\#{self.class}` in file `\#{location.path}:\#{location.lineno}`\n MSG\n\n raise NameError, message\n else\n super\n end\nend\n" |
Class Attribute Details
.attributes ⇒ Hash<Symbol, Attribute>
Returns The attributes of the class.
112 113 114 |
# File 'lib/epuber/dsl/object.rb', line 112 def attributes @attributes end |
Instance Attribute Details
#file_path ⇒ String? (readonly)
18 19 20 |
# File 'lib/epuber/dsl/object.rb', line 18 def file_path @file_path end |
Class Method Details
.from_file(file_path) ⇒ Self
Creates new instance by parsing ruby code from file
66 67 68 |
# File 'lib/epuber/dsl/object.rb', line 66 def self.from_file(file_path) from_string(::File.new(file_path).read, file_path) end |
.from_string(string, file_path = nil) ⇒ Self
Creates new instance by parsing ruby code from string
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/epuber/dsl/object.rb', line 76 def self.from_string(string, file_path = nil) obj = if file_path eval(string, nil, file_path) # rubocop:disable Security/Eval else eval(string) # rubocop:disable Security/Eval end unless obj.is_a?(self) msg = "Invalid object #{obj.class}, expected object of class #{self}" msg += ", loaded from file #{file_path}" if file_path raise StandardError, msg end obj.instance_eval { @file_path = file_path } obj end |
Instance Method Details
#freeze ⇒ Object
Returns nil.
34 35 36 37 |
# File 'lib/epuber/dsl/object.rb', line 34 def freeze super @attributes_values.freeze end |
#from_file? ⇒ Bool
Returns is created from file.
97 98 99 |
# File 'lib/epuber/dsl/object.rb', line 97 def from_file? !file_path.nil? end |
#to_s ⇒ String
28 29 30 |
# File 'lib/epuber/dsl/object.rb', line 28 def to_s "<#{self.class} #{@attributes_values}>" end |
#validate ⇒ Object
it only check for required values for now
Validates all values of attributes, if there is some error, StandardError will be raised
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/epuber/dsl/object.rb', line 45 def validate self.class.dsl_attributes.each do |key, attr| value = @attributes_values[key] || attr.converted_value(attr.default_value) attr.validate_type(value) next unless attr.required? && value.nil? raise ValidationError, "missing required attribute `#{key.to_s.singularize}|#{key}`" if attr.singularize? raise ValidationError, "missing required attribute `#{key}`" end end |