Class: CascadingClasses::DSL

Inherits:
BlankSlate show all
Defined in:
lib/cascading_classes/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(apply_to_instances, &block) ⇒ DSL

Returns a new instance of DSL.



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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cascading_classes/dsl.rb', line 134

def initialize(apply_to_instances, &block)
  @type_raw = {}  # temp var
  @apply_to_instances = !!apply_to_instances
  @names = []
  @default, @instances_too = {}, {}
  @getters, @setters = {}, {}
  @type, @after_get = {}, {}
  @blank, @proc_inst_exec, @block_inst_exec = {}, {}, {}
  @new, @inherit = {}, {}
  @ensure_type = {}
  @options_given = {}  # need to know which options were set manually

  return unless block_given?
  if block.arity == 0
    instance_eval &block
  else
    raise "ArgumentError: wrong number of args for block: #{block.arity} for 1"
  end

  @names.each do |name|
    type = @type[name] = CascadingClasses.get_type(@default[name], *@type_raw[name])

    @blank[name] = CascadingClasses.default_blank(type) unless @blank[name]
    @new[name] = CascadingClasses.default_new(type) unless @new[name]  

    unless CascadingClasses.undefined_type?(type)
      ## unless @default.include?(name) and not @default[name].nil?
      unless @default.include?(name)
        @default[name] = @new[name].call
      end
    end

    unless @inherit.include?(name)
      @inherit[name] = CascadingClasses.undefined_type?(type) ? :undef :
        (CascadingClasses.is_container?(type) ? false : true)
    end

    @ensure_type[name] =
      CascadingClasses.default_ensure_type(type, @blank[name], @new[name]) unless
        @ensure_type.include?(name)

    @options_given[name] ||= []
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

def method_missing(meth, *args, &block)



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

def method_missing(meth, *args, &block)
  ## args.unshift(Hash.new) if block_given?   # hackish: turns empty block into a hash
  case meth
  when /inspect/
    super
  else
    __property__(meth, *args, &block)
  end
end

Instance Method Details

#__property__(name, args = {}, &block) ⇒ Object



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
67
68
69
70
71
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
# File 'lib/cascading_classes/dsl.rb', line 29

def __property__(name, args={}, &block)
  name = name.to_sym

  @names << name unless @names.include? name

  args.keys.each{|k| args[k.to_sym] = args.delete(k) unless Symbol === k}

  t = [:default, :start, :begin, :data, :template].detect{|v| args.include?(v)}
  ## t = [:default].detect{|v| args.include?(v)}
  if t
    @default[name] = args[t]
    ## (@options_given[name] ||= []) << :default
  end

  t = [:classes_only, :only_classes, :exclude_instances, :no_instances,
       :not_instances, :skip_instances].detect{|v| args.include?(v)}
  s = [:instances, :apply_to_instances, :both, :classes_and_instances,
       :instances_and_classes, :instances_too, :instance].detect{|v| args.include?(v)}
  @instances_too[name] = nil
  @instances_too[name] = !args[t] if t
  @instances_too[name] = !!args[s] if s
  @instances_too[name] = @apply_to_instances unless t or s

  t = [:getter, :getters, :reader, :readers].detect{|v| args.include?(v)}
  @getters[name] = t ? [*args[t]] : [name]

  t = [:setter, :setters, :writer, :writers].detect{|v| args.include?(v)}
  @setters[name] = t ? [*args[t]] : ["#{name}=".to_sym]

  # nil or a after callback
  t = [:after_get, :after_getter, :after_read, :after_reader,
       :after, :after_callback, :callback].detect{|v| args[v]}
  @after_get[name] = t ? args[t] : nil
  ## @after_get[name] = block if block # and t.nil?
  # NOTE: the line above doesn't work when default == {}
  #   since it assumes there is a block

  @type_raw[name] =
    if t = [:bool, :boolean, :binary, :FalseClass,
            :TrueClass].detect{|v| args.include?(v)}
      [:TrueClass, args[t]]
    elsif t = [:int, :integer, :fixnum, :Fixnum,
               Fixnum].detect{|v| args.include?(v)}
      [:Fixnum, args[t]]
    elsif t = [:float, :Float, Float].detect{|v| args.include?(v)}
      [:Float, args[t]]
    elsif t = [:string, :str, :String, String].detect{|v| args.include?(v)}
      [:String, args[t]]
    elsif t = [:dict, :hash, :Hash, Hash].detect{|v| args.include?(v)}
      [:Hash,  args[t]]
    elsif t = [:array, :list, :Array, Array].detect{|v| args.include?(v)}
      [:Array, args[t]]
    elsif t = [:set, :Set, Set].detect{|v| args.include?(v)}
      [:Set, args[t]]
    elsif t = [:proc, :Proc, Proc].detect{|v| args.include?(v)}
      [:Proc, args[t]]
    ## elsif t = [:nil, :NilClass, NilClass].detect{|v| args.include?(v)}
      ## [:NilClass, args[t]]
    elsif t = [:sym, :symbol, :Symbol, Symbol].detect{|v| args.include?(v)}
      [:Symbol, args[t]]
    else
      args.include?(:type) ? [args[:type].to_s.to_sym, true] : [nil, nil]
    end

  if t = [:blank, :null].detect{|v| args.include?(v)}
    @blank[name] = if Proc === args[t]
      args[t]
    else
      blank_val = args[t]
      Proc.new{|v| v == blank_val}
    end
    (@options_given[name] ||= []) << :blank
  end

  t = [:proc_inst_exec, :proc_instance_exec].detect{|v| args.include?(v)}
  @proc_inst_exec[name] = t ? !!args[t] : CascadingClasses.proc_inst_exec?

  t = [:block_inst_exec, :block_instance_exec].detect{|v| args.include?(v)}
  @block_inst_exec[name] = t ? !!args[t] : CascadingClasses.block_inst_exec?

  t = [:new, :new_instance, :init, :initialize].detect{|v| args.include?(v)}
  if t
    raise "#{args[t]} is not of type Proc (for #{t})" unless Proc === args[t]
    @new[name] = args[t]
    (@options_given[name] ||= []) << :new
  end
    
  t = [:ensure_type].detect{|v| args.include?(v)}
  if t
    raise "#{args[t]} is not of type Proc (for #{t})" unless Proc === args[t]
    @ensure_type[name] = args[t]
    (@options_given[name] ||= []) << :ensure_type
  end

  t = [:inherit, :inherit_mode].detect{|v| args.include?(v)}
  if t
    unless s = [nil, false, true].any?{|v| args[t] == v}
      raise "':inherit' value: #{args[t]} must be one of: nil, false, true, <fixnum>" unless
        Fixnum === args[t]
    end
    @inherit[name] = args[t]
    (@options_given[name] ||= []) << :inherit
  end
end