Module: Mockolate::Types::TypeMetaData

Defined in:
lib/mockolate/types.rb

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mockolate/types.rb', line 19

def self.extended(base)
  base.class_eval do
    attr_reader   :name,  :options
    attr_accessor :children

    def initialize(name:, options: nil)
      @name     = name
      @options  = options
      @children = []
    end

    def parse
      return name => value
    end

    def has_children?
      !children.empty?
    end

    def value
      _get_value.public_send(_cast)
    end

    private
    def _get_value
      options[:value] || _get_from_faker!
    end

    def _parse_child
      children.flatten.map do |child|
        child.parse
      end
    end

    def _cast
      class_name = self.class.name.split('::')

      return "to_#{class_name.last[0].downcase}".to_sym
    end

    def _get_from_faker!
      Object.const_get(
        "Faker::#{options[:fake_from]}"
      ).public_send(options[:key])
    end
    
  end
end