open_dsl 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/open_dsl.rb +1 -1
  2. data/lib/open_dsl/context.rb +13 -13
  3. metadata +3 -3
data/lib/open_dsl.rb CHANGED
@@ -8,7 +8,7 @@ require 'open_dsl/context'
8
8
  require 'open_dsl/eval_stack'
9
9
 
10
10
  module OpenDsl
11
- VERSION = '0.3.2'
11
+ VERSION = '0.3.3'
12
12
  end
13
13
 
14
14
  def open_dsl(&blk)
@@ -5,12 +5,7 @@ module OpenDsl
5
5
 
6
6
  def initialize(const_name, &blk)
7
7
  @stack = EvalStack.new(self)
8
- if constant_or_constant_name?(const_name)
9
- @toplevel_object_already_exists = existing_class?(const_name)
10
- @toplevel_object = new_instance(const_name)
11
- else
12
- @toplevel_object = Array.new
13
- end
8
+ @toplevel_object = constant_or_constant_name?(const_name) ? new_instance(const_name) : mark_as_created_by_open_dsl(Array.new)
14
9
  @stack.eval_and_keep(@toplevel_object, &blk)
15
10
  end
16
11
 
@@ -49,7 +44,7 @@ module OpenDsl
49
44
  end
50
45
 
51
46
  def assign_attribute_with_block(name, &blk)
52
- struct = OpenStruct.new
47
+ struct = mark_as_created_by_open_dsl(OpenStruct.new)
53
48
  assign_attribute(name, struct)
54
49
  @stack.eval(struct, &blk)
55
50
  end
@@ -65,7 +60,7 @@ module OpenDsl
65
60
  @stack.bottom << (values.size == 1 ? values.first : values)
66
61
  else
67
62
  define_getter_and_setter_if_needed(name)
68
- if @toplevel_object_already_exists && values.size == 0
63
+ if !created_by_open_dsl?(@stack.bottom) && values.size == 0
69
64
  @stack.bottom.send("#{name}=", true)
70
65
  else
71
66
  @stack.bottom.send("#{name}=", *values)
@@ -75,7 +70,7 @@ module OpenDsl
75
70
 
76
71
  def define_getter_and_setter_if_needed(name)
77
72
  return if @stack.bottom.respond_to?("#{name}=")
78
- raise "Expected #{@toplevel_object.class.name} to have defined a setter method for '#{name}'" if @toplevel_object_already_exists
73
+ raise "Expected #{@stack.bottom.class.name} to have defined a setter method for '#{name}'" unless created_by_open_dsl?(@stack.bottom)
79
74
 
80
75
  @stack.bottom.class.instance_eval do
81
76
  define_method("#{name}=") do |value|
@@ -92,14 +87,19 @@ module OpenDsl
92
87
  get_or_define_const(const_name).new
93
88
  end
94
89
 
95
- def existing_class?(name_or_const)
96
- return true if name_or_const.is_a?(Class)
97
- Object.const_defined?(name_or_const)
90
+ def created_by_open_dsl?(stack_object)
91
+ stack_object.class.class_variables.include?("@@created_by_open_dsl")
92
+ end
93
+
94
+ def mark_as_created_by_open_dsl(object)
95
+ object_class = object.class == Class ? object : object.class
96
+ object_class.instance_eval { class_variable_set("@@created_by_open_dsl", true) }
97
+ object
98
98
  end
99
99
 
100
100
  def get_or_define_const(name_or_const)
101
101
  return name_or_const if name_or_const.is_a?(Class)
102
- Object.const_defined?(name_or_const) ? Object.const_get(name_or_const) : Object.const_set(name_or_const, Class.new)
102
+ Object.const_defined?(name_or_const) ? Object.const_get(name_or_const) : mark_as_created_by_open_dsl(Object.const_set(name_or_const, Class.new))
103
103
  end
104
104
  end
105
105
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_dsl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 2
10
- version: 0.3.2
9
+ - 3
10
+ version: 0.3.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ian Leitch