Class: BusinessFlow::DSL::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/business_flow/dsl.rb

Overview

Helper class to manage logic around adding fields

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ Field

Returns a new instance of Field.



361
362
363
364
365
366
367
# File 'lib/business_flow/dsl.rb', line 361

def initialize(field)
  @field = field
  # For proc bindings.
  ivar_name = instance_variable_name
  @getter = ivar_name
  @setter = self.class.setter_factory(field, ivar_name)
end

Class Method Details

.eval_method(klass, name, str) ⇒ Object



378
379
380
381
382
383
# File 'lib/business_flow/dsl.rb', line 378

def self.eval_method(klass, name, str)
  return if klass.method_defined?(name) ||
            klass.private_method_defined?(name)

  unsafe_eval_method(klass, name, str)
end

.setter_factory(_field, ivar_name) ⇒ Object



390
391
392
393
394
# File 'lib/business_flow/dsl.rb', line 390

def self.setter_factory(_field, ivar_name)
  <<-SETTER
    #{ivar_name} = new_value
  SETTER
end

.unsafe_eval_method(klass, name, str) ⇒ Object



385
386
387
388
# File 'lib/business_flow/dsl.rb', line 385

def self.unsafe_eval_method(klass, name, str)
  body = ["private def #{name}", str, 'end'].join("\n")
  klass.class_eval body, __FILE__, __LINE__
end

Instance Method Details

#add_to(klass) ⇒ Object



369
370
371
372
# File 'lib/business_flow/dsl.rb', line 369

def add_to(klass)
  Field.eval_method(klass, field, getter)
  Field.eval_method(klass, setter_name, setter)
end

#nameObject



374
375
376
# File 'lib/business_flow/dsl.rb', line 374

def name
  field
end