Class: ActWithBooleans::Admin
- Inherits:
-
Object
- Object
- ActWithBooleans::Admin
- Defined in:
- lib/act_with_booleans/admin.rb,
lib/act_with_booleans/print.rb,
lib/act_with_booleans/utils.rb,
lib/act_with_booleans/define.rb,
lib/act_with_booleans/booleans.rb
Defined Under Namespace
Classes: Location
Instance Attribute Summary collapse
-
#locations ⇒ Object
readonly
Returns the value of attribute locations.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #add_accessors(accessor, origin, mask) ⇒ Object
- #add_flag(name, pos) ⇒ Object
- #add_mask_et_all(origin) ⇒ Object
-
#initialize(model) ⇒ Admin
constructor
A new instance of Admin.
- #position(name) ⇒ Object
- #remove_accessors(accessor) ⇒ Object
- #reset ⇒ Object
- #reset_model(model) ⇒ Object
- #to_boolean(value) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(model) ⇒ Admin
Returns a new instance of Admin.
8 9 10 11 12 13 14 15 |
# File 'lib/act_with_booleans/admin.rb', line 8 def initialize(model) @locations = {} @model = model @size = 0 @boolean_hash = {} [true, "true", 1, "1"].each { |x| @boolean_hash[x] = true } [false, "false", 0, "0"].each { |x| @boolean_hash[x] = false } end |
Instance Attribute Details
#locations ⇒ Object (readonly)
Returns the value of attribute locations.
6 7 8 |
# File 'lib/act_with_booleans/booleans.rb', line 6 def locations @locations end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/act_with_booleans/admin.rb', line 4 def model @model end |
#origin ⇒ Object
Returns the value of attribute origin.
5 6 7 |
# File 'lib/act_with_booleans/admin.rb', line 5 def origin @origin end |
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'lib/act_with_booleans/admin.rb', line 6 def size @size end |
Instance Method Details
#add_accessors(accessor, origin, mask) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/act_with_booleans/define.rb', line 4 def add_accessors(accessor, origin, mask) unless model.method_defined?(:act_with_booleans) model.class_eval %( def act_with_booleans #{model}.act_with_booleans end ), __FILE__, __LINE__ - 4 end model.class_eval %( def #{accessor} #{accessor}? end def #{accessor}? !( self.#{origin}.to_i & #{mask} ).zero? end def #{accessor}=(value) booleans = self.#{origin}.to_i result = self.act_with_booleans.to_boolean(value) if result booleans |= #{mask} else booleans &= ~#{mask} end self.#{origin} = booleans result end ), __FILE__, __LINE__ - 22 end |
#add_flag(name, pos) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/act_with_booleans/utils.rb', line 4 def add_flag(name, pos) accessor = name.to_sym validate_accessor accessor, "#{accessor}?", "#{accessor}=" pos = check_pos(pos) loc = Location.new(model, origin, pos) add_to_locations accessor, loc mask = model.booleans_mask(accessor) add_accessors(accessor, origin, mask) end |
#add_mask_et_all(origin) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/act_with_booleans/utils.rb', line 16 def add_mask_et_all(origin) model.class_eval %( def booleans_mask(*names) #{model}.booleans_mask(*names) end def booleans_any?(*names) mask = #{model}.booleans_mask(*names) booleans = self.#{origin} || 0 !(booleans & mask).zero? end def booleans_all?(*names) mask = #{model}.booleans_mask(*names) booleans = self.#{origin} || 0 (booleans & mask) == mask end def booleans_none?(*names) mask = #{model}.booleans_mask(*names) booleans = self.#{origin} || 0 (booleans & mask).zero? end ), __FILE__, __LINE__ - 22 end |
#position(name) ⇒ Object
8 9 10 |
# File 'lib/act_with_booleans/booleans.rb', line 8 def position(name) @locations[name].position end |
#remove_accessors(accessor) ⇒ Object
38 39 40 |
# File 'lib/act_with_booleans/define.rb', line 38 def remove_accessors(accessor) my_undef model, accessor, "#{accessor}?", "#{accessor}=" end |
#reset ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/act_with_booleans/utils.rb', line 42 def reset names = @locations.keys.sort names.each { |name| remove_accessors name } reset_model model end |
#reset_model(model) ⇒ Object
17 18 19 |
# File 'lib/act_with_booleans/admin.rb', line 17 def reset_model(model) initialize model end |
#to_boolean(value) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/act_with_booleans/admin.rb', line 21 def to_boolean(value) res = @boolean_hash[value] return res unless res.nil? raise "invalid boolean <#{value}>" end |
#to_s ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/act_with_booleans/print.rb', line 4 def to_s res = [] res << title("Variables") res << variables(:origin) res << variables(:size) res << variables(:boolean_hash) res << blk("Booleans sorted alfabetically") { |key, loc| "#{key} #{loc}" } res << blk("Booleans and mask; sorted alfabetically") { |key, loc| "#{key} #{hex(model.booleans_mask(key))}" } res << blk("BOOLEAN assignment; sorted alfabetically") { |key, loc| "BOOLEAN_#{key.upcase} = #{hex(model.booleans_mask(key))}" } res << title("@locations") res << @locations res.flatten.join("\n") end |