Author Topic: A little module add-on (For scripters)  (Read 2556 times)

Offline Zeriab

  • Ethereal Devastator
  • ****
  • Posts: 350
  • LV: 65
  • Mighty Scripter
    • View Profile
    • Zeriab's Junk
A little module add-on (For scripters)
« on: January 08, 2008, 01:51:41 PM »
Here's a litte addition to the module I have made ^^
Code: [Select]
class Module
  def attr_secure_accessor(sym, *args, &block)
    if block.nil? # default actions
      args = [0] if args.size == 0 #default = 0
      if args.size < 2 # One pair
        attr_writer sym
        attr_secure_reader sym, *args
      else # loads of methods followed by a default value.
        default = args[-1]
        syms = [sym].concat(args)
        syms.pop
        for sym in syms
          attr_writer sym
          attr_secure_reader sym, default
        end
      end
    else # when a block is given
      # currently just pair sematics
      args.unshift(sym)
      i = 0
      while i < args.size
        attr_writer args[i]
        attr_secure_reader args[i], args[i+1]
        i += 2
      end
    end
  end
  
  def attr_secure_reader(sym, default = 0)
    sym = sym.id2name
    string = "def #{sym};" +
             "  @#{sym} = #{default}  if @#{sym}.nil?;" +
             "  @#{sym};" +
             "end;"
    module_eval(string)
  end
end

Simple Version
Code: [Select]
class Module
  def attr_secure_accessor(sym, default = 0)
    attr_writer sym
    attr_secure_reader sym, default
  end
  
  def attr_secure_reader(sym, default = 0)
    sym = sym.id2name
    string = "def #{sym};" +
             "  @#{sym} = #{default}  if @#{sym}.nil?;" +
             "  @#{sym};" +
             "end;"
    module_eval(string)
  end
end

You can use it like this:
Code: [Select]
attr_secure_accessor :method, defaultIt works pretty much like the attr_accessor except that you can only do method creation per call.
method is the method name.
default is the default value the method returns. (If it has not been written to yet.)

An usage example:
Code: [Select]
class Foo
  attr_secure_accessor :visible, false
  attr_secure_accessor :data, 'Data_Foo.new'
end

This way you'll have that the default variable of visible is false.
If it is called before you write to the variable you will get 'false' rather than 'nil'
One thing to note is that lazy instantiation is used.
Let us assume that Data_Foo is a heavy object. It is only created if you try to read what the data attribute contains before you have written to it. More specifically. If the value of data attribute is nil then it is change to be what the default value is.

The code in the example is converted to this:
Code: [Select]
class Foo
  def visible=(value)
    @visible = value
  end
  def visible
    @visible = false  if @visible.nil?
    @visible
  end
  
  def data=(value)
    @data = value
  end
  def data
    @data = Data_Foo.new  if @data.nil?
    @data
  end
end
One thing to notice is that you must put '' around Data_Foo.new to make it a string. Otherwise you will get an error in almost all cases.

The greatest use will probably be if you want to add information to hidden classes. Let's for an example take RPG::Actor.
You can use its initialize method to set the default value by default because it won't be called automatically.
Other than that it will hopefully make the coder shorter and easier to understand.

Naturally. Only use this functionality where it is needed or where it can have a positive use ^_^

*hugs*
 ~ Zeriab
« Last Edit: December 05, 2010, 12:21:12 PM by Blizzard »

Offline Fantasist

  • Lexima Warrior
  • *******
  • Posts: 2223
  • LV: 146
  • Gender: Male
    • View Profile
    • Blog
Re: A little module add-on (For scripters)
« Reply #1 on: January 08, 2008, 04:05:37 PM »
This went into my 'Default Script Enhancement' project :)
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)

The best of freeware reviews: Gizmo's Freeware Reviews



Offline Zeriab

  • Ethereal Devastator
  • ****
  • Posts: 350
  • LV: 65
  • Mighty Scripter
    • View Profile
    • Zeriab's Junk
Re: A little module add-on (For scripters)
« Reply #2 on: January 11, 2008, 02:26:20 AM »
'Default Script Enhancement' project.
That sounds interesting. I would love to hear more about it.

I'm glad you found this useful ^_^

Offline Fantasist

  • Lexima Warrior
  • *******
  • Posts: 2223
  • LV: 146
  • Gender: Male
    • View Profile
    • Blog
Re: A little module add-on (For scripters)
« Reply #3 on: January 12, 2008, 12:15:13 AM »
Don't expect too much, just slight modding and adding features into default scripts, that's all. I used other scripts a lot efore, but right now, I only use Bliz's debugger.

About this nifty addon, I think I'll name it something shorter, like simply attr or attribute :)
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)

The best of freeware reviews: Gizmo's Freeware Reviews



Offline Zeriab

  • Ethereal Devastator
  • ****
  • Posts: 350
  • LV: 65
  • Mighty Scripter
    • View Profile
    • Zeriab's Junk
Re: A little module add-on (For scripters)
« Reply #4 on: January 12, 2008, 02:56:40 AM »
Sounds fun ^^

Don't call it attr. There is already a module method called attr. You shouldn't overwrite it ;)
I don't know about attribute though... I don't think that has been used. Still. I suggest you name them after what they do. I would have named the method attr_secure_accessor and attr_secure_reader, but since there is not auto-completion in the script editor I shortened the name a little despite it being bad style. In fact, now that I think about it *changes*

Offline Sally

  • Sallygirl :P
  • Chaos User
  • ******
  • Posts: 1283
  • LV: 12
  • Gender: Female
    • View Profile
Re: A little module add-on (For scripters)
« Reply #5 on: January 12, 2008, 03:01:09 AM »
lol....

Offline Zeriab

  • Ethereal Devastator
  • ****
  • Posts: 350
  • LV: 65
  • Mighty Scripter
    • View Profile
    • Zeriab's Junk
Re: [XP] A little module add-on (For scripters)
« Reply #6 on: January 23, 2008, 02:17:03 AM »
Sorry for the bump.
Can anyone test whether this works also works on RMVX?
I see no reason why it shouldn't, but better safe than sorry ^_^

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 20015
  • LV: 651
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: [XP] A little module add-on (For scripters)
« Reply #7 on: January 23, 2008, 02:22:01 AM »
Yeah, then you could add a [RMVX] tag. ^_^
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.