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 [String] file_path

@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)
  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
new() click to toggle source
Calls superclass method
# File lib/epuber/dsl/object.rb, line 19
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 33
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 96
def from_file?
  !file_path.nil?
end
to_s() click to toggle source

@return [String]

# File lib/epuber/dsl/object.rb, line 27
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 44
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

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 128
      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
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/epuber/dsl/object.rb, line 118
def respond_to_missing?(name, include_private = false)
  @attributes_values.key?(name) || super
end