Class: Rogue::RogueObject

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/rogue.rb

Overview

Special class that allows dot notation access to member values eg. myobj = CustomStruct.new eg. myobj.with_properties(id: 1, title: ‘First Screen’, click_action: ‘click_to_web’) with this you can do: myobj.title and get ‘First Screen’

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



26
27
28
29
# File 'lib/rogue.rb', line 26

def method_missing(m, *args, &block)
  raise ArgumentError.new "Method :#{m} not defined with arguments #{args}" unless @methods.include?(m)
  @methods[m].call(*args) if @methods.include?(m)
end

Instance Method Details

#with_properties(args = Hash.new) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rogue.rb', line 14

def with_properties( args = Hash.new )
  @methods = {}
  args.each do |name,initial_value|
    if initial_value.is_a?(Proc)
      @methods[name] = initial_value
    else
      new_ostruct_member name
      send "#{name}=" , initial_value
    end
  end
  self
end