Kouhei Sutou
null+****@clear*****
Mon Feb 17 23:28:12 JST 2014
Kouhei Sutou 2014-02-17 23:28:12 +0900 (Mon, 17 Feb 2014) New Revision: 62f12d975af433bd15918d8f3ea969c8d3dc0e58 https://github.com/ranguba/chupa-text/commit/62f12d975af433bd15918d8f3ea969c8d3dc0e58 Message: Make data attributes as an object Added files: lib/chupa-text/attributes.rb test/test-attributes.rb Modified files: lib/chupa-text.rb lib/chupa-text/data.rb Modified: lib/chupa-text.rb (+1 -0) =================================================================== --- lib/chupa-text.rb 2014-02-17 22:14:19 +0900 (ca388b8) +++ lib/chupa-text.rb 2014-02-17 23:28:12 +0900 (100efcb) @@ -41,6 +41,7 @@ require "chupa-text/formatters" require "chupa-text/file-content" require "chupa-text/virtual-content" +require "chupa-text/attributes" require "chupa-text/data" require "chupa-text/input-data" require "chupa-text/virtual-file-data" Added: lib/chupa-text/attributes.rb (+126 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/attributes.rb 2014-02-17 23:28:12 +0900 (fb27f59) @@ -0,0 +1,126 @@ +# Copyright (C) 2014 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +module ChupaText + # Attributes of data. + class Attributes < Struct.new(:title, + :author, + :encoding, + :created_time, + :modified_time) + include Enumerable + + def initialize + super + @members = members + @extra_data = {} + end + + def to_h + super.merge(@extra_data) + end + + def inspect + super.gsub(/>\z/) do + " @extra_data=#{@extra_data.inspect}>" + end + end + + # @yield [name, value] Gives attribute name and value to the block. + # @yieldparam [Symbol] name The attribute name. + # @yieldparam [Object] value The attribute value. + def each(&block) + if block.nil? + Enumerator.new(self, :each) + else + each_pair(&block) + @extra_data.each_pair(&block) + end + end + + # Gets the value of attribute named `name`. + # + # @param [Symbol, String] name The attribute name. + # @return [Object] The attribute value. + def [](name) + name = normalize_name(name) + if****@membe*****?(name) + super + else + @extra_data[name] + end + end + + # Sets `value` as the value of attribute named `name`. + # + # @param [Symbol, String] name The attribute name. + # @param [Object] value The attribute value. + def []=(name, value) + name = normalize_name(name) + if****@membe*****?(name) + send("#{name}=", value) + else + @extra_data[name] = value + end + end + + # Sets `encoding` as the `encoding` attribute value. + # + # @param [String, Encoding, nil] encoding The encoding. + def encoding=(encoding) + super(normalize_encoding(encoding)) + end + + # Sets `time` as the `created_time` attribute value. + # + # @param [String, Integer, Time, nil] time The created time. + # If `time` is `Integer`, it is used as UNIX time. + def created_time=(time) + super(normalize_time(time)) + end + + # Sets `time` as the `modified_time` attribute value. + # + # @param [String, Integer, Time, nil] time The modified time. + # If `time` is `Integer`, it is used as UNIX time. + def modified_time=(time) + super(normalize_time(time)) + end + + private + def normalize_name(name) + name.to_sym + end + + def normalize_encoding(encoding) + if encoding.is_a?(String) + encoding = Encoding.find(encoding) + end + encoding + end + + def normalize_time(time) + case time + when String + Time.parse(time) + when Integer + Time.at(time).utc + else + time + end + end + end +end Modified: lib/chupa-text/data.rb (+3 -2) =================================================================== --- lib/chupa-text/data.rb 2014-02-17 22:14:19 +0900 (26034fc) +++ lib/chupa-text/data.rb 2014-02-17 23:28:12 +0900 (bbb3480) @@ -43,7 +43,8 @@ module ChupaText # text and meta-data. attr_accessor :path - attr_accessor :attributes + # @return [Attributes] The attributes of the data. + attr_reader :attributes # @return [Data, nil] The source of the data. For example, text # data (`hello.txt`) in archive data (`hello.tar`) have the @@ -56,7 +57,7 @@ module ChupaText @size = nil @path = nil @mime_type = nil - @attributes = {} + @attributes = Attributes.new @source = nil end Added: test/test-attributes.rb (+104 -0) 100644 =================================================================== --- /dev/null +++ test/test-attributes.rb 2014-02-17 23:28:12 +0900 (2f24517) @@ -0,0 +1,104 @@ +# Copyright (C) 2014 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TestAttributes < Test::Unit::TestCase + def setup + @attributes = ChupaText::Attributes.new + end + + sub_test_case("title") do + def test_accessor + assert_nil(@attributes.title) + @attributes.title = "Title" + assert_equal("Title", @attributes.title) + end + end + + sub_test_case("author") do + def test_accessor + assert_nil(@attributes.author) + @attributes.author = "Alice" + assert_equal("Alice", @attributes.author) + end + end + + sub_test_case("encoding") do + def test_string + @attributes.encoding = "UTF-8" + assert_equal(Encoding::UTF_8, @attributes.encoding) + end + + def test_encoding + @attributes.encoding = Encoding::UTF_8 + assert_equal(Encoding::UTF_8, @attributes.encoding) + end + + def test_nil + @attributes.encoding = nil + assert_nil(@attributes.encoding) + end + end + + sub_test_case("created_time") do + def test_string + @attributes.created_time = "2014-02-17T23:14:30+09:00" + assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), + @attributes.created_time) + end + + def test_integer + @attributes.created_time = 1392646470 + assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), + @attributes.created_time) + end + + def test_time + @attributes.created_time = Time.parse("2014-02-17T23:14:30+09:00") + assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), + @attributes.created_time) + end + + def test_nil + @attributes.created_time = nil + assert_nil(@attributes.created_time) + end + end + + sub_test_case("modified_time") do + def test_string + @attributes.modified_time = "2014-02-17T23:14:30+09:00" + assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), + @attributes.modified_time) + end + + def test_integer + @attributes.modified_time = 1392646470 + assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), + @attributes.modified_time) + end + + def test_time + @attributes.modified_time = Time.parse("2014-02-17T23:14:30+09:00") + assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), + @attributes.modified_time) + end + + def test_nil + @attributes.modified_time = nil + assert_nil(@attributes.modified_time) + end + end +end -------------- next part -------------- HTML����������������������������... Descargar