Module: Gadgeto::Dslable::ClassMethods

Defined in:
lib/gadgeto/dslable.rb

Instance Method Summary collapse

Instance Method Details

#dslable_method(method, *args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gadgeto/dslable.rb', line 72

def dslable_method(method, *args)
  self.instance_eval do

    stored_at = method.to_s + 's' # pluralize with ActiveSupport

    # method_params:          key, *arguments, options = {}
    # method_values_set:      i.name = name
    # method_setters_getters: def key, def arguments, def options

    method_params, method_values_set, method_accessors = [], [], []

    args.each do |arg|
      unless [String, Symbol, Hash].include? arg.class
        raise ArgumentError, 'allowed arguments are Symbols, Strings and Hashes'
      end

      method_params          << (arg.is_a?(Hash) ? arg.to_a : [arg]).flatten.join(' = ')

      arg_name = (arg.is_a?(Hash) ? arg.keys.first : arg).to_s.gsub('*', '')

      method_values_set      << arg_name
      method_accessors       << arg_name
    end

    method_params = method_params.join(', ')

    method_values_set = method_values_set.map do |p|
      'i.%s = %s' % [p, p]
    end.join(';')

    method_accessors = method_accessors.map do |p|
      "def #{p}; read_attribute(:#{p}); end;" + "def #{p}=(v); write_attribute(:#{p}, v); end;"
    end.join

    self.class_eval <<-RUBY

      #{method_accessors}

      def draw(code = nil, &block)
        i = self.class.new

        if code
          i.instance_eval code
        else
          i.instance_eval &block
        end

        @#{stored_at} = i.#{stored_at}
        return nil
      end

      def #{method.to_s}(#{method_params}, &block)
        i = self.class.new
        #{method_values_set}

        i.instance_eval &block if block

        self.#{stored_at} << i
      end

      def #{stored_at}
        @#{stored_at} ||= []
      end

      def #{stored_at}=(v)
        @#{stored_at} = v
      end

      def attributes
        @attributes ||= {}
      end

      def attributes=(a)
        @attributes = a
      end

      def read_attribute(n)
        attributes[n]
      end

      def write_attribute(n, v)
        attributes[n] = v
      end

    RUBY

  end
end