class Epuber::DSL::Object
Attributes
attributes[RW]
@return [Hash<Symbol, Attribute>] The attributes of the class.
attributes_values[RW]
@return [Hash<Symbol, Any>]
file_path[R]
@return [String, nil]
Public Class Methods
from_file(file_path)
click to toggle source
Creates new instance by parsing ruby code from file
@param file_path
[String]
@return [Self]
# File lib/epuber/dsl/object.rb, line 65 def self.from_file(file_path) from_string(::File.new(file_path).read, file_path) end
from_string(string, file_path = nil)
click to toggle source
Creates new instance by parsing ruby code from string
@param string [String]
@return [Self]
# File lib/epuber/dsl/object.rb, line 75 def self.from_string(string, file_path = nil) # rubocop:disable Lint/Eval obj = if file_path eval(string, nil, file_path) else eval(string) end # rubocop:enable Lint/Eval unless obj.is_a?(self) msg = "Invalid object #{obj.class}, expected object of class #{self}" if file_path msg += ", loaded from file #{file_path}" end raise StandardError, msg end obj.instance_eval { @file_path = file_path } obj end
new()
click to toggle source
Calls superclass method
# File lib/epuber/dsl/object.rb, line 18 def initialize super @attributes_values = {} @file_path = nil end
Public Instance Methods
freeze()
click to toggle source
@return nil
Calls superclass method
# File lib/epuber/dsl/object.rb, line 32 def freeze super @attributes_values.freeze end
from_file?()
click to toggle source
@return [Bool] is created from file
# File lib/epuber/dsl/object.rb, line 101 def from_file? !file_path.nil? end
to_s()
click to toggle source
@return [String]
# File lib/epuber/dsl/object.rb, line 26 def to_s "<#{self.class} #{@attributes_values}>" end
validate()
click to toggle source
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
# File lib/epuber/dsl/object.rb, line 43 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? if attr.singularize? raise ValidationError, "missing required attribute `#{key.to_s.singularize}|#{key}`" else raise ValidationError, "missing required attribute `#{key}`" end end end
Protected Instance Methods
method_missing(name, *args)
click to toggle source
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
# File lib/epuber/dsl/object.rb, line 129 def method_missing(name, *args) if /([^=]+)=?/ =~ name attr_name = $1 location = caller_locations.first raise NameError, "Unknown attribute or method `#{attr_name}` for class `#{self.class}` in file `#{location.path}:#{location.lineno}`" else super end end