Class: Epuber::DSL::Object

Inherits:
Object
  • Object
show all
Extended by:
AttributeSupport
Defined in:
lib/epuber/dsl/object.rb

Direct Known Subclasses

Book, TreeObject

Defined Under Namespace

Classes: ValidationError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttributeSupport

attribute, define_method_attr, dsl_attributes, find_root

Constructor Details

#initializeObject

Returns a new instance of Object.



18
19
20
21
22
23
24
25
26
27
# File 'lib/epuber/dsl/object.rb', line 18

def initialize
  super
  @attributes_values = {}
  @file_path = nil

  # iterate over all attributes to write default values
  self.class.dsl_attributes.each do |key, _attr|
    self.send(key)
  end
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

Returns:

  • nil



134
135
136
137
138
139
140
141
142
# File 'lib/epuber/dsl/object.rb', line 134

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

Class Attribute Details

.attributesHash<Symbol, Attribute>

Returns The attributes of the class.

Returns:

  • (Hash<Symbol, Attribute>)

    The attributes of the class.



121
122
123
# File 'lib/epuber/dsl/object.rb', line 121

def attributes
  @attributes
end

Instance Attribute Details

#file_pathString? (readonly)

Returns:

  • (String, nil)


16
17
18
# File 'lib/epuber/dsl/object.rb', line 16

def file_path
  @file_path
end

Class Method Details

.from_file(file_path) ⇒ Self

Creates new instance by parsing ruby code from file

Parameters:

  • file_path (String)

Returns:

  • (Self)


70
71
72
# File 'lib/epuber/dsl/object.rb', line 70

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

Parameters:

  • string (String)

Returns:

  • (Self)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/epuber/dsl/object.rb', line 80

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

Instance Method Details

#freezeObject

Returns nil.

Returns:

  • nil



37
38
39
40
# File 'lib/epuber/dsl/object.rb', line 37

def freeze
  super
  @attributes_values.freeze
end

#from_file?Bool

Returns is created from file.

Returns:

  • (Bool)

    is created from file



106
107
108
# File 'lib/epuber/dsl/object.rb', line 106

def from_file?
  !file_path.nil?
end

#to_sString

Returns:

  • (String)


31
32
33
# File 'lib/epuber/dsl/object.rb', line 31

def to_s
  "<#{self.class} #{@attributes_values}>"
end

#validateObject

Note:

it only check for required values for now

Validates all values of attributes, if there is some error, StandardError will be raised

Returns:

  • nil



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/epuber/dsl/object.rb', line 48

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