class Epuber::DSL::Object
Attributes
@return [Hash<Symbol, Attribute>] The attributes of the class.
@return [Hash<Symbol, Any>]
@return [String, nil]
Public Class Methods
Source
# File lib/epuber/dsl/object.rb, line 66 def self.from_file(file_path) from_string(::File.new(file_path).read, file_path) end
Creates new instance by parsing ruby code from file
@param [String] file_path
@return [Self]
Source
# 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
Creates new instance by parsing ruby code from string
@param [String] string
@return [Self]
Source
# File lib/epuber/dsl/object.rb, line 20 def initialize super @attributes_values = {} @file_path = nil end
Calls superclass method
Public Instance Methods
Source
# File lib/epuber/dsl/object.rb, line 34 def freeze super @attributes_values.freeze end
@return nil
Calls superclass method
Source
# File lib/epuber/dsl/object.rb, line 97 def from_file? !file_path.nil? end
@return [Bool] is created from file
Source
# File lib/epuber/dsl/object.rb, line 28 def to_s "<#{self.class} #{@attributes_values}>" end
@return [String]
Source
# 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
Validates all values of attributes, if there is some error, StandardError will be raised
@note it only check for required values for now
@return nil
Protected Instance Methods
Source
# 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 message = <<~MSG Unknown attribute or method `#{attr_name}` for class `#{self.class}` in file `#{location.path}:#{location.lineno}` MSG raise NameError, message else super end end
Raise exception when there is used some unknown method or attribute
This is just for creating better message in raised exception
@return nil
Calls superclass method
Source
# File lib/epuber/dsl/object.rb, line 119 def respond_to_missing?(name, include_private = false) @attributes_values.key?(name) || super end
Calls superclass method